Game Development Community

Your more experienced opinion on a function

by Joao Brandao · in Torque Developer Network · 09/28/2011 (7:52 am) · 9 replies

First of all: Hi everyone!
I'm new to both Torque 2D and programming in general, so I apologize in advance for any questions with what may seem really obvious answers.

I would like to ask your opinion on a function I created. It works, I just wonder if it's really all that efficient. The function makes a certain class of objects (in this case a circle) change frames (it's a cell imagemap) with every mouseUp. BUT, I wanted it not to repeat it self. If for example the current frame is 4 the new frame number can't be 4 again. This is what I did:

function movingLight::onMouseUp(%this)
{
   %frameCounter = %this.getFrame();

   if (%frameCounter == 0)
      while (%this.getFrame() == 0)
      {
         %this.setFrame(getRandom(0,6));
      }
   
   if (%frameCounter == 1)
      while (%this.getFrame() == 1)
      {
         %this.setFrame(getRandom(0,6));
      }

   if (%frameCounter == 2)
      while (%this.getFrame() == 2)
      {
         %this.setFrame(getRandom(0,6));
      }

   if (%frameCounter == 3)
      while (%this.getFrame() == 3)
      {
         %this.setFrame(getRandom(0,6));
      }

   if (%frameCounter == 4)
      while (%this.getFrame() == 4)
      {
         %this.setFrame(getRandom(0,6));
      }

   if (%frameCounter == 5)
      while (%this.getFrame() == 5)
      {
         %this.setFrame(getRandom(0,6));
      }
      
   if (%frameCounter == 6)
      while (%this.getFrame() == 6)
      {
         %this.setFrame(getRandom(0,6));
      }
}

What do you guys think?

About the author

I'm a little old to start programming, as I'm 32, but being unemployed pushed me into learning new skills and go back to the area I graduated in (New Technologies). I am my own worst enemy, as I procrastinate a lot, and I'm very lazy. Also, I love games.


#1
09/28/2011 (8:24 am)
@Joao - I updated your original post to use the code tags and proper tabbing, just to make it easier for other people to read. Looking at it, there are ways to optimize it.

1. Swap all the if statements out for a single switch statement, with each case representing %frameCounter:

switch(%frameCounter)
{
   case 0:
   // some code

   case 1:
   // some code

   // and so on through each numerical case
}

That's the first place I would start. Next, having a while statement based on a random number generator is prone to major delays. I see what you are trying to accomplish. You want a random frame that is not the current frame number. The code will execute fairly quickly, but the worst case scenario is that you could end up in an infinite loop. It's not likely, but it's possible.
#2
09/28/2011 (9:02 am)
Hi Michael. Thanks about the code tags (I had wondered about that).

Thanks for the suggestion, I swapped all the if statements for switchs, and it works fine.

When you say that in a worst case scenario I could end up in an infinite loop with the while statements using a random number generator, how could that happen if I have the generator creating a very limited ammount of numbers and I have all those numbers covered in the switch statements? I really don't understand where it could go wrong. Unless the generator would randomly generate the same number over and over again ad nauseam.
#3
09/28/2011 (9:11 am)
Quote:I really don't understand where it could go wrong. Unless the generator would randomly generate the same number over and over again ad nauseam.
As I said, it is highly unlikely. The worst case scenario is exactly what you stated. The generator could produce the same value until the end of time. This could be due to a bug or the worst luck known to the history of the world. I'm saying it is possible, but the odds are low. Just thought I'd bring it up.
#4
09/28/2011 (9:15 am)
Thanks again!
#5
09/28/2011 (11:17 am)
You can simplify that a lot

function movingLight::onMouseUp(%this)
{
   // get the current frame
   %currentFrame = %this.getFrame();

   // generate a random frame
   %randomFrame = getRandom(0,6);

   // if randomFrame is equal to currentFrame,
   // keep trying until we get one that isn't
   while(%randomFrame == %currentFrame)
      %randomFrame = getRandom(0, 6);

   // set the frame
   %this.setFrame(%randomFrame);
}
#6
09/28/2011 (12:31 pm)
I look at that simplicity, and I feel like an even bigger noob :)
Thank you. That is very clean.
#7
09/30/2011 (6:02 pm)
I don't know if any of you can help me further, but I'm having some kind of trouble with 1.5

Now, I can't even get a simple mouseUp event to work.

this doesn't work:

function movingLight::onAdd(%this)
{
   %this.setUseMouseEvents(true);
   %this.setFrame(getRandom(0,5));
}

function movingLight::onMouseUp(%this)
{   
   %this.setFrame(getRandom(0,5));
}

The mouseUp simply doesn't do anything. And yes, this script file is being properly executed, since the setFrame in the onAdd function is working.

Please help.

#8
09/30/2011 (6:25 pm)
@Joao - We upgraded the touch support in iTorque 2D v1.5. You need to change onMouseX functions to onTouchX. For example:

function movingLight::onTouchUp(%this, %touchID, %worldPos)
{
   %this.setFrame(getRandom(0, 5));
}

Read through THIS LINK to learn about the updated input system.
#9
10/01/2011 (2:19 am)
Thank you once again Michael.