Home   Help Search Login Register  

Author Topic: Timer based on a variable  (Read 1921 times)

0 Members and 1 Guest are viewing this topic.

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
Timer based on a variable
« on: 27 May 2010, 11:48:35 »
So, before I test this and probably be disappointed I thought I would share this and get input to what mistakes might take place with this script.

Function:
-I have 3 fuel trucks that can be used as resupply trucks for FOBs. Depending on the amount of trucks present the timer should be effected for proper resupply time. The more trucks present, the less time it should take to resupply. obj1_trucks is defined in a game logic in mission as 0. Then depending on presence of trucks in triggers a value of 1 is added.

-OBJ1_Completed is the condition for the objective to check off.

Possible problem:
- Since my obj1_trucks is set by a game logic will the value reset with a JIP or are game logics only run on the server?
- Is my waitUntil coded correctly?


Code: [Select]
obj1_timer = 0;
obj1_end_timer = 0;
obj1_current_time = time;


if (obj1_trucks == 1)then
{
obj1_timer = 120;
};

if (obj1_trucks == 2)then
{
obj1_timer = 70;
};

if (obj1_trucks == 3)then
{
obj1_timer = 55;
};

obj1_end_timer = obj1_current_time + obj1_timer;

waitUntil{time >= obj1_end_timer};

obj1COMPLETED = true;
TS3 IP: tor.zebgames.com:9992

Offline Loyalguard

  • Former Staff
  • ****
Re: Timer based on a variable
« Reply #1 on: 27 May 2010, 13:30:31 »
If obj1_trucks is defined in the init line of the game logic then it will reset the value whenever someone JIPs, but just on their machine.  Instead of declaring it outright, you could use setVariable/getVariable to update the count.  Example assuming obj1_trucks is name of the logic.

Code: [Select]
private ["_trucks"];

// Get the current num of trucks.
_trucks = obj1_trucks getVariable "trucks";

// Add a truck.
_trucks = trucks + 1;

// Update the number of trucks for all clients.
objectName setVariable ["trucks", _trucks, true];

The waitUntil looks good as far as I can tell.  Is there any chance that another truck could show up during resupply.  If so, the code above won't evaluate that once it gets to the waitUntil....maybe not a big deal, just wanted to point it out.

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
Re: Timer based on a variable
« Reply #2 on: 28 May 2010, 04:48:16 »
I wont have a trigger fire off for the script to work for about 40 seconds this way the trucks should all be lined up. It is a convoy mission, MP style. So, that should take care of that problem.

I know about set and get methods from java... but can you explain what is going on with

private ["_trucks"]

is that setting a private variable?

just can u go into detail with that whole thing u gave me... walk me though it please.
TS3 IP: tor.zebgames.com:9992

Offline Loyalguard

  • Former Staff
  • ****
Re: Timer based on a variable
« Reply #3 on: 28 May 2010, 10:46:53 »
It is very late here, so I hope this makes sense...Using the private command ensures that that a variable is available throughout the entire scope/variable space of the script.  Variables that are initialized inside a control structure (if, forEach, while, etc.) are only available inside that control structure.  Example:


Code: [Select]
if (true) do
{
     _var = 1;
};

hint format ["var = %1", _var];

In the above example, the hint will return nothing because _var is not available outside the if block.  To make it available you have to 1) initialize it before the control structure OR 2) use the private command:

Code: [Select]
_var = 0;

if (true) do
{
     _var = 1;
};

hint format ["var = %1", _var]; // Will return 1.

OR

Code: [Select]
private ["_var"];

if (true) do
{
     _var = 1;
};

hint format ["var = %1", _var]; // Will return 1.

I know this doesn't seem overly relevant to your current script, but it is good practice to always begin your scripts with the private command to make sure that local variables will always be available (especially if there are times you don't want to actually initialize them right away) even if you aren't using inner scopes/control structures (because you might add one later!)...I have spent many a hour tracking down bugs down to failure to use the private command.  Yes, there are times you don't want them available outside of a specific control structure, but those are fairly rare.

So on to setVariable/getVariable.   These commands allow us to store/retrieve a value to/from the variable space of an object (player, vehicle, game logic, etc.).  So just like _var = 1 stores the value of _var to the variable space of the script you are running, player setVariable ["var", 1]; stores the variable to the object.  So this value is like a global variable, that is, it is available wherever the object is available.  So, using your scenario for example:

Code: [Select]
obj1_trucks =1; // Where obj1_trucks is a global variable.
...and...

Code: [Select]
obj1 setVariable ["trucks", 1]; // Where obj1 is the name of a game logic.
...Achieve the same result, store a value of 1 to a global variable space.

So, why use setVariable instead of global variables?  It really is a preference call, but there are times when one might be more useful than another.  For example, say you had a group of 5 units and you wanted to store their "moods".  You could do this one of two ways:

Code: [Select]
// Create GVs to store "moods" for the units in group1.

unit1Mood = "happy";
unit2Mood = "happy";
unit3Mood = "happy";
unit4Mood = "happy";
unit5Mood = "happy";

OR

Code: [Select]
// Use setVariable to create "moods".

{_x setVariable ["mood", "happy"]} forEach units group1;

In this case, I think the setVariable method is far easier.

You can also use setVariable like a public variable as well.  You just add a boolean to the parameters array.  Example:

Code: [Select]
obj1_trucks =1; // Where obj1_trucks is a global variable.
publicVariable "obj1_trucks";

...and...

Code: [Select]
obj1 setVariable ["trucks", 1, true]; // Where obj1 is the name of a game logic.
...do the same thing (for JIP as well).

I hope that clarifies things a little...do you need to use setVariable, no, but I wanted to make sure you were aware of the option.



Offline F2kSel

  • Members
  • *
Re: Timer based on a variable
« Reply #4 on: 28 May 2010, 11:52:48 »
Great post Loyalguard, I always meant to get around to finding out what private was all about.

Offline Ironman

  • Former Staff
  • ****
    • {GSF} Home Page
Re: Timer based on a variable
« Reply #5 on: 28 May 2010, 12:42:42 »
thanks for the quick reply, I will re-read it when I am not shit tired.

--------------edit------------------------

So all private does is declare the variable but doesn't assign it any value until in the control structure.

I know the crap about the scope and what not. Do the set and get methods do anything to help the JIP problem?


Just thought, wouldn't throwing an if(isServer) solve the JIP problem?
« Last Edit: 28 May 2010, 23:02:52 by Ironman »
TS3 IP: tor.zebgames.com:9992