• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

A neater way of updating the image_index from array values? [RESOLVED]

I

innercitysumo

Guest
I'm trying to learn coding & GameMaker and one of the things I worry about is that my solutions are very inefficient/messy because I rely heavily on the few things I know. Here's an example where I feel it could be made more elegant, but I don't know how.

I have an array that I use to keep track of the health of different parts of a ship, e.g. hull, engines etc. This value, 0-100, is then used to display a sprite - so if the health of the hull is 20% then the 20% health graphic is displayed in the relevant section.

In the Step event I am setting the image_index based on the array value as per the code below, but there is a *lot* of repetition. My first thought was to use a Switch case, but it looks like I can't do that with an array. Does anyone have any pointers on how I could do this better?

Thanks

Code:
if array[0,0] == 100
{
image_index = 0;
}

if array[0,0] == 90
{
image_index = 1;
}

if array[0,0] == 80
{
image_index = 2;
}

if array[0,0] == 70
{
image_index = 3;
}

// etc
 
D

DevNorway

Guest
Code:
image_index = 10 - (array[0, 0]  / 10);
You probably need to use the round or floor function in this code. Pick what suits you the best.
 
Top