• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

GMS 2.3+ Setting image index does not display the expected sub image

bob97470

Member
Encountering a strange issue in regards to image_index. problem is reproduceable.
create new project
create sprite and resize to 96x128. (resizing may not be necessary)
create 29 frames and draw the numbers 0 through 28 in them. (drawing the numbers is not necessary, but helpful to see results)
create object and assign the sprite to it.
drop object in room.
create press key event for the object and try any of the following

image_index = 15; //this shows sub image 14, consequently setting it to 14 also shows 14 and setting it to 16 shows 16, but 15 will not show 15
or
image_index++; //count your way up as you press the key and notice that sub image 14 shows twice in a row, that doesnt sound right.
or
image_index = 15.01; //this actually shows the correct sub image.

you can also create a draw event like
draw_self();
draw_text(x+10,y+10,string(image_index));

this will of course show the image_index in the corner of the sprite as you cycle through them. I noticed that once it gets to 15 it changes from an integer to a decimal. displaying 15.00 , but sub image is still 14.

If anyone can explain why this is happening I would appreciate it.
 

Slyddar

Member
If anyone can explain why this is happening I would appreciate it.
It's interesting, but it's likely to do with floating point rounding.

If you set up a variable, say num, and have mouse wheel up/down increase/decrease it, you can easily move through the index values. 15 does indeed become 15.00, and does not show image_index 15, instead it shows as 14. The same thing happens if you continue counting up and loop around. When num is 30 image_index = 1.00 but it shows image_index 0. num 4 shows image_index 3, num 7 shows image_index 6, and so on.

Maybe floating point rounding, as mentioned in this post from the manual - https://forum.yoyogames.com/index.p...-i-turn-an-int-into-a-float.41764/post-256967
As you've mentioned, if you set num to something like 15.01 it will display fine.

I noticed too that it's not working when you set image_index = 15 directly in the create event, with an image_speed of 0, but it does when image_index = 15.01. Definitely weird.

For anyone wanting to play around with it - Project
 
Last edited:

Tornado

Member
I tried yor test project. For me it works fine. I'm using latest Beta on Windows.

Did you try using floor() function when assigning value to image_index from a calculated value? (because num is not directly set, but it is calculated by applying ++ to it)
image_index = floor(num);

I guess after some amount of increments, the aritmetic error accumulates at decimal places, which results in displaying wrong image.
 
Last edited:

Slyddar

Member
Did you try using floor() function when assigning value to image_index from a calculated value? (because num is not directly set, but it is calculated by applying ++ to it)
image_index = floor(num);
I've set num to 15 in the create event. I tried flooring num, and it makes no difference. When num is 15, and assigned to image_index, image_index is 15.00, and still shows image 14. I'm not testing on the beta though. Even if I set image_speed to 0 in the sprite, then in the create event set image_index = 15, with no other code, it still shows 14, but setting it to 15.01 works correctly.
 
Last edited:

Tornado

Member
I tried it now also in GMS 2.3.0.
I can confirm the problem!
When image_index is set to 15, it is like if I set it to 14! The image on the index 14 is shown!!!
1603130094206.png
1603130176325.png

This is crazy!
Apparently they fixed this in Beta, as in Beta it does not occur.
 
Top