Return Vector not possible after 1 frame

Posted By: Yking

Return Vector not possible after 1 frame - 02/24/15 18:13

Hello everybody,

I have yet another problem I am not sure about why it occurs.
I have the following code-example:

Quote:

function calculate()
{
wait(1);
return (vector(100,200,300));
}



function main()
{
VECTOR* the_endresult = calculate();

if (the_endresult.x == 100 & the_endresult.y == 200 & the_endresult.z == 300)
{printf("Works");}
}



I just want to return a vector, but as long as the wait(1);-command exists, this whole code does not work.
In my actual (much longer code) I have a while-loop in my return function (which contains wait-commands) so I can't leave them out.
But only 1 single wait-command and the return-function won't work.

I also don't want to globally save my numbers because this function might be opened multiple times at the same time with different numbers to work with, so a global variable would be overwritten by all functions and that might cause weird results.

Is there any good way I can still return my Vector?
Posted By: Wjbender

Re: Return Vector not possible after 1 frame - 02/24/15 18:58

"wait" returns from a function like "return" ,it returns back to the calling function (if there's one),in this case it returns back to main ,and by then that function wich assigned the value to the vector already returned through wait.., after which wait is supposed to resume to the next code line where the vector is returned ,but all to late for the calling function , it makes no sense to me why one would want a "wait" if you return with a vector anyway ..

or I am confused in What's suppose to be , and my understanding of this failed terrible lol

jb

edit : oh I forgot , if you move the "wait" to the last line your function would work as you wrote it there however senseless outside a loop as it is there ...
Posted By: Wjbender

Re: Return Vector not possible after 1 frame - 02/24/15 19:39

oh and if you need to compare you should use && instead of &

although I'm sure that was just a mistake on your post..

you can also pass the vector through the function's argument list ,something similar to

void getvalue (var x , var y, var z)

or

void getvalue (VECTOR* that_vector)

Posted By: Yking

Re: Return Vector not possible after 1 frame - 02/25/15 08:41

Thanks for the help,

This code I gave is only an example, in my actual code there would be a while-loop that contains a c_move. Every loop the c_move is used to move an entity a little bit. Well it doesn't really matter, I cant use return this way so I have to figure out something else I guess smirk

(Passing through the functions argument list does not work in my code, I dont want to call the function again)
Posted By: Wjbender

Re: Return Vector not possible after 1 frame - 02/25/15 10:04

Originally Posted By: Yking
Thanks for the help,

This code I gave is only an example, in my actual code there would be a while-loop that contains a c_move. Every loop the c_move is used to move an entity a little bit. Well it doesn't really matter, I cant use return this way so I have to figure out something else I guess smirk

(Passing through the functions argument list does not work in my code, I dont want to call the function again)


"return" exits the function , so any while loop will stop being executed when return is executed from within that function .

while (1)
{
if(bla) then return bla; // this loop wil shut down
wait (1);
}

now look at what I told you about wait so that you understand my confusing post better.

in the manual goto index and type return

read where it 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 is executed ..

jb
Posted By: Fred_Flintstones

Re: Return Vector not possible after 1 frame - 02/25/15 10:12

So someting like this wouldn't work?

Code:
function calculate(VECTOR* a_vector)
{
wait(1);
vec_set(a_vector,vector(100,200,300)); 
}

function main()
{
VECTOR* the_endresult;	
calculate(the_endresult);
wait_for_my(calculate); //wait(1);
if (the_endresult.x == 100 && the_endresult.y == 200 && the_endresult.z == 300)
{printf("Works");}
}

Posted By: Wjbender

Re: Return Vector not possible after 1 frame - 02/25/15 10:39

that should work however you need to prevent passing an empty pointer laugh

VECTOR *result=malloc (sizeof (vector));
calculate (result);
wait_for_my(calculate); or wait_for (calculate);
etc etc....

jb
© 2024 lite-C Forums