Creating "instances" of a script.
For a game that i am developing, i would like to use AngelScript. The game level editor will allow the player to add a new entity into the level. The entity is pretty much just an angel script. I want them to be able to add as much entities using the same script as they want...so like when they add an entity, it creates an "instance" of the script, and they do NOT share variables.
I was going to implement it by adding the definition of the entity with AddScriptSection() and than for every instance i create a context for the engine and call the new modules main function.
Than i saw the sample "Events" and noticed that
"string char = \"-\"; "
"bool doQuit = false; "
were being shared between the two modules....that sort of ruins what i wanted to where each module has its variables rather than sharing. How can i make instances of a script?
EDIT: Ok now that i look again, the events example does not give a module name, just a 0. Would what i described work if each definition of an enitity has its own module name?
Ok after reading the documentation a bit more, i dont think its possible to create instances of scripts, unless i copy the code and build it for each instance of the same script. A better example of what i was hoping to achieve is similiar to classes. You only define the code once and you can make many instances of the one class and each instance only takes up as much memory as the variables defined in the class.
Well, you actually can define classes in script (with members and methods), which you can instantiate either from script or from native code. Is that what you want?
What Dentoid said is the best you can do for now.
I have plans for a future version of AngelScript that will allow cloning of compiled modules. This process will duplicate all global variables, but the byte code will be shared between the modules. This will not be implemented for a while yet though, as it requires quite a lot of restructuring of the AngelScript code.
I have plans for a future version of AngelScript that will allow cloning of compiled modules. This process will duplicate all global variables, but the byte code will be shared between the modules. This will not be implemented for a while yet though, as it requires quite a lot of restructuring of the AngelScript code.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Is there any way to get the variable pointers for an instance of a class defined in AngelScript?
EDIT: Alright this is how i think im going to do it. The entities will be a class and they will be event-based (like void onCollision()).
I get the event method ID using "GetMethodIDByName()" and create instances of an entity using "CreateScriptObject()". Then when i want to call an event i do
context->SetObject(entityInstance);
contect->Prepare(methodId);
so now i just need to find out a way to get the pointer to the variables defined in the class, and i need to do this for each instance..?(such as float x, float y, float z, float rotation, etc). These variables will be in every entity and its safe to assume that they will be the first variables defined in the classes, and always in the same order (because if there is no standard way of getting these pointers, there may be a way of retrieving them using offsets and the pointer to the object)
Would the following code work?:
//c++
void* obj = CreateScriptObject(blah);
float* x = (float*)obj;
float* y = (float*)obj + 4;
float* z = (float*)obj + 8;
float* rotation = (float*)obj + 12;
or is the structure of an object not that simple?
[Edited by - 39ster on December 5, 2007 2:13:56 AM]
EDIT: Alright this is how i think im going to do it. The entities will be a class and they will be event-based (like void onCollision()).
I get the event method ID using "GetMethodIDByName()" and create instances of an entity using "CreateScriptObject()". Then when i want to call an event i do
context->SetObject(entityInstance);
contect->Prepare(methodId);
so now i just need to find out a way to get the pointer to the variables defined in the class, and i need to do this for each instance..?(such as float x, float y, float z, float rotation, etc). These variables will be in every entity and its safe to assume that they will be the first variables defined in the classes, and always in the same order (because if there is no standard way of getting these pointers, there may be a way of retrieving them using offsets and the pointer to the object)
Would the following code work?:
//c++
void* obj = CreateScriptObject(blah);
float* x = (float*)obj;
float* y = (float*)obj + 4;
float* z = (float*)obj + 8;
float* rotation = (float*)obj + 12;
or is the structure of an object not that simple?
[Edited by - 39ster on December 5, 2007 2:13:56 AM]
Once you have the pointer to the script class that you received with CreateScriptObject, you should use the methods of asIScriptStruct to enumerate and access the class properties.
You shouldn't rely on a hardcoded offset from the object address for accessing the variables, because AngelScript stores private data within the structure (reference counter and other things). Future versions of AngelScript may change how much private data that is stored, thus changing the offset the member variables. It's also possible that I'll change the alignment of the member variables, so you shouldn't even rely on the properties being stored with 4 bytes difference.
You shouldn't rely on a hardcoded offset from the object address for accessing the variables, because AngelScript stores private data within the structure (reference counter and other things). Future versions of AngelScript may change how much private data that is stored, thus changing the offset the member variables. It's also possible that I'll change the alignment of the member variables, so you shouldn't even rely on the properties being stored with 4 bytes difference.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
Ok im a bit confused on how to use asIScriptStruct....I create an instance of a class with CreateScriptObject(), but how do i get asIScriptStruct involved?
Like this?:
void* obj = CreateScriptObject(dajdhgkdgh);
asIScriptStruct* objStruct = (asIScriptStruct*)obj;
float* x = (float*)objStruct->GetPropertyPointer(0);
float* y = (float*)objStruct->GetPropertyPointer(1);
float* z = (float*)objStruct->GetPropertyPointer(2);
float* rotation = (float*)objStruct->GetPropertyPointer(3);
???
Like this?:
void* obj = CreateScriptObject(dajdhgkdgh);
asIScriptStruct* objStruct = (asIScriptStruct*)obj;
float* x = (float*)objStruct->GetPropertyPointer(0);
float* y = (float*)objStruct->GetPropertyPointer(1);
float* z = (float*)objStruct->GetPropertyPointer(2);
float* rotation = (float*)objStruct->GetPropertyPointer(3);
???
Exactly.
CreateScriptObject will return a pointer to an object of the type you requested. In the case of script declared classes you'll get a pointer to an asIScriptStruct interface, that you use to interact with the script class.
CreateScriptObject will return a pointer to an object of the type you requested. In the case of script declared classes you'll get a pointer to an asIScriptStruct interface, that you use to interact with the script class.
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
Popular Topics
Advertisement