Jump to content
  • Advertisement
Sign in to follow this  

Object References and parameter Declaration Conundrum

This topic is 2915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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),

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),

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.

Share this post

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


Share this post

Link to post
Share on other sites
Thanks SiCrane and Andreas this clears everything up and I can continue using angelscript!

Many Thanks!

Share this post

Link to post
Share on other sites
Sign in to follow this  

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