Question is it Possible to get an object Pointer...
It is possible to write a C++ Class(Interface) and inheritance this class in anglescript and then get the a pointer to that object and work with this pointer as it a normal pointer?
If above is not possible is there a good work around to create classes and inheritance all from one interface to make it easy to use them with c++ code?
It seem that no one understand me :)
I try it again:
Is there a way that i get in c++ an object pointer to class so that i store them e.g. in std::vector and call rom each object a update function?
And if yes how it works?
I try it again:
Is there a way that i get in c++ an object pointer to class so that i store them e.g. in std::vector and call rom each object a update function?
And if yes how it works?
Give all of these objects that need to update a common base class - make the Update function virtual, derive whatever objects you need updating from that base class, and pass the base class pointer into the vector, hey presto, you've got a list of objects that you can cycle through to update
- Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
and how it works via anglescript?
I mean i want the vector cycle hardcode in c++ bur the objects out in anglescript how does it work?
I mean i want the vector cycle hardcode in c++ bur the objects out in anglescript how does it work?
Register a smartptr type class w/ AS, then register a std::vector binding for the smart pointer class, and a creational method that returns the smart pointers, and you will have code like this:
That is kind of crappy way, but you should be able to improve on it.
ObjArray myObjs;myObjs.PushBack(CreateSmartObject("Square");
That is kind of crappy way, but you should be able to improve on it.
The real problem with this is that you have to use the ScriptAny class to get types declared in angelscript back out. This tends to be a real pain.
The best model for this right now is probably to define a 'base class' that knows which script-functions to call.
Something like that. :) If you go down a few threads, you'll see a 'wrapper library' I posted. It could really streamline this process for you. In fact, it will handle just about everything for you. :)
The best model for this right now is probably to define a 'base class' that knows which script-functions to call.
class base_class{public: some_type table_of_script_functions; void foo() { call_script_function(table_of_script_functions["foo"]); }};
Something like that. :) If you go down a few threads, you'll see a 'wrapper library' I posted. It could really streamline this process for you. In fact, it will handle just about everything for you. :)
Currently you could do it something like this:
AngelScript:
C++:
The above code will not compile, I just wrote it quickly to give you an idea on how you can do what you want. You'll likely want to optimize the code as well, especially the GetFunctionID and GetMethodID should be cashed after each script compilation.
I recognize that the above is a very convoluted way of doing what you need, and I've worked hard to get improve this. With the next version that is coming out (2.7.0) you'll be able to simplify the code by quite a bit.
AngelScript:
C++:
I hope I understood what you want to do, so that I didn't write all this for nothing. [wink]
Regards,
Andreas
AngelScript:
// Declare the class with the Update methodclass Monster{ void Update() { // Update this object }};// A factory functionany@ CreateObject(){ return any(@Monster());}
C++:
// Compile all the scripts into separate modulesengine->AddScriptSection("monster", script, strlen(script));engine->Build("monster");// This is the list of objects that will be updated each framevector<asIScriptStruct*> objects;// Create the script object and add it to the listasIScriptContext *ctx = engine->CreateContext();ctx->Prepare(engine->GetFuncIDByDecl("monster", "any @CreateObject()"));ctx->Execute();asIScriptAny *any = ctx->GetReturnObject();asIScriptStruct *obj = 0;int typeId = any->GetTypeId();any->Retrieve(&obj, typeId);any->Release();objects.push_back(obj);// Now call update on each objectfor( UINT n = 0; n < objects.size(); n++ ){ asIScriptStruct *obj = objects[n]; int typeId = obj->GetStructTypeId(); string className = engine->GetTypeDeclaration(typeId); ctx->Prepare(engine->GetMethodIDByDecl("monster", className.c_str(), "void Update()")); ctx->SetObject(obj); ctx->Execute();}
The above code will not compile, I just wrote it quickly to give you an idea on how you can do what you want. You'll likely want to optimize the code as well, especially the GetFunctionID and GetMethodID should be cashed after each script compilation.
I recognize that the above is a very convoluted way of doing what you need, and I've worked hard to get improve this. With the next version that is coming out (2.7.0) you'll be able to simplify the code by quite a bit.
AngelScript:
// Declare the class that implements the AppIntfclass Monster : AppIntf{ void Update() { // Update this object }};// A factory functionAppIntf@ CreateObject(){ return Monster();}
C++:
// Compile all the scripts into separate modulesengine->RegisterInterface("AppIntf");engine->RegisterInterfaceMethod("AppIntf", "void Update()");engine->AddScriptSection("monster", script, strlen(script));engine->Build("monster");// This is the list of objects that will be updated each framevector<asIScriptStruct*> objects;// Create the script object and add it to the listasIScriptContext *ctx = engine->CreateContext();ctx->Prepare(engine->GetFuncIDByDecl("monster", "AppIntf@ CreateObject()"));ctx->Execute();asIScriptStruct *obj = ctx->GetReturnObject();obj->AddRef();objects.push_back(obj);// Now call update on each objectint typeId = engine->GetTypeIdByDecl(0, "AppIntf");int funcId = engine->GetMethodIDByDecl(typeId, "void Update()");for( UINT n = 0; n < objects.size(); n++ ){ ctx->Prepare(funcId); ctx->SetObject(objects[n]); ctx->Execute();}
I hope I understood what you want to do, so that I didn't write all this for nothing. [wink]
Regards,
Andreas
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