Return Problem...

Posted By: NeoJT

Return Problem... - 04/24/13 15:06

Hi... (and sorry for my english)

I have created a function for checking a string with an array of strings
and i want to return the position of that string if exist ... otherwise return -1.

l_temp_1_array_str : contain the string.
g_commands_array : is a TEXT* that contain the strings that i want for check
g_commands_sum_int : is a variable that contain the sum of commands

l_temp_2_int : is the variable that i want to contain the position of string in array.

Code:
.
.
int l_temp_2_int = 0;
.
.
l_temp_2_int = f_check_str(l_temp_1_array_str);
wait_for(f_check_str);
.
.



Code:
typedef var bool;

//----------------------------------------
int f_check_str(STRING** l_temp_1_array_str)
{
   int l_i;
   bool l_while_stop_bool;
		
   l_i = 0; 
   l_while_stop_bool = false;	
   while ( (l_i < g_commands_sum_int) && (l_while_stop_bool == false) )
   {		
      if ( str_cmp(l_temp_1_array_str[0], (g_commands_array.pstring)[l_i] ) )
      {
         l_while_stop_bool = true;
      }
      else
      {
         l_i++;
      }
      wait(1);
   }

   if (l_while_stop_bool == true)
   {
      return l_i;
   }
   else
   {
      return -1;
   }
}



i have check inside that function and it works... and the l_i has the value that i want...

but the l_temp_2_int ( is the variable that call the int function ) takes a wrong number ... e.x.: (78775160)
(i have check this with the printf("%i", l_i); after the wait_for(f_check_str);

Please help...

thanks you all...

Best Regards.
Dimitris.

Posted By: Uhrwerk

Re: Return Problem... - 04/24/13 16:07

You can either use wait or return inside a function. Don't mix these two. When you call wait inside a function the calling function will procede in the next frame. In this case:
Code:
l_temp_2_int = f_check_str(l_temp_1_array_str);
wait_for(f_check_str);

l_temp_2_int will only get a value assigned if f_check_str returns a value in the exact frame it was called in. Otherwise the value will be random. On a quick look I don't see any reason why f_check_str should contain a wait. Does not make any sense to me.
Code:
typedef var bool;

Be careful. There already is a datatype BOOL and that's long. It does not make any sense to define a "bool" type for that function.
Posted By: NeoJT

Re: Return Problem... - 04/24/13 17:55

i thought that inside all loops (except the "for" loop ) we must use wait();

i tried now and it works..

thank you Uhrweck...

and thank you for your explanation... i must check all my functions and all loops for that... :-)

Best Regards.
Dimitris.
Posted By: Uhrwerk

Re: Return Problem... - 04/24/13 19:53

Originally Posted By: NeoJT
i thought that inside all loops (except the "for" loop ) we must use wait();

You have to use wait when you want to wait a certain time or number of frames. wait does not have anything to do with loops in the first place. There are just many cases when a loop and wait go hand in hand, for example for continuously updating a certain property of an entity.
Posted By: Kartoffel

Re: Return Problem... - 04/24/13 20:22

in addition to what Uhrwerk said:

if you want to go through a while as fast as possible don't use any wait but make sure the while really exits properly. otherwise the application will freeze
...and user input don't work in that case (a mistake I made quite often)
Posted By: NeoJT

Re: Return Problem... - 04/26/13 13:26

me 2 my friend... me 2...
i made that mistake and i thought that the wait() i must put inside
of all loops...

:-)

thank you all and for all :-)

( and sorry for my english ).

Best Regards.
Dimitris.
© 2024 lite-C Forums