GameMaker Getting precise image_index via multiple of image_speed

Hiya,

Instead of using an alarm and a variable check, I'd like to use a precise image_index value check. To do this, it'd be best if a multiple of image_speed could be determined and peg this in conjunction with the the target frame(s) once the frame of the animation where an action must execute, is reached.

Just as an example, if an animation has 12 frames (index 0 to 11), image_speed of 0.27, and the target frames are index 1 and index 6. Then I could have something similar to the following:

Code:
if (image_index==(multiple of 0.27 that is greater than or equal to 1)) //multiple in this case is 1.08 (0.27*4)
or (image_index==(multiple of 0.27 that is greater than or equal to 6)) //multiple is 6.21 (0.27*23)
    {do stuff}
I have looked at the div and modulo functions in the manual but don't see how they would translate in this case. Is this possible?

Thanks in advance.
 

Yal

🐧 *penguin noises*
GMC Elder
image_index already is the precise subimage of the sprite, what's the point of checking if it's a multiple of the image speed? Also, equalities might never be true if you have a floating-point image_speed because of rounding errors, you should always check for a range to increase stabilities when dealing with real numbers. (You can set the math epsilon used for real equalities but that's besides the point)

It might be easier to use image_index in conjunction with a variable (to make sure the cross-that-subimage boundary only triggers the event once):

Code:
if(image_index >= 1 && !oneshot){
  do_stuff()
  oneshot = true
}
else if(image_index >= 6 && !sixshot){
  do_stuff()
  sixshot = true
}
 
Thanks for the reply.

The point of checking if its a multiple of image speed is to perform an action at a specific frame of animation only once. I understand the issue of rounding. Even so, in testing the use of a multiple of image speed, GM was able to perform the required actions only once per each respective frame, and without any trouble with rounding. In my case, an image speed of 0.27 with the desired image index of 1 would mean the multiple 1.08, and with an index of 6, the multiple 6.21.

The only problem is I don't know how to tell GM to arrive at those figures automatically. In your example, I'd have to create variables, and account for resetting those variables, both of which I'd like to avoid if there is a better way to go about doing this.
 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
The only problem is I don't know how to tell GM to arrive at those figures automatically.
Let's break it down with algebra.

Target frame to do stuff at = unknown constant x * image_speed
T = xI
x = T/I

So if you divide the animation frame you want to do the thing at with image_speed, you've got your image_speed multiple.
 
Thank you again for the reply.

A few things. Maybe my math is really bad, but the definitions of multiples seems off here. If I type "multiples of 6" in google, I get 6, 12, 18, 24, 30, 36, 42, and 48. Therefore, the first few multiples of image speed of 0.27 should be 0.27, 0.54, 0.81 and 1.08.

In the above equation, if I plug-in the values, I get the following:

Target frame T = 1
image_speed I = 0.27
x = a multiple value of 0.27 closest to T

T (target frame) = x (multiple) * I (image_speed)
1 = x * 0.27
x = 1/0.27
x = 3.70

3.70 is not a multiple of 0.27. The goal is to get GM to arrive at the multiple of 0.27 that is the closest figure greater than or equal to the target frame. The "closest to" part can be taken care of. GM just needs to arrive at 1.08 in the first place.
 

Joe Ellis

Member
This is kind of a side note, but image_speeds that don't create 1 at some point result in the animations not changing frames at the exact same rate in game frames. With image_speed I prefer to think of it as "how many game frames do I want it to take until the animation's frame changes"
So if I want it to change anim frame every 7 frames, image_speed = 1 / 7. etc

But this also might be a solution to your problem, if you handle the speed in this way, the checks that you need won't require working out the exact image_index it needs to be, cus they'll be exactly the whole number with no decimals on each frame change
 
^Thanks for the input.

That's an interesting approach and if checking when each individual frame is hit via using whole numbers instead of decimals, that would definitely work. But how would checking the image index be done in this way? The way I see it currently, each frame represents a number as an integer and image_speed increments through each frame. Once it reaches the next integer value, the next frame is displayed. This is what I've been doing so far, and haven't ever found a way to use exact frame numbers (since virtually all my image_speeds range from values between 0 and 1). Could you show me the syntax?
 

Joe Ellis

Member
It would just be: if image_index = n cus of it always being a whole number when the frame is changed.
However that only detects the exact game frame\step that the anim frame is changed,
if you want to know what anim frame the instance is currently on, you can use floor(image_index), this removes the decimals
 
^Thanks.

I wasn't able to get around using decimals unfortunately, so I'll have to stick with the variable+alarm method for now. If a better method is found, I'll update this thread.
 
Top