Skip to main content
GameDev.net gamedev.net
🔒 Locked

Passing reference as parameter

Started by maximegriot Feb 11, 2013 at 1:00 AM 4 replies 2.6k views
Original Post
maximegriot
maximegriot

Hello,

I am trying to pass an exiting object as a reference to a function but I can't get it to work...

I have tried NPC@ and NPC& as type and nothing seems to work...

I use SetArgObject and give it a pointer to my object (I don't want to copy it because I want to be able to call functions on that object).

If I put the object in a global variable it works just fine...

Could you give me some insights ?

Thanks !

saejox
saejox

SetArgObject works just fine.

Please post some code.

maximegriot
maximegriot

Here it is :


NPC npc;
context->Prepare(func);
context->SetArgObject(0, &npc);
context->Execute();
context->Unprepare();

And the AS code :


void OnNew(NPC @npc)
{
print(npc.GetName());
}

Note that it does work if I set mt NPC to a global parameter so there is nothing wrong with my GetName() :(

WitchLord
WitchLord

The code you showed should work. For parameters by handle, like in your case, SetArgObject will call Addref on the object and put the pointer on the stack.

Is any of the calls to context's methods returning an error? i.e. a negative value?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
maximegriot
maximegriot

The thing being I don't have ref counting on these objects (my application manages everything).

And no no negative value what's so ever :s

My bad, my variadic template call was actually doing a hard copy of the object and passing a stack address that had been deleted.

Here is my code if someone wants an easy way to start : https://github.com/yamashi/SkyrimOnline/blob/master/Client/Src/Server/Src/Game/ScriptEngine.hpp

WitchLord
WitchLord

I haven't begun working with C++11 yet, but I must say the template variadic arguments does look neat.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.