Jump to content


Photo

sprite_index doesn't work as claimed?


  • Please log in to reply
8 replies to this topic

#1 Maythe

Maythe

    GMC Member

  • New Member
  • 4 posts
  • Version:GM8

Posted 03 November 2011 - 09:55 PM

Hi everyone,

I used GameMaker 6 and 7 a lot when I was younger and have only recently gotten back into it, to help a friend who is trying to learn version 8.1. He came across a strange problem (well strange to me at least) where changing the sprite_index would reset the image_speed immediately. In my own little test, I ran this code in the step event of an object with an animation of 100 frames:

if(image_index == 20)
{
//sprTest2 is only a 10 frame animation.
sprite_index = sprTest2;
show_message(image_index);
}

Sure enough, the show_message displayed 0 as the image_index where as it had just been 20, 2 lines before. Does this mean that instead of just assigning a new sprite index to the variable sprite_index, GameMaker is actually running a function of sorts each time it sees the assignment? This is the help file entry for sprite_index:

sprite_index This is the index of the current sprite for the instance. You can change it to give the instance a different sprite. As value you can use the names of the different sprites you defined. Changing the sprite does not change the index of the currently visible subimage.


So according to the manual, this shouldn't happen. Is this a newly implemented feature that I missed while I was away?

Thanks for any enlightenment you might be able to offer this confused soul,
Maythe
  • 0

#2 rwkay

rwkay

    YoYo Games CTO

  • YoYo Games Staff
  • 2937 posts
  • Version:Unknown

Posted 03 November 2011 - 10:39 PM

image_index is affected by image_speed and it also loops on the number of subimages in a sprite, so when you change sprite if the number of subimages changes then that will affect the value of image_index.

Russell
  • 0

#3 Maythe

Maythe

    GMC Member

  • New Member
  • 4 posts
  • Version:GM8

Posted 03 November 2011 - 10:55 PM

Hi Russell,

Thanks for your response, but doesn't that happen on a frame by frame basis? This is happening immediately after the sprite is changed.
  • 0

#4 rwkay

rwkay

    YoYo Games CTO

  • YoYo Games Staff
  • 2937 posts
  • Version:Unknown

Posted 03 November 2011 - 11:07 PM

Just checked the code and on GM8.1 at least it is only setting the sprite_index, the addition of the image_speed and the bounding of image_index would happen at the end of the game cycle, so no idea why you are seeing this affect.

Russell
  • 0

#5 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 03 November 2011 - 11:23 PM

gm7:
if the new sprite has the same number of frames or more, image_index will retain it's value.

if the new sprite has less frames, it falls to 0.
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#6 Maythe

Maythe

    GMC Member

  • New Member
  • 4 posts
  • Version:GM8

Posted 03 November 2011 - 11:25 PM

Oh okay that's what I thought, yet both I and my friend got the same effect. When you say you checked the code, do you mean you tried running a similar situation to see if you got the same results? I know it's a real burn of time, but I'd be interested to see if anyone else could replicate this.
  • 0

#7 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 25708 posts
  • Version:GM:Studio

Posted 03 November 2011 - 11:50 PM

Logically, if the image_index is 20 (21 sub-images from 0-20) and the new sprite is only 10 frames (sub-images 0-9), then

20 = (0-9) + (0-9) +1 = 0

See what I mean? You are looping through the sub-images twice and then one more which would set the image-index back to 0. To test this, make the new sprite 12 images long and see if the image_index goes to 2...
  • 0

U1FVsm3.png

40799.png


#8 Maythe

Maythe

    GMC Member

  • New Member
  • 4 posts
  • Version:GM8

Posted 04 November 2011 - 02:24 AM

Nocturne- I see what you mean, and I understand that by the end of the frame, the image_index will be reset to 0. I was just surprised that the index was immediately set to zero because in a normal coding atmosphere, an assignment such as sprite_index = blah would not and could not have any immediate affect on image_index.

icuurd12b42- Is that in the documentation somewhere? If so, then assignments to sprite_index are indeed treated as a function by the GML compiler. Perhaps this change was never added to the 8.1 GameMaker help file?
  • 0

#9 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 04 November 2011 - 02:31 AM

Nocturne- I see what you mean, and I understand that by the end of the frame, the image_index will be reset to 0. I was just surprised that the index was immediately set to zero because in a normal coding atmosphere, an assignment such as sprite_index = blah would not and could not have any immediate affect on image_index.

icuurd12b42- Is that in the documentation somewhere? If so, then assignments to sprite_index are indeed treated as a function by the GML compiler. Perhaps this change was never added to the 8.1 GameMaker help file?


sprite_index or image_index or both are likely not variables but properties. In such code can run code when you assign or fetch their value.

The same with the speed, hspeed, vspeed and direction are intrinsically linked
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button