Changing the content of global variables from C++

Started by
3 comments, last by WitchLord 2 months, 1 week ago

Hello everyone!

I need to set value of variable declared in script from C++ side.

The declaration is:

class MyClass
{
   bool a;
   int b;
   float c;
};

MyClass asObj1;

There is a bit complex way of iterating object's properties and setting each manually. That's tough.

I need simpler way, possibly using copy constructor or operator.
Reading of this forum and documentation gives me two possible functions:

int asIScriptEngine::AssignScriptObject

And using asIScriptObject:

asIScriptModule::GetAddressOfGlobalVar

But i can't find exact solution.

Let the power of C++ be with you.

Advertisement

Assuming you have an already instantiated copy of the object in objSrc you can do an assignment to the script declared variable with the following steps:

asIScriptObject *objDst = (asIScriptObject*)mod→GetAddressOfGlobalVar(mod→GetGlobalVarIndexByName("asObj1");

engine→AssignScriptObject(objDst, objSrc, objDst→GetObjectType());

Of course, you want to ensure you do the proper error handling, and make sure the type of objects are matching as well.

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 it correct that objSrc has type asIScriptObject too?

Let the power of C++ be with you.

Yes, it must be the same script class 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

This topic is closed to new replies.

Advertisement