🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Some implementation questions

Started by
3 comments, last by WitchLord 5 years, 9 months ago

Sorry for the lousy title, but I don't really want to post half a dozen separate threads. Hope that's ok!

I'm implementing an angelcode interface to our software. I'm generally very happy with both the API and language itself, but I still have some questions.

1. Is there a way to pass a C++ lambda to RegisterObjectBehaviour? I find myself writing numerous one-liners when existing functions don't _quite_ match what I want to expose to the script. A lambda would save a bit of code.

2. Is it possible to create a script function that takes something like an initializer_list (from C++)? I have some cases where being able to pass any number of arguments (all of the same type) would be very useful.

3. I have asEP_USE_CHARACTER_LITERALS set to true. However, the engine still accepts multiple characters between single quotes. Is this intended behaviour?

4. Am I correct in assuming that DiscardModule also changes the internals of the engine, and should therefore also only be called from one thread? Or at least be protected by a mutex?

5. I'm still a little unclear on how often I should be collecting garbage. When I call GetGCStatistics() all five parameters always return 0, even though quite a bit of script activity has been going on by then. Our use case involves running scripts for up to six months at a time, so I'd like to make sure we do the garbage collection correctly and not run out of memory halfway through.

Thanks in advance!

Advertisement

1.

C++ lambda functions should work just fine. They are ordinary functions, except that they are defined inline in expressions.

2.

The closest you can get is to use the array or dictionary (or your own container types). Example:

void func(dictionary @args) {}

func({{'arg1', 1}, {'arg2', 'string'}, {'arg3', 3.14}});

3.

Yes, this allows you to define unicode characters that would require multiple bytes.

4.

Unless you have different threads that uses and that discards the same module you shouldn't need to do anything particular. Internally the engine has mutexes to protect the internal data structure.

Please let me know if you identify anything that is not properly protected in the code.

5.

If all five arguments returned by GetGCStatistics are always 0, your scripts do not create any garbage at all. That could happen if your scripts do not work with any reference types.

Unless you've turned off the automatic GC collection, the engine will also call the GarbageCollect routines from time to time. Though it obviously won't be finetuned for all cases, so having the application call GarbageCollect explicitly may be more efficient in some cases.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

On 9/1/2018 at 7:29 PM, WitchLord said:

1.

C++ lambda functions should work just fine. They are ordinary functions, except that they are defined inline in expressions.

I'm having a hard time making this work. Is this the correct approach?

    Engine.RegisterObjectMethod ("foo", "void bar()", asFUNCTION ([] (foo &Foo) { Foo.Bar (); }), asCALL_CDECL_OBJFIRST);

...because this doesn't compile, with error C2440: '<function-style-cast>': cannot convert from 'T' to 'size_t'

 

On 9/1/2018 at 7:29 PM, WitchLord said:

2.

The closest you can get is to use the array or dictionary (or your own container types). Example:

void func(dictionary @args) {}

func({{'arg1', 1}, {'arg2', 'string'}, {'arg3', 3.14}});

Ok. I was hoping there was a way I could use those {repeat} statements in normal functions as well ?

 

On 9/1/2018 at 7:29 PM, WitchLord said:

4.

Unless you have different threads that uses and that discards the same module you shouldn't need to do anything particular. Internally the engine has mutexes to protect the internal data structure.

Please let me know if you identify anything that is not properly protected in the code.

I think I understand the problem: both threads are accessing the string factory, and that one is marked as "not thread safe". Sorry - it took me a while to notice that.

I now have mutex protection around the discard call, so it isn't a problem anymore.

 

On 9/1/2018 at 7:29 PM, WitchLord said:

5.

If all five arguments returned by GetGCStatistics are always 0, your scripts do not create any garbage at all. That could happen if your scripts do not work with any reference types.

Unless you've turned off the automatic GC collection, the engine will also call the GarbageCollect routines from time to time. Though it obviously won't be finetuned for all cases, so having the application call GarbageCollect explicitly may be more efficient in some cases.

Odd: I have plenty of reference types around. Is it object creation that creates garbage, or more specifically creating cycles between objects that does?

Anyway, thanks for the help!

 

Before passing the lambda function to asFUNCTION, try casting it to a function with a known signature.

Reference types with asOBJ_GC notiifies the garbage collector of their existance upon creation. Without asOBJ_GC the type cannot form circular reference thus won't be monitored by the GC.

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