do while problem

Posted By: NeoJones

do while problem - 05/14/17 08:29

Hey guys,
i would like to know, how I can solve that problem.
I need a result of a function, that contains a while loop for example:
Code:
function sec_func(var param1)
{
	var i = 0;
        var new_value = 0;

	while(i < 1000)
	{
		i++;
		new_value += getValue(i);
	}
	return new_value;
}

function first_func()
{
        var my_value = 0;	
        while(1)
	{
		my_value = sec_func(50); //no correct result
		wait(1);
	}
}



The above code is just only an example.
My problem is, that I dont get the correct result of sec_func, because the function need to much time to give me a correct result. How can I manage that?

Regards, NJ
Posted By: Aku_Aku

Re: do while problem - 05/14/17 09:43

What do you want finally?
Seems to me there is at least one problem with your code: sec_func doesn't use the param1 variable.
There may be others.
Posted By: Reconnoiter

Re: do while problem - 05/14/17 09:55

For readability I would replace the while loop with a for loop, cause there is another strange thing I notice; you do i++ even before you use the 'i' variable in the array, so getValue[0] never gets used (also use square brackets for arrays not ()).
Posted By: NeoJones

Re: do while problem - 05/14/17 10:07

@Aku_Aku: Oh sorry, yes right. param1 is not used xD This is only an example. I dont need this code for something. Iam very interested, how I can manage that, if I need a result of a function, that contains a while loop and need more time to finish.
The problem is, that the script dont waiting for the other functions.
My only idea is to make a global variable, save the result of the second function in it and wait in the first function for a value.
Or can I use wait_for for that? my_value = sec_func(50); must wait for the sec_func to complete. You know what i mean?

@Reconnoiter: Yes of course. Thats only an example. This -> my_value = sec_func(50); is important for me. The script should be wait for a result of a second function. It gives me an incorrect result, cause the loop need more time to run...

Oh i hope you understand grin Sorry for my bad english xD

Posted By: Reconnoiter

Re: do while problem - 05/14/17 10:34

Ah okay. Anyway you probably use/used a wait() somewhere in your original 'sec_func' before returning the value. Otherwise is should work.
Posted By: NeoJones

Re: do while problem - 05/14/17 10:58

Originally Posted By: Reconnoiter
Ah okay. Anyway you probably use/used a wait() somewhere in your original 'sec_func' before returning the value. Otherwise is should work.


Yes i tried that but the manual says:
Quote:
A function that contains a wait loop can not return a parameter, because wait() already returns to the calling function before the final return statement is excecuted.
And thats my problem. Thats why i think about a workaround.
Posted By: Ayumi

Re: do while problem - 05/14/17 11:41

Just put wait(1) in the first while or use a for loop, because he jump out...with wait(1) the function is waiting for second while. I have had the same problem with count alpha from Panel.
Posted By: NeoJones

Re: do while problem - 05/14/17 11:48

Okay, thanks laugh
Posted By: Aku_Aku

Re: do while problem - 05/14/17 14:55

If you want to wait for finishing of a function you should do this way:
Code:
function first_func()
{
        var my_value = 0;	
        while(1)
	{
		my_value = sec_func(50); //no correct result
		wait_for(sec_func);
		wait(1);
	}
}


In hope i understood you.
Posted By: NeoJones

Re: do while problem - 05/14/17 18:21

Thanks laugh
© 2024 lite-C Forums