Jump to content
ZU23

How to make a script loop forever

Recommended Posts

What do I write in the init to make a script loop forever?

Share this post


Link to post
Share on other sites

while {true} do {

//code

};

 

Edit: it needs to be run in a scheduled environment (execVM, spawn)

  • Like 2

Share this post


Link to post
Share on other sites

^ youre gonna have a bad time without a sleep in there also.

Share this post


Link to post
Share on other sites
1 hour ago, MKD3-FHI said:

^ youre gonna have a bad time without a sleep in there also.

 

Perhaps, but that'd be within the //code part.

 

And to further clarify my comment about the scheduled environment, if it's run in a non-scheduled environment (e.g. call) it'll only run for 10,000 iterations. After that it just stops, hence the execVM/spawn.

Share this post


Link to post
Share on other sites

Maybe to specify this even more for you case, you should put the infinite loop in a separate script (let's call loop.sqf) and spawn/execVM this from within your init.

// Unit init/init.sqf
_loopHandle = spawn "loop.sqf";

//loop.sqf
while {true} do {
  // Stuff here
};

 

  • Like 1

Share this post


Link to post
Share on other sites
On 27/05/2017 at 2:46 AM, beno_83au said:

while {true} do {

//code

};

 

Edit: it needs to be run in a scheduled environment (execVM, spawn)

 

Hi ! I'm trying to loop several animations in a mission I'm making.
Would your solution works for looping this : 

[]spawn {
officierONU switchMove "HubBriefing_loop";
sleep 10.00;
officierONU switchMove "HubBriefing_scratch";
sleep 10.00;
officierONU switchMove "HubBriefing_think";
sleep 10.00;
officierONU switchMove "HubBriefing_pointLeft";
sleep 10.00;
};

They are running one after one other correctly but when the "pointLeft" one is done the unit keeps looping the first animation.

Thanks in advance ! :wink_o:

Share this post


Link to post
Share on other sites
[]spawn {
	while {true} do {
		officierONU switchMove "HubBriefing_loop";
		sleep 10.00;
		officierONU switchMove "HubBriefing_scratch";
		sleep 10.00;
		officierONU switchMove "HubBriefing_think";
		sleep 10.00;
		officierONU switchMove "HubBriefing_pointLeft";
		sleep 10.00;
	};
};

This does work if you meant it that way. In your script there's no loop, therefore the last animation you called is running infinitely.

 

Spoiler

Just some own thought: If someone wants a more dynamic animation then this script would add some more randomness:


[]spawn {
	while {true} do {
		_moves = ["HubBriefing_loop", "HubBriefing_scratch", "HubBriefing_think", "HubBriefing_pointLeft"];
		_playMove = selectRandom _moves;
		officierONU switchMove _playMove;
		sleep (random 5 +10);
	};
};

 

 

Share this post


Link to post
Share on other sites

Thanks mate ! I was trying to do something like a script with randomness.

The way I understand the one you suggest, it will run infinitely the animations in a random order ? Is that how it works ?

 

 

18 hours ago, 7erra said:

sleep (random 5 +10);

Does this part means that the animation will change every 5 to 10sec on average ?

Share this post


Link to post
Share on other sites
[]spawn {
	while {true} do { // The true condition can be changed to any value that returns true. Eg you can have a variable my_case_trueFalse which can be set to false if you want to stop the loop
		_moves = ["HubBriefing_loop", "HubBriefing_scratch", "HubBriefing_think", "HubBriefing_pointLeft"]; 
		_playMove = selectRandom _moves; // select a random animation. chances are equal for every animation
		officierONU switchMove _playMove;
		sleep (random 5 +10); // waits at least 10 seconds and another 0-5 seconds. (so between 10 and 15 seconds)
	};
};

Here are some comments on my previous script to clarify. The loop runs infinitly, even after the unit dies (leads to some strange animations).

  • Like 1

Share this post


Link to post
Share on other sites
55 minutes ago, 7erra said:

The loop runs infinitly, even after the unit dies (leads to some strange animations)

 

It won't bother me for that mission (well, unless someone delibarately kill the unit). Is there a way to avoid this ? I'm still curious, I might need it for other missions

 

Thanks :wink_o:

Share this post


Link to post
Share on other sites
On 5/27/2017 at 0:39 PM, Mokka said:

Maybe to specify this even more for you case, you should put the infinite loop in a separate script (let's call loop.sqf) and spawn/execVM this from within your init.


// Unit init/init.sqf
_loopHandle = spawn "loop.sqf";

//loop.sqf
while {true} do {
  // Stuff here
};

 


1. you cannot spawn a script by file name

2. even if you could, you cannot use spawn without an argument 

Share this post


Link to post
Share on other sites
56 minutes ago, Mr_Sideburns said:

 

It won't bother me for that mission (well, unless someone delibarately kill the unit). Is there a way to avoid this ? I'm still curious, I might need it for other missions

 

Thanks :wink_o:

 

Just: while {alive officierONU} do { ..} instead of true; then add after this loop: officierONU switchMove "";

  • Like 1

Share this post


Link to post
Share on other sites
1 hour ago, pierremgi said:

Just: while {alive officierONU} do { ..} instead of true; then add after this loop: officierONU switchMove "";

 

Nice ! It looks so obvious sometimes when you see it :sigh:

Share this post


Link to post
Share on other sites
On 3/6/2017 at 6:13 PM, 7erra said:

[]spawn {
	while {true} do { // The true condition can be changed to any value that returns true. Eg you can have a variable my_case_trueFalse which can be set to false if you want to stop the loop
		_moves = ["HubBriefing_loop", "HubBriefing_scratch", "HubBriefing_think", "HubBriefing_pointLeft"]; 
		_playMove = selectRandom _moves; // select a random animation. chances are equal for every animation
		officierONU switchMove _playMove;
		sleep (random 5 +10); // waits at least 10 seconds and another 0-5 seconds. (so between 10 and 15 seconds)
	};
};

 

it's a great script. I just need to execute animations in order; not randomly but keeping the loop. Can you help me?

Share this post


Link to post
Share on other sites
officierONU switchMove "HubBriefing_loop";
officierONU addEventHandler ["AnimDone", {
	params ["_unit", "_anim"];
//	systemchat format ["Animation %1 ended",_anim];
	if (!alive _unit) exitWith {_unit removeAllEventHandlers "AnimDone"};
	switch (true) do {
		case (_anim == "HubBriefing_loop"): {_unit switchMove "HubBriefing_scratch"};
		case (_anim == "HubBriefing_scratch"): {_unit switchMove "HubBriefing_think"};
		case (_anim == "HubBriefing_think"): {_unit switchMove "HubBriefing_pointLeft"};
		case (_anim == "HubBriefing_pointLeft"): {_unit switchMove "HubBriefing_loop"};
		default {};
	};
}];

 

  • Thanks 2

Share this post


Link to post
Share on other sites
1 hour ago, davidoss said:

officierONU switchMove "HubBriefing_loop";
officierONU addEventHandler ["AnimDone", {
	params ["_unit", "_anim"];
//	systemchat format ["Animation %1 ended",_anim];
	if (!alive _unit) exitWith {_unit removeAllEventHandlers "AnimDone"};
	switch (true) do {
		case (_anim == "HubBriefing_loop"): {_unit switchMove "HubBriefing_scratch"};
		case (_anim == "HubBriefing_scratch"): {_unit switchMove "HubBriefing_think"};
		case (_anim == "HubBriefing_think"): {_unit switchMove "HubBriefing_pointLeft"};
		case (_anim == "HubBriefing_pointLeft"): {_unit switchMove "HubBriefing_loop"};
		default {};
	};
}];

 

It works, but for any reason it doesnt when i use this animations:

 


officierONU switchMove "Acts_JetsCrewaidR_idle";
officierONU addEventHandler ["AnimDone", {
    params ["_unit", "_anim"];
//  systemchat format ["Animation %1 ended",_anim];
    if (!alive _unit) exitWith {_unit removeAllEventHandlers "AnimDone"};
    switch (true) do {
        case (_anim == "Acts_JetsCrewaidR_idle"): {_unit switchMove "Acts_JetsCrewaidRCrouch_in_m"};
        case (_anim == "Acts_JetsCrewaidRCrouch_in_m"): {_unit switchMove "Acts_JetsCrewaidRCrouchThumbup_in_m"};
        case (_anim == "Acts_JetsCrewaidRCrouchThumbup_in_m"): {_unit switchMove "Acts_JetsCrewaidRCrouchThumbup_loop_m"};
        case (_anim == "Acts_JetsCrewaidRCrouchThumbup_loop_m"): {_unit switchMove "Acts_JetsCrewaidRCrouch_out_m"};
        default {};
       
};
}];

Share this post


Link to post
Share on other sites
officierONU switchMove "Acts_JetsCrewaidR_idle";
officierONU addEventHandler ["AnimDone", {
    params ["_unit", "_anim"];
//  systemchat format ["Animation %1 ended",_anim];
    if (!alive _unit) exitWith {_unit removeAllEventHandlers "AnimDone"};
    switch (true) do {
        case (_anim == "Acts_JetsCrewaidR_idle"): {_unit switchMove "Acts_JetsCrewaidR_idle_m"};
        case (_anim == "Acts_JetsCrewaidR_idle_m"): {_unit switchMove "Acts_JetsCrewaidRCrouch_in_m"};
        case (_anim == "Acts_JetsCrewaidRCrouch_in_m"): {_unit switchMove "Acts_JetsCrewaidRCrouchThumbup_in_m"};
        case (_anim == "Acts_JetsCrewaidRCrouchThumbup_in_m"): {_unit switchMove "Acts_JetsCrewaidRCrouchThumbup_loop_m"};
        case (_anim == "Acts_JetsCrewaidRCrouchThumbup_loop_m"): {_unit switchMove "Acts_JetsCrewaidRCrouchThumbup_out_m"};
        case (_anim == "Acts_JetsCrewaidRCrouchThumbup_out_m"): {_unit switchMove "Acts_JetsCrewaidRCrouch_out_m"};
        case (_anim == "Acts_JetsCrewaidRCrouch_out_m"): {_unit switchMove "Acts_JetsCrewaidR_idle_m"};
        case (_anim == "Acts_JetsCrewaidR_idle_m"): {_unit switchMove "Acts_JetsCrewaidR_idle"};

        default {};
       
};
}];

officierONU can not have weapons with this anim

  • Like 4
  • Thanks 1

Share this post


Link to post
Share on other sites
12 minutes ago, davidoss said:

officierONU can not have weapons with this anim

 

Ohh thanks a ton dude! that was the problem!!

  • Like 1

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

×