Jump to content
kavoven

Scripting in Multiplayer - Sleep and Hint

Recommended Posts

Hi,

 

it's been literally 15 years since I scripted the last time in OFP so my knowledge is a little but rusty.  As far as I can see, you now have to use spawn when you want to execude looped scripts. The aim is to create an area in which a drone constantly loses fuel until it leaves the area again. The script is executed by a trigger, that also sets fuelReduce to true (and false when leaving). It should be running on the server only (I checked the server only box in the trigger).

[]spawn {
		while(fuelReduce == true) do {
			_droneFuel = fuel drone1;
			_droneFuel = _droneFuel - 0.01;
			drone1 setFuel = _droneFuel;
			parseText format ["SOMETEXT HERE: %1", _droneFuel] remoteExec ["hintSilent"];
	};
};

However, when using sleep within a while do loop, I get the generic error message. I do not get this error when I execute something comparable via the init file (e.g. for moving markers). So I guess the problem is when I execute the script (fuelReduce = true; drone1 exec "droneFuelReduce.sqf")? By the way - I was used to executing sqs files but I recon that the right way to do it now is using sqf?

 

Moreover, the \n command does not seem to work in the parseText format function. Is there any alternative for broadcasting texts that contains %1 and the respective variable to all clients? I was kind of confused which exact commands I have to use when reading the wiki.

Share this post


Link to post
Share on other sites

Okay, your general problem is, that you call the script bei "exec" which is used to call sqs code. The error you get is simply because sleep command is not defined in sqs (there it is ~123). When you use execVM, your script will be interpreted as sqf code and should run fine. Also you can get rid of the [] spawn {some code} as execVM is already loading and executing the file in a seperate sqf thread aka scheduled environment. exec and execVM are loading the script from file everytime and file access is very slow. In A3 you would normally load the script once as a function (myFunctionName = compile preprocessFileLineNumbers "dronefuel.sqf") and than just call or spawn the function.

 

But when using trigger, there is a more elegant option to solve your problem (atleast in my opinion):

 

First create the trigger over the area you want to encompass and set it to activation "Everyone", "Present" and "Multiple". Than, in "On Activation" you put your code like this:

(thisList select {typeof _x == "DroneClassName"}) apply {_x setfuel (fuel _x - 0.01);(format ["SOMETEXT HERE: %1", fuel _x]) remoteExec ["hintSilent"];}

Explanation:

thisList is an array of all the vehicles that satisfy the trigger condition (so in this case everybody in it).

select {typeof _x == "DroneClassName"} reduces the array of all elements, where the expression is true, so only objects with a specific classname (you need to find your drones classname in this case).

apply expression is called for every element in the array on the left side. In the expression _x is a variable containing the element selected from the array.

 

The trigger should be fired ~0.5 times per second, so you maybe need to tune the fuel value a bit. Oh, and for "server only" you need to set the "Condition" to "this && isServer" or does 3DEN has an option for that nowadays?

 

This is a more elegant solution without calling scripts etc. It is definitly not the best. If I would need to solve this problem, I would most likely put the stuff in a function, but that is maybe too much for simple answer here in the forum.

Share this post


Link to post
Share on other sites

Welcome back! :rthumb:

 

Correct format is while { .... } do { .... };

 

Below should work 

 

[] spawn { 
	while {fuelReduce} do { 
		private _droneFuel = fuel drone1; 
		drone1 setFuel (_droneFuel - 0.01); 
		parseText format ["SOMETEXT HERE: %1", fuel drone1] remoteExec ["hintSilent"]; 
		sleep 1; 
	}; 
};

 

Share this post


Link to post
Share on other sites

Thanks for the explanations, that helps a lot! :)

 

Ah, and the \n not working error was because of the "parseText" in front of the format. 

Share this post


Link to post
Share on other sites
5 hours ago, kavoven said:

Ah, and the \n not working error was because of the "parseText" in front of the format. 

 

Jup, parseText expects HTML like formatting while normal hint/sidechat/etc supports basic ASCII like \n

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

×