Advertisement

Function Arguments in angelscript

Started by January 15, 2022 01:37 PM
2 comments, last by MaxCE 2 years, 8 months ago

seems like if you want to set function arguments you have to know the type of the argument. but what if i don't know the type of the argument. there isn't any anonymous or template ctx→SetArg() function so what should i use. this is the code.

    template<class... Args>
    inline int callfunc(const char* funcdecl, asIScriptModule* module, asIScriptEngine* engine, Args... args)
    {
        // Create our context, prepare it, and then execute
        asIScriptContext* ctx = engine->CreateContext();

        asIScriptFunction* func = module->GetFunctionByDecl(funcdecl);

        if (func != 0)
        {
            ctx->Prepare(func);

            if constexpr(sizeof...(args) > 0)
            {
                int num = 0;
                ((ctx->SetArg(num++, args)), ...); //at first i thought it was SetArgDWord but that's for a Dword argument
            }

            int r = ctx->Execute();
            if (r != asEContextState::asEXECUTION_FINISHED)
            {
                if (r == asEContextState::asEXECUTION_EXCEPTION)
                {
                    printf("An exception '%s' occurred. Please correct the code and try again.\n", ctx->GetExceptionString());
                }
            }

            // Clean up
            ctx->Release();
            return 1;
        }
        return 0;
    }

True, There is no generic function for setting script function arguments without knowing the type.

You'll need to write the logic to determine the type expected by the function, and do the type conversion from the C++ arg, and then call the appropriate SetArg function (or use GetAddressOfArg and assign the value). You can determine the type that the script function expects with asIScriptFunction::GetParam.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

@WitchLord it's been a long time but i manged it using a template helper function. there are as manyexplicit declarations of the type as there are setarg functions. you pass the ctx and position of the argument and during compile time it'll get figured out dring the template deducing stage. right now i'm having trouble with registering classes but that's a whole 'nother issue

This topic is closed to new replies.

Advertisement