Advertisement

Statics Angelscript

Started by March 25, 2019 12:22 PM
3 comments, last by WitchLord 5 years, 7 months ago

Hello,

I use Angelscript as my go-to scripts in my tools and mini game-engines and I was looking for a way to register statics constants and found this very old post (Angelscript and static method).

For example, if we have a Vector3 class, wouldn't that be nice to Register a Vector3.Up, static constant?

If I understand correctly, the "work around" is to just use globals. So I can ask my user to use the "Up" global variable or, "Vector3Up" or something.

I don't mind doing that, but is there a way to "fake" that it's a static? I'm really just asking for looks.

I would like something like that:


mScriptEngine->RegisterObjectType("Vector3");
mScriptEngine->RegisterGlobalProperty("Vector3", "Up", &SomeGlobalVariable);

And then, in scripts, the user type Vector3.Up to get the global pointer?

At least in the AngelScript version before the latest one, you could register global functions under a namespace that is the same name as the class name (not sure if that changed or if there are plans to not allow this in the future).

So yeah you probably can still register a global function / property "Up" under namespace "Vector3" and this way it will be accessible in scripts with "Vector3::Up"

(Fingers crossed this is not going away without an official static variable replacement)
If static members do get introduced, I imagine the in-script syntax would be the same and wouldn't require script changes to transition.

Advertisement

MrFloat, Thank you very much. This is exactly what I was looking for and never thought about the namespaces.

I just tried it that way:


int32_t tResult = 0;

// save the current namespace:  
const char* tNamespace = mScriptEngine->GetDefaultNamespace();

// set the namespace using the same name has the object:
tResult = mScriptEngine->SetDefaultNamespace("Vector3");
assert(tResult >= 0);

// add a global property to that namespace:
tResult = mScriptEngine->RegisterGlobalProperty("Up", ptr);
assert(tResult >= 0);

// get back to previous namespace:
tResult = mScriptEngine->SetDefaultNamespace(tNamespace);
assert(tResult >= 0);

And voila, I can use Vector3::Up in the scripts.

P.S. : I'm using version 2.33.0

On 3/25/2019 at 9:43 AM, MrFloat said:

(Fingers crossed this is not going away without an official static variable replacement)

That is the plan. ?

Allowing namespaces with the same name as the classes was a shortcut to implementing true static members. When static members is implemented I'll deprecate the support for namespaces with the same nams as classes.

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