Advertisement

Object References and parameter Declaration Conundrum

Started by October 28, 2011 02:33 AM
2 comments, last by Hatori 13 years, 1 month ago
Hello all,

First I will give a bit of overview before I ask my question.

In my engine objects CANNOT be instantiated from script. All this happens through C++ and the C++ side of the code manages the lifetime of objects. I have been reading the angelscript docs and it says that if I return an object from C++ to the script engine I have to Add a reference. Also
it says that if the script passes a handle to a C++ object I have to release it once I am done with it.

My question is if my AddReference and Release functions are just dummy routines that don't do anything (since the engine controls when objects are created and destroyed) Do I still need to follow what is described in the manual in regards to references?

Also if I don't follow these rules will this mess up the garbage collector in any way?

To my utter surprise I noticed that the scriptstdstring implementation does not have the asBEHAVE_ADDREF and asBEHAVE_RELEASE behaviors set. How is this possible? I thought that if you did not register these methods in a class they would fail to work and angelscript would crash. If I have a CHuman class can I get away without having these behaviors and still have angelscript behave properly?

Also when declaring routines I noticed that you need to declare parameters properly so they are passed by reference and not by value.

For instance:

// set game path animation
iResult = pcScriptEngine->RegisterObjectMethod ("CHuman",
"void setGamePathAnimation (const string &in strGamePath, const string &in strAnimation)",
asFUNCTION (setGamePathAnimation),
asCALL_CDECL_OBJLAST);


By using the const string &in these strings are passed in by reference. My question is if I return a CHuman class how do I declare it properly so that I can be assured that the object returned is a reference and NOT a copy. Here is the example:

// register the get human function
iResult = pcScriptEngine->RegisterGlobalFunction ("CHuman &getHuman (const string &in strName)",
asFUNCTION (getHuman),
asCALL_CDECL);


I want to ensure that a reference to the desired CHuman object is returned and NOT a copy.

Thanks for anyone that can help my on my myriad of questions.
scriptstdstring doesn't register addref and release behaviors because it's a value type.
Advertisement
If your application is managing the memory and you've registered the addref/release behaviours as dummy functions, then there is no need to call them.

The garbage collector won't be impacted by the fact that you've implemented the addref/release behaviours as dummies.


The fundamental difference betwen reference types and value types is: Reference types are allocated on the heap, and allow handles. Value types are allocated on the stack, and do not allow handles.

Value types can be passed by value to functions, though in the case of std::string it's a good idea to make the parameters as references in C++ to avoid unnecessary copies of the internal buffer.

References returned from a function refer to the actual object. No copies are made, so you can make a call or access members of the object correctly. However, you will not be able to forward or store that reference (unless the reference is to 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

Thanks SiCrane and Andreas this clears everything up and I can continue using angelscript!

Many Thanks!

This topic is closed to new replies.

Advertisement