Inkey in function entertains me with error messages [solved]

Posted By: Mr Wurm

Inkey in function entertains me with error messages [solved] - 12/20/08 13:46

After spending near two days, trying all possible manipulations i could think off with this code, I would like to ask for help.

What I'm trying to do is get the player to enter a number thats passed back as the return of the function. Its a real simple piece of code, which is why I just for the world of me can't figure out where it goes wrong.
Quote:


STRING* s_t_input = "xx";
TEXT* t_input =
{
layer = 10;
pos_x = 0;
pos_y = 0;
string = s_t_input;
flags = VISIBLE;
}

int number_input(){
inkey((t_input.pstring)[0]);
str_to_int((t_input.pstring)[0]);
return(str_to_int((t_input.pstring)[0]));

}


I have tried using directly a string rather than a text element. Have tried waiting for inkey with wait_for(inkey(x));. Have tried waiting in while for inkey_active to turn to 0 again. Have also tried using inchar along the lines of the snippet in the manual.
Interestingly, when I move the code out of a dedicated function (unpractical but possible) it doesn't crash anymore, but instead of the code being executed multiple times, its executed one time, then never again. I'm baffled and sure its something completely obvious....but what?

Error I get is E1513. "Crash in number_input"

Thanks in advance for any suggestions.
Posted By: Xarthor

Re: Inkey in function entertains me with error messages - 12/20/08 14:23

Why not just:
Code:
int number_input()
{
  result = inkey(s_t_input);
  if(result == 13)
  {
    return(str_to_int(s_t_input));
  }
}

Posted By: Mr Wurm

Re: Inkey in function entertains me with error messages - 12/20/08 14:34

I've replaced the funtion with your suggestion Xarthor, but it doesn't change anything. Still the same Error Message.

The behaviour I get is that the engine starts up, then shows the error message, after which the input function is called, so i can enter the number, but the value returned (observed through digit) doesen't match the input. Furthermore, the inkey is triggered only one time, when really it should be triggered twice.
Posted By: Spirit

Re: Inkey in function entertains me with error messages - 12/20/08 15:26

Your function has no crash bug, so something else is crashing in your script, but probably your calling the function in a wrong way. The "return" makes no sense.

When you want to do something with the entered string, you must wait until the user has pressed the enter key. But your function returns immediately before the user has entered anything. So you cant do anything with the return value.

Code:
int number_input()
{
   inkey((t_input.pstring)[0]);
   printf("%d",str_to_int((t_input.pstring)[0]));
}


function main()
{
   number_input();
   printf("Immediate return!");

}


Hope this helps.


Posted By: Mr Wurm

Re: Inkey in function entertains me with error messages - 12/20/08 16:16

Thanks for your reply.

If I replace the inkey function with a dummy that just passes an ever increasing int every time it i called, I get no error.
Quote:

int number_input() //dummy
{
global_counter++;
return(global_counter-1);
}

//The way I call the function
int loc_i2 = number_input();


I see, so the problem lies in timing. Program skips ahead to what follows the function call with a value of 0, then after inkey is terminated the rest of the commands in the function are executed but its now too late to pass the int to the function which has already finished executing, wreaking havoc with the code.

At this point I am feeling like a total noob...So how can I make the function wait for the input? "while(number_input() == 0){wait(1);} doesn't seem to do the trick. Alternatively, just having

Quote:

int number_input(){
inkey((t_input.pstring)[0]);
result= str_to_int((t_input.pstring)[0]);
}

//and calling
number_input();
while(inkey_active != 0){ wait(1); }
wait(-1);
loc_i2 = result;

results in a similar overrun, because the function call is in a loop thats executed 3 times, I only get to enter the number once, after which the same number is feed back another 2 times.

I'm going bonkers over this...
Posted By: Gordon

Re: Inkey in function entertains me with error messages - 12/20/08 16:36

Using your inkey function you could:

Code:
void number_input() {
   inkey((t_input.pstring)[0]);
   result= str_to_int((t_input.pstring)[0]);
}
 
//in your other function
for (i = 0; i < 3; i++) {
   number_input();
   while (proc_status(number_input)) {
      wait(1);
   }
   loc_i2 = result;
}


the reason for this is that inkey will internally exicute a wait and will cause your function to return to the caller.
Posted By: Mr Wurm

Re: Inkey in function entertains me with error messages - 12/20/08 17:10

Thanks Gordon. proc_status(x) finally did the trick. Since the function making the call was directly called 3 times in a list,
I needed an additional if(inkey_active !=){ wait(1); } to keep the three functions waiting for their turn. Now its _finally_ working....yesss! All that remains is to clean out all the clutter i added during trialanderroring

Thank you for your time everyone.

Posted By: Gordon

Re: Inkey in function entertains me with error messages - 12/20/08 18:13

Quote:

Since the function making the call was directly called 3 times in a list,
I needed an additional if(inkey_active !=){ wait(1); }

you should also use the proc_status for the function that makes the calls in the list. This will give you freedom to change to any type of input you want later.
Posted By: Mr Wurm

Re: Inkey in function entertains me with error messages - 12/21/08 00:18

Code:
	
while(inkey_active != 0){	wait(1);	}
number_input();
while(proc_status(number_input)){wait(1);}
loc_i2 = result;


Thats the way it is now. And I guess I'll be writing the same thing whenever I'm calling the number_input(); function in my code. Not all that trim and pretty, but it works. Won't be requesting User input all that often anyways, luckily smile.

Not quite sure I understand what you are trying to say though Gordon. What do you mean with type of input? As I understand, the way the function is set up now, even if the mother function is called numerous times, the copies will queue for the bottleneck human, until one by one input has been given (hopefully in the order entered).

What do you mean with "freedom to change to any type of input"?
Posted By: Mr Wurm

Re: Inkey in function entertains me with error messages - 12/21/08 14:29

Nvmind, I have discovered the meaning in your words, and the faults in my logic blush
Now I know what it is, solving it is piece of cake.
© 2024 lite-C Forums