Jump to content
kresjah

Monitoring an object variable

Recommended Posts

Long story short, I am trying to monitor a variable I have set on an object (via setVariable). I want to trigger certain events based on the value of said variable. In my mind, the ideal scenario would have an event handler trigger on changes to the variable.

 

The entire script is supposed to be run serverside. I couldn't find any event handlers which reacted to variable changes on an object, apart from addPublicVariableEventHandler. It is close to what I'm looking for, but it only acts on public variables (which is unnecessary if the script runs mainly serverside) and it seems to only trigger on machines which the variable is broadcast too, so it wouldn't fire on the server itself when it changes the variable.

 

Whilst I'm interested in a general solution to this as I'm sure to run into similar issues with future plans as well, the context is the disabling and enabling of a FSM state machine. When an "enabled" variable of an object change from true to false, the state machine will exit by its own means. However, when changing from false to true, the FSM would need to be restarted.

 

Currently the solution is to use a while-loop with sleep to check up on objects whose "enabled" variable is set to false. I'm curious as to what other potential solutions exist. Could anyone shed a light on this?

Share this post


Link to post
Share on other sites

Spawning a while loop would work, and is simple and effective. If you just have a few objects that you're tracking, you can probably even stop there, honestly. However if you have more than "just a few", you're better off doing another way because the while loops can run faster than once per frame and since setVariable can only change a value once per frame/every few frames (I think I read that on the wiki a long time ago) there's no point in using all that processing power. It may be better to use a stacked onEachFrame event handler with a global variable (array containing a list of the objects you want to track)

["RunFSM", "onEachFrame",
{
	{
		if (_x getVariable ["myVariable", false]) then
		{
			execFSM "MyCustomFSM.fsm";
		}
	} forEach myObjectsArray;
}, nil] call BIS_fnc_addStackedEventHandler;

Every object that you want to track should be added to your global array upon creation.

_obj = createVehicle [blah,blah,blah,blah,blah];
myObjectsArray pushBack _obj;

I've never worked with FSM's but you might also be able to achieve the same thing by creating some kind of "idle" state that simply checks if the variable is true. Below code assumes that you have execFSM with the unit itself as the only argument

waitUntil {(_this getVariable ["myVariable", false])};
//go back to normal state

Hope that helps

  • Like 1

Share this post


Link to post
Share on other sites

Using a stacked event handler never occured to me. That's something I need to keep in mind.

 

The reason I considered a while-loop was as mentioned so that I could add a sleep. The main reason for exiting the FSM when the variable is set to disabled is to reduce the workload. Instead of being checked every frame (or similar) I can probably get away with a 1 second sleep without it being very noticable.

 

I did find BIS_fnc_runLater. Any experience with it? Was wondering how it compared vs. the while+sleep solution. I would presume that the while+sleep would be less taxing based on its simplicity, but I have to wonder: What about using the runLater with a 1 second delay, with its code set to check the list of disabled object and do another 1 second runLater (essentially creating a loop)?

Share this post


Link to post
Share on other sites

Um, I haven't seen much use of it, but I think....I reiterate...I think this may be suitable for this situation as well, but I'm not 100% on how to use it (cause I've never messed with it):

 

https://community.bistudio.com/wiki/BIS_fnc_addScriptedEventHandler

 

Larrow has a relatively good example to work from in this thread.

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

×