Passing reference as parameter

Started by
4 comments, last by WitchLord 11 years, 2 months ago

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 !

Advertisement

SetArgObject works just fine.

Please post some code.

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() :(

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 - Tower - free puzzle game

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

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 - Tower - free puzzle game

This topic is closed to new replies.

Advertisement