Advertisement

Just starting with AngelScript. How do I define this type?

Started by September 05, 2023 11:28 AM
16 comments, last by StrikerTheHedgefox 1 year, 2 months ago

Yes, that could work. Though as you register it as asOBJ_VALUE | asOBJ_POD as script could attempt to copy it, which obviously wouldn't work.

Instead I suggest you register it with asOBJ_REF | asOBJ_NOHANDLE. This will prevent the script from taking any copies of the array or even store additional references to it.

https://angelcode.com/angelscript/sdk/docs/manual/doc_adv_single_ref_type.html

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

Sometimes I might want to copy the data it's outputting, like making a backup of the sprite data in AngelScript. Like:

sprite_t spriteBackup = sprite[num];

What would I do then?

None

Advertisement

I have another question including the last one. I have a struct array that has an array within, like:

typedef struct
{
    int32_t t_data[10];
    // other stuff
} actor_t;

actor_t actor[16384];

I want to be able to access it in AngelScript like actor[actorNum].t_data[1]
is this doable?

None

The individual sprite type can be registered with asOBJ_VALUE to allow it to be copied as a value. But for the array of sprites type you probably don't want to use asOBJ_VALUE because that would mean the array should be allocated on the stack, which is probably not adequate if it is large (which I suspect it is).

If the array is not always the same size, then I suggest you define a proxy object so the script engine doesn't manipulate the C++ array directly, but instead only work with it indirectly through the proxy object.

To the proxy object you can add proper reference counting to make sure it is deallocated when no longer used, and then register it with asOBJ_REF.

The same advice goes for the actor type.

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

But for the actor type, how would I construct things? That's what I'm asking. How do I get it so that the array within the array knows what parent struct array index it's accessing t_data from? t_data is a static size, btw.

Like, this obviously doesn't work:

asEngine->RegisterObjectProperty("actor_t", "int32_t t_data[10]", asOFFSET(actor_t, t_data));

// actor_t
asEngine->RegisterObjectType("actor_t", sizeof(actor_t), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits<actor_t>());
//asEngine->RegisterObjectProperty("actor_t", "int32_t t_data[10]", asOFFSET(actor_t, t_data)); <- Can't access this. I need to be able to do actor[idx].t_data[idx2]; in AngelScript. I need my C++ program to know which index of the base actor[] to get t_data from.
asEngine->RegisterObjectProperty("actor_t", "uint32_t flags", asOFFSET(actor_t, flags));
asEngine->RegisterObjectProperty("actor_t", "vec3_t bpos", asOFFSET(actor_t, bpos));
asEngine->RegisterObjectProperty("actor_t", "int32_t floorz", asOFFSET(actor_t, floorz));
asEngine->RegisterObjectProperty("actor_t", "int32_t ceilingz", asOFFSET(actor_t, ceilingz));
asEngine->RegisterObjectProperty("actor_t", "vec2_t lastv", asOFFSET(actor_t, lastv));
asEngine->RegisterObjectProperty("actor_t", "int16_t htpicnum", asOFFSET(actor_t, htpicnum));
asEngine->RegisterObjectProperty("actor_t", "int16_t htang", asOFFSET(actor_t, htang));
asEngine->RegisterObjectProperty("actor_t", "int16_t htextra", asOFFSET(actor_t, htextra));
asEngine->RegisterObjectProperty("actor_t", "int16_t htowner", asOFFSET(actor_t, htowner));
asEngine->RegisterObjectProperty("actor_t", "int16_t movflag", asOFFSET(actor_t, movflag));
asEngine->RegisterObjectProperty("actor_t", "int16_t tempang", asOFFSET(actor_t, tempang));
asEngine->RegisterObjectProperty("actor_t", "int16_t timetosleep", asOFFSET(actor_t, timetosleep));
asEngine->RegisterObjectProperty("actor_t", "int16_t stayput", asOFFSET(actor_t, stayput));
asEngine->RegisterObjectProperty("actor_t", "int8_t floorzoffset", asOFFSET(actor_t, floorzoffset));
asEngine->RegisterObjectProperty("actor_t", "int8_t waterzoffset", asOFFSET(actor_t, waterzoffset));
asEngine->RegisterObjectProperty("actor_t", "int16_t dispicnum", asOFFSET(actor_t, dispicnum));
asEngine->RegisterObjectProperty("actor_t", "uint8_t cgg", asOFFSET(actor_t, cgg));
asEngine->RegisterObjectProperty("actor_t", "uint8_t lasttransport", asOFFSET(actor_t, lasttransport));

asEngine->RegisterObjectType("actorarray_t", 1, asOBJ_REF | asOBJ_NOHANDLE);
asEngine->RegisterObjectMethod("actorarray_t", "actor_t &opIndex(uint32_t idx)", asFUNCTION(asOpIndex_actor), asCALL_CDECL_OBJLAST);
asEngine->RegisterGlobalProperty("actorarray_t actor", &dummyPtr);

None

Did this, which seems to work. But, is there maybe a better way for a static array?

inline int32_t& asOpIndex_tdata(uint32_t idx, int32_t* t_data)
{
    if (idx >= ARRAY_SIZE(actor[0].t_data))
    {
        LOG_F(ERROR, "AngelScript: Tried to access invalid t_data index %d!", idx);
        return t_data[0];
    }

    return t_data[idx];
}
    // actor_t
   asEngine->RegisterObjectType("tdata_t", 1, asOBJ_REF | asOBJ_NOHANDLE);
   asEngine->RegisterObjectMethod("tdata_t", "int32_t &opIndex(uint32_t idx)", asFUNCTION(asOpIndex_tdata), asCALL_CDECL_OBJLAST);

   asEngine->RegisterObjectType("actor_t", sizeof(actor_t), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits<actor_t>());
   asEngine->RegisterObjectProperty("actor_t", "tdata_t t_data", asOFFSET(actor_t, t_data));

None

Advertisement

If you need for the syntax in Angelscript to be “actor[actorNum].t_data[1]” then you did it right.

But if you will mainly be working with the array directly to access the members of the elements, then you could register an opIndex with two parameters on the actorarray_t type directly. It would be more efficient as there would not be a need to make two calls, but it would change the syntax a bit to “actor[actorNum, 1]”

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

For the sake of syntactical clarity, I'll leave it as-is. Because with the parameter like that, it's less clear what the second parameter actually does/accesses.

None

This topic is closed to new replies.

Advertisement