|
do while problem
#465817
05/14/17 08:29
05/14/17 08:29
|
Joined: Dec 2010
Posts: 224 NRW, Germany
NeoJones
OP
Member
|
OP
Member
Joined: Dec 2010
Posts: 224
NRW, Germany
|
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:
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
Errors are the engine of progress.
Version: A8 Commercial OS: Win 7 64bit Models: Cinema 4D
|
|
|
Re: do while problem
[Re: Aku_Aku]
#465820
05/14/17 09:55
05/14/17 09:55
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
Serious User
|
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
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 ()).
Last edited by Reconnoiter; 05/14/17 09:56.
|
|
|
Re: do while problem
[Re: Aku_Aku]
#465822
05/14/17 10:07
05/14/17 10:07
|
Joined: Dec 2010
Posts: 224 NRW, Germany
NeoJones
OP
Member
|
OP
Member
Joined: Dec 2010
Posts: 224
NRW, Germany
|
@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  Sorry for my bad english xD
Last edited by NeoJones; 05/14/17 10:11.
Errors are the engine of progress.
Version: A8 Commercial OS: Win 7 64bit Models: Cinema 4D
|
|
|
Re: do while problem
[Re: Reconnoiter]
#465824
05/14/17 10:58
05/14/17 10:58
|
Joined: Dec 2010
Posts: 224 NRW, Germany
NeoJones
OP
Member
|
OP
Member
Joined: Dec 2010
Posts: 224
NRW, Germany
|
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: 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.
Last edited by NeoJones; 05/14/17 11:00.
Errors are the engine of progress.
Version: A8 Commercial OS: Win 7 64bit Models: Cinema 4D
|
|
|
Re: do while problem
[Re: NeoJones]
#465827
05/14/17 14:55
05/14/17 14:55
|
Joined: Sep 2009
Posts: 1,032 Budapest
Aku_Aku
Serious User
|
Serious User
Joined: Sep 2009
Posts: 1,032
Budapest
|
If you want to wait for finishing of a function you should do this way:
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.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|