Hey,
I'm re-working the level saving in the game I'm working on and I'm trying to work out how to handle entity construction when loading.
I have an entity base class and several entity types that derive from it,
Entities are currently saved by storing a type index, which is passed in to a switch statement to construct the correct type when loading.
So I was looking for a way to do this automatically, without having to add items to the switch statement for each new entity type.
I tried using function pointers to constructors, but I could only get it to work for the base class, I couldn't work out how to cast the derived constructor to the base function pointer type...
Another option is to add another context and a c++ function that calls the factory function and returns the object reference, but that seems like a kind of round about way of doing it..
So I guess my question is, is there a way to construct an object by its string name (directly from AngelScript), or by some other stored value, like a reference to the constructor?
Sorry if this has been answered, I have read the manual and done some searching.
Thanks!
Entity factory
What you are looking to do is something akin to object serialization. The problem is that entity type ID's can vary from game instance to game instance, so you will need a way to store the type in a manner that is independent of the numerical type ID.
What you could do is instead of storing the ID is you could store the class name (asIObjectType::GetName) and any other relevant data then pass that into your deserialization function (the function with the potentially massive switch statement).
You might find an interface in angelscript (ISerializable or ISavable for instance) useful for this to extend beyond predefined type you were using before.
What you could do is instead of storing the ID is you could store the class name (asIObjectType::GetName) and any other relevant data then pass that into your deserialization function (the function with the potentially massive switch statement).
You might find an interface in angelscript (ISerializable or ISavable for instance) useful for this to extend beyond predefined type you were using before.
Hey, thanks for the reply _orm_, and sorry about the late response.
I found what I was looking for a while ago, which was a way to construct an object by its name directly in AngelScript (via c++), without having to create a second context.
Does exactly what I wanted, I can construct script objects from a c++ function that is called from AngelScript.
I ended up deciding to just store the entity type name, rather than maintain a list of ID's matched to string names, and I did the same for the names of stored variables, too.
As it turns out, it wasn't worth worrying about a few extra bytes per entity and even per variable because the rest of the level data (tiles, props) far outweighs the relatively sparsely placed entities.
Also storing variable names allows variables to be added/removed/changed in entity types without breaking existing level data, which is nice.
Thanks for the help,
And thanks [color="#1C2837"]Andreas for AngelScript, it has been invaluable to the project I'm working on! (:
I found what I was looking for a while ago, which was a way to construct an object by its name directly in AngelScript (via c++), without having to create a second context.
int32 typeId = _moduleMain->GetTypeIdByDecl( typeName );
scriptEntity = ( asIScriptObject* )_engine->CreateScriptObject( typeId );
Does exactly what I wanted, I can construct script objects from a c++ function that is called from AngelScript.
I ended up deciding to just store the entity type name, rather than maintain a list of ID's matched to string names, and I did the same for the names of stored variables, too.
As it turns out, it wasn't worth worrying about a few extra bytes per entity and even per variable because the rest of the level data (tiles, props) far outweighs the relatively sparsely placed entities.
Also storing variable names allows variables to be added/removed/changed in entity types without breaking existing level data, which is nice.
Thanks for the help,
And thanks [color="#1C2837"]Andreas for AngelScript, it has been invaluable to the project I'm working on! (:
Hey, thanks for the reply _orm_, and sorry about the late response.
I found what I was looking for a while ago, which was a way to construct an object by its name directly in AngelScript (via c++), without having to create a second context.
int32 typeId = _moduleMain->GetTypeIdByDecl( typeName );
scriptEntity = ( asIScriptObject* )_engine->CreateScriptObject( typeId );
Does exactly what I wanted, I can construct script objects from a c++ function that is called from AngelScript.
I ended up deciding to just store the entity type name, rather than maintain a list of ID's matched to string names, and I did the same for the names of stored variables, too.
As it turns out, it wasn't worth worrying about a few extra bytes per entity and even per variable because the rest of the level data (tiles, props) far outweighs the relatively sparsely placed entities.
Also storing variable names allows variables to be added/removed/changed in entity types without breaking existing level data, which is nice.
Thanks for the help,
And thanks [color="#1C2837"]Andreas for AngelScript, it has been invaluable to the project I'm working on! (:
Oh wow..... I wish I had seen that one earlier because I wanted to something similar before as a sort of "Spawn" function. I feel like a bloody idiot.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement