Jump to content
Sign in to follow this  
galzohar

pointers to pointers

Recommended Posts

I've been looking for a way to pass a pointer to an object to a script in a way that will allow the script to change that pointer. Will doing [objPointer] execVM "script.sqf" then allow me to change objPointer by using "_this select 0" = otherObjPointer" or is "_this select 0" unchangeable, and if it is, is there any other way to do this?

I'm assuming that passing the variable normally and not in an array will pass it by value and not by reference?

Share this post


Link to post
Share on other sites

If the passed var is global, just assign new value to that directly instead of the local _this.

objPointer execvm "script.sqf"

script.sqf:

objPointer = otherObjPointer

Share this post


Link to post
Share on other sites

_this select 0 ALWAYS points at the first parameter the script has been executed with, and there's no way to change that.

What is it you want to do that'd require such an action to be done to start with? What is it you are trying to achieve with the pointer-changing?

Share this post


Link to post
Share on other sites

I'm pretty sure objects are always passed by reference (there is no way that I'm aware of to pass objects by value). So for example:

_player = player;
_newPlayer = _player;
_player = objNull;
if(_newPlayer == player)then{hint "_newPlayer still references player"};

And as the code implies, _newPlayer would still reference player.

Your example is not a good one, because "_this select 0" is not a pointer in itself, but rather accesses a member of the array pointed to by _this. Also, _this is a special reserved variable. I'm not sure if you can reassign it to point to something else.

I think everything else (numbers, strings, arrays, etc..) is passed by value though. So:

// this will loop forever
while((_this select 0) == player)do{
_params = _this;
_params set [0,objNull];
// will return objNull
if(isNull (_params select 0))then{hint "Param 0 is null"};
};

The message "Param 0 is null" would also keep being displayed indefinately because the loop will never break.

There are other primitives too that might be passed by reference (ex: I assume script handles are passed by reference). I don't have a complete list.

Hope that helps you understand.

Edited by Big Dawg KS

Share this post


Link to post
Share on other sites

Obviously passing an object passes a pointer to that object and not the actual object, but I assume that the *pointer* is passed by value, and I want to be able to change it. Doesn't seem like you can, though, from what you're saying.

So basically the only way to have a called script change something that isn't global is by having it return it?

Share this post


Link to post
Share on other sites

Well, I'm not sure I would even say the concept of pointers even applies to ArmA 2 scripting.

So basically the only way to have a called script change something that isn't global is by having it return it?

The real question here is what that something is. If it's a variable than it's more related to variable scope than pointers. You CAN change variables in a called script if they are within the same scope. For example:

_string = "Hello";

[] call {_string = "Goodbye"};

// prints "Goodbye world!"
hint format ["%1 world!",_string];

Please specify what you're trying to do though... I think your generalizations are leading to confusion.

Share this post


Link to post
Share on other sites

Well for the specific problem I already worked around it by having everything in the same script, but at first I wanted to have separate sripts - one that determines when a vehicle should respawn and another that will actually respawn it. Placing the actual respawn commands in the same script (or having the respawned vehicle get returned rather than modified) works around this issue here, but I wouldn't be surprised if this comes up again in future scripting.

Anyway, for now I don't have a specific problem that needs it because I found other ways to solve it, but I had used it in other programming (many windows functions ask for a pointer to some generic struct where it places the results of the operation so you can access them after the function completes, rather than returning it). Again this can usually (if not always) be worked around, but sometimes it makes life easier to use a pointer to a pointer.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×