🎉 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!

Register a reference type

Started by
3 comments, last by B_old 15 years, 6 months ago
I have a question regarding registering reference types. I want to register a type that cannot be instantiated by the script. I thought all I had to do is to omit the factory stuff. But then AngelScripts complains that there are no behaviors.

engine->RegisterObjectType("Object", 0, asOBJ_REF); //Ok, but not enough?!

What am I doing wrong? Just so that you know what I want to accomplish: I want to pass a reference of an object to the script, call some methods of that object and nothing more. I don't need to create new objects from the script. And if I had to I would call a function in my program that does so. (Do I have to care about the reference counting? I don't quite understand where that is supposed to happen.)
Advertisement
If you want to allow the script to store references to the objects it receives from the application, then you must register the ADDREF and RELEASE behaviours. Though if you don't want to keep track of the reference count, the behaviour function can be a simple dummy function, but then you'll need to make sure that the scripts are destroyed before the actual object.

If you don't want to allow the script to store the reference it receives from the application, then you can register the type with asOBJ_REF | asOBJ_NOHANDLE. This will prevent the scripts from declaring variables of this type, but the application can register a global property that the script can access. The application can also return a reference to the type from a function.

Registering a reference type

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

Hi,
I followed your advice and registered the ADDREF/RELEASE behavior with dummy functions and now it is working.
I am very pleased with the results. Its going to be a lot of fun to script some stuff for my game.
But there is one thing I wonder. By not implementing the reference counting, have I created a memory leak?
I will never instantiate an Object in the script and only use references passed from the application. Is that OK? Destroying the script (by releasing the context I presume), before I delete my objects, is no problem.

Last I would like to thank you again for the cool engine and especially for the kind support you provide. It makes using AngelScript a pleasure. :)
No, you haven't created any memory leak by using a dummy function for the AddRef/Release behaviours. The only consequence of this is that your game won't know if the script stores a reference to the game object in some global variable. This may cause dangling pointers, unless your game engine makes sure the object instance stays alive at least as long as the script.

The script is not contained in the context, so releasing the context is not enough to destroy the script. To destroy the script you need to discard the script module in the script engine.

Thanks for the compliments. I'm glad to hear you like AngelScript.

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

Quote: Original post by WitchLord
This may cause dangling pointers, unless your game engine makes sure the object instance stays alive at least as long as the script.

Should not be a problem for me then.

Quote: Original post by WitchLord
The script is not contained in the context, so releasing the context is not enough to destroy the script. To destroy the script you need to discard the script module in the script engine.

I see. In that case I gain nothing from releasing the context? Instead I discard the script module... very well.

Thanks for the info!

This topic is closed to new replies.

Advertisement