Jump to content

Recommended Posts

Hello, I am curious, is there a way I can stop looping script using text chat? For example:
Script like:

if (!isServer) exitWith {};
[] spawn { 
while{true} do {
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.9,0.1,0.9,0.3)"];
sleep 1;
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.9,0.1,0.1,0.3)"];
sleep 1;
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.1,0.9,0.1,0.3)"];
sleep 1;
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.5,0.1,0.5,0.5)"];
sleep 1;
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.1,0.1,0.9,0.3)"];
sleep 1;
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.1,0.5,0.5,0.5)"];
sleep 1;
this setObjectTextureGlobal [0, "#(argb,8,8,3)color(0.9,0.9,0.1,0.6)"];
sleep 1;
};
};

Would it be possible to for example type in chat smth like "terminate script" and it would stop looping, or well, stop in general?
 

Share this post


Link to post
Share on other sites

You're using

while {true} do

Just use a variable instead and set that to false when you want the loop to stop.

That's the main usage of while loops, being able to be stopped at any time.

 

Cheers

  • Like 2

Share this post


Link to post
Share on other sites

There is still the issue of running code from text chat. I found a script system from A2 that purported to allow this:  

Unlikely it will work out of the box, if at all, but it might point you in the right direction.

 

Unless there's some new function for A3 that already does this, of course...

Share this post


Link to post
Share on other sites

According to BIki, spawn returns a script handle which can be used to check if the script is done or terminate it. For your case, you would most probably need to call terminate on the script handle. A possible implementation would be (emulating your script with the implementation details skipped)

// Run only on the server
if(!isServer) exitWith {};

// Call the while-loop with the script you want to run and get a handle to it
scriptHandle = [] spawn {
	// In here place the while-loop you wish to run
};

Now, you can use the scriptHandle variable to terminate the script whenever you wish. You can do that with

// Terminate the script
terminate scriptHandle;

Please note two things:

1) According to the documentation of terminate the script will not be terminated immediately, but only the next time it will be processed by the scheduler.

2) In the code I provided the scriptHandle variable is global to the server (since you make sure the script won't run in any other PC than the server). This way you can have access to the handle from other scripts in the server. I am not sure how you could have access to the handle from another computer since according to the publicVariable documentation you cannot broadcast scripts (which is what the returned handle is, according to spawn documentation). Maybe someone around here could provide some alternatives.

 

Hope this helps in one way or the other, or at least give some hints to a possible (better) solution.

  • 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

×