IF elimination

Game Editor comments and discussion.

IF elimination

Postby foleyjo » Wed Sep 21, 2011 9:29 am

I've been readind some threads regarding IF statements and how its best not to use them where possible.
So I went through one of my programs and used alternatives. However there are a couple of statements relating to a timer that I think should be left as if's. Just wondering what you guys think and if there are any better alternatives.

I have a key press capture event within draw actor. There is a timer between each key press to make a delay to prevent the key being repeated.
The key press is caught in a function called InputText() this returns an integer of 1 or 0 to say if there had been a key press . I was saying if (InputText ==1 ) set timer but now I just say

Code: Select all
 Timer =  InputText(text,30)*5;  //5 is the value of the timer


I aslo got rid of if(timer>0)timer--; bit by using
Code: Select all
Timer = max(--Timer,0);


Now the one I cant get rid of. When the Timer is 0 and when the text entry box is ready for an input I have

Code: Select all
if(Timer<=0 && BInputReady)
{
  Timer =InputText(text,30)*5;
}


There is also a seperate key capture event for when the enter key is pressed. The Return key does not rely on the key delay but it does rely on the BInputReady boolean

Code: Select all
if (key[KEY_RETURN] && BInputReady)
{Do stuff}



The only thing that I think I could do to make it better is to have

Code: Select all
switch (BInputReady)
{
  case 1: if(Timer<=0) {do stuff}
              if (key[KEY_RETURN]{do stuff}
  break;
  default:{Error Trap Break} break;
}


Any other ways of doing this that would be better?
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: IF elimination

Postby Game A Gogo » Wed Sep 21, 2011 1:10 pm

Code: Select all
if(Timer<=0 && BInputReady)
{
  Timer =InputText(text,30)*5;
}

can be replaced by
Code: Select all
Timer=(Timer<=0&&BInputReady)?InputText(text,30)*5:Timer;


As for the last one, I'd suggest not using a switch statement if there is only one case... You'd need to assign something in the "Do stuff" so you can figure out which lines you can put outside the if condition, exemple:

Code: Select all
x+=(key[KEY_RIGHT]-key[KEY_LEFT])*BInputReady*5;

I don't know what you want to do with your enter key so I can't help you on this
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: IF elimination

Postby foleyjo » Wed Sep 21, 2011 3:26 pm

Prob make more sense if I just upload the ged file.

It's a quiz based on commodore 64 games.

I am making it to get some practice working with text.
I'm also using it to try using some new techniques I've been reading about in the advanced topics.

It's very messy at the moment as I have been using a lot of trial and error as I'm going along.

The section of code that I was discussing here is found in Answer - Draw Actor.

Feel free to give feedback and advice on any other sections of code though.
Attachments
Quizzo.rar
(975.5 KiB) Downloaded 86 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest