Jump to content
Sign in to follow this  
bangabob

Run code when variable changes

Recommended Posts

Hi guys. A bit of a logic puzzle here. I spent the late hours of last night trying to figure this out to no avail.

I have a while loop that runs through a IF "triggerActivated trig" condition. Everytime the loop runs it will run the code inside the condition because the trigger is deactivated (ELSE). So my code will be ran every 0.5 seconds.

My code basically calls a function that updates markers properties. Therefore should only be run when the marker is changed. (Triggered by my trigger).

- I only want the code to run ONCE while the trigger is activated.

- I only want the code to run ONCE while the trigger is deactivated.

And so on.

PRIVATE ["_var"];
while {true} do
{
/* THIS IF ELSE WILL RUN EVERY 0.5 SECONDS. I DONT WANT TO CALL MY FUNCTION EVERY 0.5 SECONDS */
if (triggerActivated _trigger)
        then {
       _var=true;
     // 0= [] execVM "updateMarker.sqf";
            }else{
            _var=false;
          //0= [] execVM "updateMarker.sqf";
                   };

/* I WANT THIS CODE TO RUN WHEN THE _var VARIABLE IS CHANGED */
if (_var /*SOME MAGIC HERE*/) 
                  then 
                  {
            /* CALL MY FUNCTION ONCE */
            0= [] execVM "updateMarker.sqf";
                 };
sleep 0.5;
};  

Thanks for reading

Edited by BangaBob

Share this post


Link to post
Share on other sites

I suppose it's related to your EOS and COS, isn't it? If so, triggers are mandatory, yes?

Because if not, this situation is the reason why I don't use triggers, make the function call depending on distances of an array of objects and you will be able to fine tune whatever + control everything.

Like:

while {true} do
if (_marker in activatedarrayofmarkers) then
{
do something

waitUntil {sleep 0.5; not (_marker in activatedarrayofmarkers};

do something else
}
sleep 0.5;
};

Share this post


Link to post
Share on other sites
I suppose it's related to your EOS and COS, isn't it? If so, triggers are mandatory, yes?

Because if not, this situation is the reason why I don't use triggers, make the function call depending on distances of an array of objects and you will be able to fine tune whatever + control everything.

Like:

while {true} do
if (_marker in activatedarrayofmarkers) then
{
do something

waitUntil {sleep 0.5; not (_marker in activatedarrayofmarkers};

do something else
}
sleep 0.5;
};

I find triggers are reliable and efficient but most importantly they seem to work on dedicated servers.

But there has to be a relatively simple way of detecting when a variable changes.

Something like my code below, which monitors a variable and passes the condition when it changes.

PRIVATE ["_var"];
while {true} do
{
/* THIS IF ELSE WILL RUN EVERY 0.5 SECONDS. I DONT WANT TO CALL MY FUNCTION EVERY 0.5 SECONDS */
if (triggerActivated _trigger)
        then {
       _var=true;
     // 0= [] execVM "updateMarker.sqf";
            }else{
            _var=false;
          //0= [] execVM "updateMarker.sqf";
                   };

/* I WANT THIS CODE TO RUN WHEN THE _var VARIABLE IS CHANGED */
if (_var /*SOME MAGIC HERE*/) 
                  then 
                  {
            /* CALL MY FUNCTION ONCE */
            0= [] execVM "updateMarker.sqf";
                 };
sleep 0.5;
};

Edited by BangaBob

Share this post


Link to post
Share on other sites

Something like this?

while {true} do {
if (_var == true) then { // When _var is true we start to read the script
	dosomething;
	waitUntil {_var == false}; // We wait until _var changes to continue reading the script
	dosomething2;
};
sleep 0.5; // Now we'll wait until _var is true on the beggining of the script
};

Share this post


Link to post
Share on other sites

Your code does not really make sense BangaBob as in your waiting for something to be true, to set something true, that when true does something. I mean it works but its two steps just for when something was true anyway. :/

You could always do

while {true} do {
_triggerStaus = triggerActivated myTrigger;
switch (_triggerStaus) do {
	case true : {
		hint "activeated";
		waitUntil {!triggerActivated myTrigger};
	};
	case false : {
		hint "deactiveated";
		waitUntil {triggerActivated myTrigger};
	};
};
sleep 0.5;
};

but again this over doing it as this is exactly what the triggerStatements are for... as in they do something only when the triggers condition changes

myTrigger setTriggerStatements ["this","hint 'Activated'","hint 'DeActivated'"];

but maybe there is something your not showing us that changes what your trying to do the way you are?.

Edited by Larrow

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  

×