Jump to content
  • Advertisement
Edgyr

Scripting Statics Angelscript

Recommended Posts

Posted (edited)

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?

Edited by Edgyr

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!