Jump to content
Sign in to follow this  
SpitShine

variables, why won't they work?

Recommended Posts

tank = obj1

if (isserver) then {

tank setdir 20;

};

Why does tank return an undefined variable error in the above situation?

Share this post


Link to post
Share on other sites

if the error is not the syntax, then it depends on a few things.

if obj1 is not an object on the server already loaded in before script runs, then the script will not know what obj1 is.

if obj1 is an actual object on the server, and the program you are running runs after the mission file is instantiated, then its should identify obj1 and a global variable to the object you named. Therefore it should work (syntax ; aside).

additionally tank is a global variable - so why do you need to reference another global variable.

you should use _tank tank to save on memory when this particular script finishes.

cant you just use

if (isserver) then 
{
obj1 setdir 20;
};

Share this post


Link to post
Share on other sites

No can do, because tank is not a pre-placed unit, it is spawned by one script and then given further variables by another script.

But the later script returns tank as an undefined variable even if it spawns before the later script is run.

---------- Post added at 23:52 ---------- Previous post was at 23:42 ----------

Oh and guess what on another mission an actual honest to goodness pre-placed object is somehow an undefined variable when a script attempts to reference it

isn't that just grand?

Share this post


Link to post
Share on other sites

if (isServer) then {
    waitUntil {!isnil "obj1"};  //that way you make sure your _tank variable is defined and only defined as soon as obj1 is present.
    _tank = obj1;
    _tank setdir 20;
};

obj1 has to be the name of the object, not the classname.

alternatively you can try:

if (isServer) then {
    if (isNil "obj1") then {     //but make sure that obj1 will sooner or later be present.
         waitUntil {!isnil "obj1"};
         _tank = obj1;
         _tank setdir 20;
    };
};

Edited by Pergor

Share this post


Link to post
Share on other sites

if the script that spawns the tank is not on the same node as the script that also uses the tank variable, then you will need to pv "tank" or pv "obj1"

also code like

waituntil {! Isnil "Tank"};

could be used.

As far ans the undefined variable issues are concerned, you are doing something wrong, making a logical assumption that is incorrect.

Share this post


Link to post
Share on other sites

I am guessing here, but I think that Terox is saying that you need to pass the variable name for the tank into the public domain after it is created (otherwise, your script can't figure out the name of the dynamically created tank).

For instance...

MyMakeATankScript.sqf

//  gonna make a tank and let the world know...
myTank = "B_MBT_01_cannon_F" createVehicle (position player);
publicVariable ("myTank");

MyUseTheTankNameToPointItNorthEastish.sqf

if (isServer) then {
    waitUntil {!isnil "myTank"};  //that way you make sure your _tank variable is defined and only defined as soon as obj1 is present.
    _tank = myTank;
    _tank setdir 20;
};  

although they are right, you could just use myTank in the script. The only reason to cast it to another name is if you are passing it different tank names as a param and don't want a script per tank name.

DasClark

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  

×