Jump to content
  • Advertisement
Sign in to follow this  
BloodyEpi

Passing object by value gives weird result

This topic is 1144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by BloodyEpi

Share this post


Link to post
Share on other sites
Advertisement

Got it to work

void ConstructEntity(const Entity entity, void* memory)

needs to get a reference

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!