Hmm.. that's unfortunate, because that's one of the primary features I was looking for in a scripting language =(...
At least AS works though, so it beats GM ;)
I wonder what I'd have to "donate" in order for that feature to get implemented =)
My Message Handling Functionality in AS
Quote:
Original post by RsblsbShawn
Hmm.. that's unfortunate, because that's one of the primary features I was looking for in a scripting language =(...At least AS works though, so it beats GM ;)
You can probably still accomplish most of what you want by using basic design patterns. It may be require a few extra steps such as the use of interfaces and storing a behavior object in the class but is supported in the language. You might end up with something like:
class A : IEntity { IEntityBehaviour @updateBEH; A() { } void update() { if(updateBEH != null) { updateBEH.exec(this); } }}
The following example shows how to implement a global factory for instantiation of objects across modules. This is the closest example I have that matches what you described in your original post (replacing an object at runtime). Hopefully this will help you on your journey a little...
#include "angelscript.h"#include "utils.h"static COutStream out;static asIScriptStruct *objectFactory = NULL;static const char *const scriptTest ="IObject @object1; \n""IObject @object2; \n""IObject @object3; \n"" \n""interface IObject1 \n""{ \n"" void execute1(int id) const; \n"" void execute2(int id); \n""} \n"" \n""void DoTest(IObject @object) \n""{ \n"" object.execute1(1); \n"" object.execute2(2); \n""} \n"" \n""void Test1() \n""{ \n"" IObject @object; \n"" \n"" @object = objectFactory.create(); \n"" DoTest(object); \n""} \n"" \n""void Test2() \n""{ \n"" DoTest(object1); \n"" DoTest(object2); \n"" DoTest(object3); \n""} \n"" \n";static const char *const scriptObject1 ="class Object1 : IObject \n""{ \n"" void execute1(int id) { execute(1, 1, id); } \n"" void execute2(int id) { execute(1, 2, id); } \n""} \n"" \n""class Object1Factory : IObjectFactory \n""{ \n"" IObject @create() \n"" { \n"" execute(1, 0, 0); \n"" return Object1(); \n"" } \n""} \n";static const char *const scriptObject2 ="class Object2 : IObject \n""{ \n"" void execute2(int id) { execute(2, 2, id); } \n"" void execute1(int id) { execute(2, 1, id); } \n""} \n"" \n""class Object2Factory : IObjectFactory \n""{ \n"" IObject @create() \n"" { \n"" execute(2, 0, 0); \n"" return Object2(); \n"" } \n""} \n";static void execute(asDWORD typeID, asDWORD methodID1, asDWORD methodID2){ if(0 == methodID1) { printf("Creating type %d\n", typeID); } else { printf("Object%d.execute%d() Object%d.execute%d()\n", typeID, methodID1, typeID, methodID2); }}static void SetFactory(asIScriptEngine *engine, const char *module, const char *object){ int typeId = engine->GetTypeIdByDecl(module, object); if(typeId >= 0) { asIScriptStruct *obj; obj = (asIScriptStruct *)engine->CreateScriptObject(typeId); if(NULL != obj) { if(NULL != objectFactory) { objectFactory->Release(); } objectFactory = obj; } }}bool TestXFactory(){ asIScriptEngine *engine; int r; engine = asCreateScriptEngine(ANGELSCRIPT_VERSION); assert(NULL != engine); r = engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL); assert(r >= 0); r = engine->RegisterGlobalFunction("void execute(int,int,int)", asFUNCTION(execute), asCALL_CDECL); r = engine->RegisterInterface("IObject"); assert(r >= 0); r = engine->RegisterInterfaceMethod("IObject", "void execute1(int)"); assert(r >= 0); r = engine->RegisterInterfaceMethod("IObject", "void execute2(int)"); assert(r >= 0); r = engine->RegisterInterface("IObjectFactory"); assert(r >= 0); r = engine->RegisterInterfaceMethod("IObjectFactory", "IObject @create()"); assert(r >= 0); r = engine->RegisterGlobalProperty("IObjectFactory @objectFactory", &(objectFactory = NULL)); assert(r >= 0); // Build base test script r = engine->AddScriptSection("test", NULL, scriptTest, strlen(scriptTest), 0); assert(r >= 0); r = engine->Build("test"); assert(r >= 0); r = engine->BindAllImportedFunctions("test"); assert(r >= 0); // Build script 1 r = engine->AddScriptSection("object1", NULL, scriptObject1, strlen(scriptObject1), 0); assert(r >= 0); r = engine->Build("object1"); assert(r >= 0); r = engine->BindAllImportedFunctions("object1"); assert(r >= 0); // Build script 2 r = engine->AddScriptSection("object2", NULL, scriptObject2, strlen(scriptObject2), 0); assert(r >= 0); r = engine->Build("object2"); assert(r >= 0); r = engine->BindAllImportedFunctions("object2"); assert(r >= 0); // Object1: Set the factory, run the test, and allocate the first object printf("Stage 1:\n"); SetFactory(engine, "object1", "Object1Factory@"); r = engine->ExecuteString("test", "Test1()"); assert(r >= 0); r = engine->ExecuteString("test", "@object1 = @objectFactory.create()"); assert(r >= 0); // Object2: Set the factory, run the test, and allocate the first object printf("\nStage 2:\n"); SetFactory(engine, "object2", "Object2Factory"); r = engine->ExecuteString("test", "Test1()"); assert(r >= 0); r = engine->ExecuteString("test", "@object2 = @objectFactory.create()"); assert(r >= 0); // Object1: Set back to the first object type and create the last object. printf("\nStage 3:\n"); SetFactory(engine, "object1", "Object1Factory"); r = engine->ExecuteString("test", "Test1()"); assert(r >= 0); r = engine->ExecuteString("test", "@object3 = @objectFactory.create()"); assert(r >= 0); // Test all objects printf("\nStage 4:\n"); r = engine->ExecuteString("test", "DoTest(@object1)"); assert(r >= 0); r = engine->ExecuteString("test", "DoTest(@object2)"); assert(r >= 0); r = engine->ExecuteString("test", "DoTest(@object3)"); assert(r >= 0); // If the factory was created release it if(NULL != objectFactory) { objectFactory->Release(); objectFactory = NULL; } engine->Release(); return (r < 0);}
Quote:
Original post by RsblsbShawn
I wonder what I'd have to "donate" in order for that feature to get implemented =)
I refuse to be "bribed" into implementing features that I don't think have a place in AngelScript. The only way I can be convinced to implement a feature is to show me that it will make AngelScript better for the majority.
However, I already have plans to make AngelScript more dynamic, it should for example be possible to "inject" new functions into an already compiled module. The intention is to allow modifiable scripts such as what you're describing. I can't say when I'll be able to implement this though.
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
Recommended Tutorials
Advertisement