Passing object by value gives weird result

Started by
1 comment, last by WitchLord 8 years, 8 months ago

Hi, Im having some trouble calling an as method and passing an object per value.

This is my as code:


class Object
{

    Object()
    {
    }

    void OnUpdate(Entity entity, int deltaMs)
    {
        Print("Hello world\n" + deltaMs);
    }
};

If I remove "Entity entity" (and other corresponding code) everything works fine. If I add it back in I get a weird error (see below), so my guess is that it has something to do with the entity class.

Here is the code I register it with:


void ConstructEntityDefault(void* memory)
{
    new (memory) Entity();
}

void ConstructEntity(const Entity entity, void* memory)
{
    new (memory) Entity(entity);
}

void DestructEntity(void* memory)
{
    ((Entity*)memory)->~Entity();
}

//later on

r = m_engine->RegisterObjectType("Entity", sizeof(Entity), asOBJ_VALUE | asOBJ_APP_CLASS_CD  );
assert( r >= 0 );
r = m_engine->RegisterObjectBehaviour("Entity", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructEntityDefault), asCALL_CDECL_OBJLAST);
assert( r >= 0 );
r = m_engine->RegisterObjectBehaviour("Entity", asBEHAVE_CONSTRUCT, "void f(const Entity &in)", asFUNCTION(ConstructEntity), asCALL_CDECL_OBJLAST);
assert( r >= 0 );
r = m_engine->RegisterObjectBehaviour("Entity", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructEntity), asCALL_CDECL_OBJLAST);
assert( r >= 0 );

And here is the code to call it:


void CallFunction(asIScriptFunction* func, void* object, Entity entity, int delta)
    {
        assert(func);
        int r = m_ctx->Prepare(func);
        assert( r >= 0 );
        if(object != nullptr)
        {
            r = m_ctx->SetObject(object);
            assert( r >= 0 );
        }

        r = m_ctx->SetArgObject(0, &entity);
        assert(r);
        r = m_ctx->SetArgDWord(1, delta);
        assert(r); 

If I call the first SetArgObject method, m_ctx (asIScriptContext) becomes 0x0. More precisely, it happens when calling the copy constructor in ConstructEntity(...)

The constructor is defined like this:


Entity(const Entity &other) = default;

If you need any other information let me know.

Thanks in advance

Epi

Advertisement

Got it to work


void ConstructEntity(const Entity entity, void* memory)

needs to get a reference


void ConstructEntity(const Entity& entity, void* memory)

smile.png

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

This topic is closed to new replies.

Advertisement