Show an image based on a set of unique combinations

H

Hollylala

Guest
Hello! I'd like to have 3 dials and based on the position of each when a button is pushed a different image will show. My question is how to map the image to the correct combination of dial positions. Something like, if a = 2, b = 3, c = 4, then image = apple; if a = 3, b = 2, c = 6, then image is orange....and so on for 20 or so combinations/images. Your help is greatly appreciated!
 

Evanski

Raccoon Lord
Forum Staff
Moderator
Look up image_index

and have the dial state change what index of the sprite for the dial should be drawn

Edit: re read

same thing applies really
have all the images in subimages of a sprite
and have them change when a, b and c are at the right values
 

ophelius

Member
After reading the new GM 2.3 features coming out soon, it's almost worth waiting for its release just for the multi-dimensional arrays that will be available. Then you can easily have this:

//set all your combinations
Combination[2][3][4] = imgApple;
Combination[3][2][6] = imgOrange;

//Then you can set your image from this array to be whatever value the dials are:
sprite_index = Combination[dial1.value][dial2.value][dial3.value]

But you'd have to wait for version 2.3 to be released first

Edit:
But considering you only have like 20 combinations, it's not that troublesome to just have:

if(dial1.value == 2 && dial2.value == 3 && dial3.value == 4) sprite_index = imgApple;
if(dial1.value == 3 && dial2.value == 2 && dial3.value == 6) sprite_index = imgOrange;
etc...
 
Last edited:
H

Hollylala

Guest
Look up image_index

and have the dial state change what index of the sprite for the dial should be drawn

Edit: re read

same thing applies really
have all the images in subimages of a sprite
and have them change when a, b and c are at the right values
Thank you very much! This helped me think through what I actually need and will help me in other ideas!
 
H

Hollylala

Guest
After reading the new GM 2.3 features coming out soon, it's almost worth waiting for its release just for the multi-dimensional arrays that will be available. Then you can easily have this:

//set all your combinations
Combination[2][3][4] = imgApple;
Combination[3][2][6] = imgOrange;

//Then you can set your image from this array to be whatever value the dials are:
sprite_index = Combination[dial1.value][dial2.value][dial3.value]

But you'd have to wait for version 2.3 to be released first

Edit:
But considering you only have like 20 combinations, it's not that troublesome to just have:

if(dial1.value == 2 && dial2.value == 3 && dial3.value == 4) sprite_index = imgApple;
if(dial1.value == 3 && dial2.value == 2 && dial3.value == 6) sprite_index = imgOrange;
etc...
Much thanks! I think this is what I need in the situation I'm imagining....very helpful! Do you know when we can expect the new release?
 

ophelius

Member
Much thanks! I think this is what I need in the situation I'm imagining....very helpful! Do you know when we can expect the new release?
Not sure, you can follow this thread for details:

I would just hard-code it for now like my 2nd example, and make a note to change it to arrays when it's released, just so you can move on with your project
 

TheouAegis

Member
No need to wait for GM2.3, just combine the values and use a switch. Especially if invalid combinations would have the same values. A 3-tier array would be a huge waste of memory.

switch (a*10+b)*10+c {
case 234: sprite_index = apple; break;
case 326: sprite_index = orange; break;
default: sprite_index = blank; break;
}
 

ophelius

Member
No need to wait for GM2.3, just combine the values and use a switch. Especially if invalid combinations would have the same values. A 3-tier array would be a huge waste of memory.

switch (a*10+b)*10+c {
case 234: sprite_index = apple; break;
case 326: sprite_index = orange; break;
default: sprite_index = blank; break;
}
Props to you, this is much better, it's a clever solution. You're right, even though using arrays would be a bit simpler, and assuming 9 possible digits per dial, that would 9^ 3 = 729 * 4 bytes ~~ 3 megs of memory. Which isn't much for today's computers, but still, I agree that you should always be as efficient as you can.

Edit: Although, this would only work if the dials have 10 or fewer digits. I suppose if it's 16 or fewer digit you just use hexadecimal, but how can this be revised if there's more than 16 positions on each dial, without resorting to arrays?
 
Last edited:

TheouAegis

Member
The 10 is your base. We're assuming he is setting his dials using base 10, 0 through 9. Base 16 would be 0 through 15, so you would replace the 10 with 16. Likewise, if you're counting using any other base, you would just replace the 10 with whatever your base is. If you have six digits possible, your base would be 6.

Now as for how he would actually write his cases out, that would be a lot more work for him, since he would want a calculator on hand unless he is really good with converting bases in his head. LOL Like, I can kind of convert base 10 over to base 8, base 16, or base
2... But it would take me awhile to convert base 10 over to base 6. LOL but yeah, writing out base 10 or base 16 is pretty straightforward, assuming he is familiar with writing in hexadecimal.

So let's assume he is using base 11. Then 2-3-4 would be 279. Now, if game maker is still as thorough as it seemed to have been back in last time I tested, he could just write out the formula for every case.

case (2*11+3)*11+4:

The compiler, from what I could tell, would calculate that out itself, not force it to be calculated at runtime.
 
Alternatively to using the switch case method, and supporting an infinite number of digits to the dial, you could concatenate the values to a string and compare it to a ds map of valid combinations. Not at my computer, so I'm not going to type out a full example, but your map keys might look something like "2:3:4".
 
Top