Stopping a function

Posted By: binsky33333

Stopping a function - 08/06/09 20:30

Ok so here is my problem basically i am trying to switch between functions and such. When i switch to another function, i want to be able to stop the other function so everything in it does not work anymore. Is there a command that can do that? Not like sleep or wait... But something that will completely quit the function?
Posted By: the_mehmaster

Re: Stopping a function - 08/06/09 20:42

#EDIT: I clicked on the icon on the side.. Didn't know it was c-script. Maybe this will help you anyways..

Use goto to skip to the end of a function:
(Lite-C)
Code:
function something()
{
//some instructions
if(needtoquit)goto somethingend;
//more instructions
somethingend:
wait(1);
}


There are better methods, but i can't get them out of the top of my head..
Posted By: Claus_N

Re: Stopping a function - 08/06/09 20:44

The only function to kill processes that comes to my mind is proc_kill, and it can't be used for that purpose.

It is however possible to do something like this:
Code:
var functionIsRunning = 0;	// A global variable

function theFunction()
{
	// Set the variable to non-zero
	functionIsRunning = 1;
	
	// Run the function as long as functionIsRunning is non-zero
	while(functionIsRunning != 0)
	{
		... // Code
	}
}



This way you can stop the function by setting 'functionIsRunning' to 0.

Edit: Never use 'goto', please frown
Posted By: DJBMASTER

Re: Stopping a function - 08/06/09 20:46

I'm not sure if you can call a function and then destroy the calling function because i think that messes up the path of execution.

Can't you just put the function to call at the end of the calling function. When the called functions finishes, it will return to the original function and then that will return. Maybe i misunderstood the question.
Posted By: the_mehmaster

Re: Stopping a function - 08/06/09 20:48

Quote:
Edit: Never use 'goto', please

I know, i know.. Very bad programming practice..

Blinsky, use Claus_N's method.
Posted By: binsky33333

Re: Stopping a function - 08/07/09 02:13

ok guys, thank you!
Posted By: EvilSOB

Re: Stopping a function - 08/07/09 04:55

I may be missing something, but cant you just use return?
As in
Code:
function firstone()
{
    while(1)
    {
       if(xxx==0)
       {
           nextfunction();
           return;
       }
       wait(1);
    }
}


Posted By: Rasch

Re: Stopping a function - 08/07/09 09:14

If it is in a while loop just use break then.

Code:
function breaking()
{
    while(1)
    {
       if(x == 0)
       {
           do_this();
           break; // Stops the while loop and continues after it
       }
       wait(1);
    }
    // Here could be more code
}


Posted By: EvilSOB

Re: Stopping a function - 08/07/09 09:44

Ive done it this way cause he said he wanted to "kill" the function.

You could also look into "proc_kill" in the manual, but I dont know if it is C-Script...
Posted By: Claus_N

Re: Stopping a function - 08/07/09 09:54

Yeah, 'return' or 'break' could be used as well, I guess it's a matter of personal preference and the situation smile

proc_kill can be used in both C-Script and Lite-C.
© 2024 lite-C Forums