GML sprite_index

D

dTex

Guest
I read the help docs and see that I can assign a different sprite to sprite_index via code. Why is it called sprite_index? When I see index, I think [0], [1]. [2]. But I do not see code/examples moving through an array. Also, I didn't understand "using a variable that has an externally loaded sprite indexed in it". I'm a newbie, haven't coded in about 15 years but enjoying GMS2. Sure beats all those "productivity apps" I did for a living.
 
F

FuRyvok

Guest
There are sprite_index, what means for ex. you made a sprite : sprBall, you make an object and don't give a sprite to it.
now go to the create event and there type:
Code:
sprite_index = "sprBall";
What you mean is the image_index, for ex. you have a sprite with 4 index than you have to add the sprite_index to the code.

Code:
switch(playerState){
case "RUNNING":
sprite_index = "sPlayerRun";
break;
case "IDLE":
sprite_index = "sPlayerIdle";
break;
}
 

jo-thijs

Member
I read the help docs and see that I can assign a different sprite to sprite_index via code. Why is it called sprite_index? When I see index, I think [0], [1]. [2]. But I do not see code/examples moving through an array. Also, I didn't understand "using a variable that has an externally loaded sprite indexed in it". I'm a newbie, haven't coded in about 15 years but enjoying GMS2. Sure beats all those "productivity apps" I did for a living.
Hi and welcome to the GMC!

This is a good question.

Variables in GameMaker:Studio 2 can only hold values that are either a number, a string, an array, an enum, a boolean or undefined (ignoring pointers, which are only used to support certain extensions).
Variables thus cannot hold sprites as values.
To refer to a sprite, we need some reference that can be encoded in one of the above data types.
GameMaker encodes references to sprites as numbers.
In GM:S, the number 0 refers to the first sprite in the resource tree of the project,
the number 1 refers to the second sprite in the resource tree, and so on.
You can understand a reference to a sprite as the index to the sprite in the resource tree.
The variable sprite_index holds such references as values.

With "externally loaded sprite", the documentation means that on top of the sprites in the resource tree of your project,
you can also load sprites from files at run time and sprite_index can refer to those as well (despite them not having a name in the resource tree).
 
D

dTex

Guest
There are sprite_index, what means for ex. you made a sprite : sprBall, you make an object and don't give a sprite to it.
now go to the create event and there type:
Code:
sprite_index = "sprBall";
What you mean is the image_index, for ex. you have a sprite with 4 index than you have to add the sprite_index to the code.

Code:
switch(playerState){
case "RUNNING":
sprite_index = "sPlayerRun";
break;
case "IDLE":
sprite_index = "sPlayerIdle";
break;
}
I see the code and understand it. I just don't know why the word "index" was added to the variable member. It can either be not set, set, or modified, right? I guess I'm just hung up on the name. Assuming "index" has something to do with grabbing the right info when compiled.
 

jo-thijs

Member
There are sprite_index, what means for ex. you made a sprite : sprBall, you make an object and don't give a sprite to it.
now go to the create event and there type:
Code:
sprite_index = "sprBall";
What you mean is the image_index, for ex. you have a sprite with 4 index than you have to add the sprite_index to the code.

Code:
switch(playerState){
case "RUNNING":
sprite_index = "sPlayerRun";
break;
case "IDLE":
sprite_index = "sPlayerIdle";
break;
}
I believe you meant to leave the "quotation marks" out of your code examples.
You may only assign numbers to sprite_index, not strings.
 
F

FuRyvok

Guest
Sure you can edit it.
Just make a variable
Code:
var sprIndex;

sprIndex =  "sPlayerRun";

you can modify this if you want just give a new string to the sprIndex variable
And for the image_index you can modify just like the other, just use numbers not strings, thats it.

Give the code, so i can help to you.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Welcome to the GMC! :)

I read the help docs and see that I can assign a different sprite to sprite_index via code. Why is it called sprite_index? When I see index, I think [0], [1]. [2]. But I do not see code/examples moving through an array.
I neither have access to YoYo Games' internal design document nor do I think it exists in a human-readable form :)P), but I guess it might be named "index" because the value you store in it is the index number (or ID) of a sprite. Sprite IDs start at 0 and increment by 1 for every added sprite. One might therefore say that your collection of sprite resources is indexed, and you're assigning the value of specific indices to it.

Also, I didn't understand "using a variable that has an externally loaded sprite indexed in it". I'm a newbie, haven't coded in about 15 years but enjoying GMS2. Sure beats all those "productivity apps" I did for a living.
This refers to a sprite which was loaded at run-time using a function like sprite_add, which loads graphics files from a location on the end-user's storage medium and loads it into memory, to be used as a sprite resource like the ones you define within the IDE. Similarly to how sprite resource names such as spr_player are constants which hold the IDs of sprites, the result of sprite_add, itself a sprite ID, needs to be stored in a variable so that it can be referred to and assigned after loading it.

Sure you can edit it.
Just make a variable
Code:
var sprIndex;

sprIndex =  "sPlayerRun";

you can modify this if you want just give a new string to the sprIndex variable
Before this causes any further confusion: Sprite IDs are numbers, not strings. When strings are assigned to sprite_index, they will be treated as value 0 and therefore, the sprite with ID 0 will be used. Remove the quotation marks and this code works as intended - then you will be referring to a sprite's name, which is technically a constant that holds the sprite's ID, which is a number.
 
F

FuRyvok

Guest
Before this causes any further confusion: Sprite IDs are numbers, not strings. When strings are assigned to sprite_index, they will be treated as value 0 and therefore, the sprite with ID 0 will be used. Remove the quotation marks and this code works as intended - then you will be referring to a sprite's name, which is technically a constant that holds the sprite's ID, which is a number.
With sprite_index you can change the sprite of an Object.
https://docs.yoyogames.com/source/d...stances/instance properties/sprite_index.html

I think you mean the image_index, with image_index you can change the index of the sprite.
 
D

dTex

Guest
Thanks all, I got it and Liked a few posts and yes remove the quotes! The only app I created so far was the Asteroids GML. So I'm now dissecting it and understanding parts I didn't really get.
 

FrostyCat

Redemption Seeker
With sprite_index you can change the sprite of an Object.
https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance properties/sprite_index.html

I think you mean the image_index, with image_index you can change the index of the sprite.
sprite_index is a built-in instance variable that controls the current sprite, and it accepts sprite IDs or -1 (NOT strings like you taught). image_index is a built-in instance variable controls which frame of the currently sprite is active, and it accepts a numeric value.

By the way, I've seen several of your other responses on the Q&A section over the past few days, and your advice is atrocious and misleading on the most part. You're behaving like a fake news bot and I'd like you to stop until you get a better handle on GM basics.
 

jo-thijs

Member
With sprite_index you can change the sprite of an Object.
https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance properties/sprite_index.html

I think you mean the image_index, with image_index you can change the index of the sprite.
You misunderstand.

sprite_index holds the index of the sprite.
image_index holds the index of the image within the sprite.

The index of a sprite is the position of the sprite in the resource tree, it's not a subimage.
It is a number that refers to the sprite, not a string.

Just try this:
1) Create a new project
2) Create a white sprite and call it "sprite0" (and keep it on the top of the resource tree)
3) Create a black sprite and call it "sprite1" (and keep it on the bottom of the resource tree)
4) Create an object with as create event:
Code:
sprite_index = "sprite1";
5) Place 1 instance of this object in the first room of the project.
6) Run the project.

You will notice that the white sprite is being drawn, not the black one.

Thanks all, I got it and Liked a few posts and yes remove the quotes! The only app I created so far was the Asteroids GML. So I'm now dissecting it and understanding parts I didn't really get.
Glad to hear it!
 
F

FuRyvok

Guest
Sorry for my mistake, but i meant to write the sprite_index without quotation marks.
 
Last edited by a moderator:

TheouAegis

Member
If we're going to get hung up on the use of the word "index" in the variable names, then I'll make a hissy-fit about using ceil() instead of ceiling(). Why not call its sister function flr() instead of floor() then? Or why not call the power function pow() instead of power()? And if the circle functions like cos(), sin() and tan() needed you to use degtorad() in the past to work, then why were the newer degree-capable sister functions like dcos() and dsin() not called degcos() and degsin()? Why is it sqr() instead of sq() if its sister function is sqrt() instead of sqrrt()? Why is it round() instead of rnd()? Why is it random() instead of rand()? Why do they use frac() instead of fraction()?

(Some of these really do bother me. I keep getting my GM and Excel functions mixed up. lol)
 

jo-thijs

Member
That was intentional, to show what would happen if you'd actually use strings rather than numbers.

If we're going to get hung up on the use of the word "index" in the variable names, then I'll make a hissy-fit about using ceil() instead of ceiling(). Why not call its sister function flr() instead of floor() then? Or why not call the power function pow() instead of power()? And if the circle functions like cos(), sin() and tan() needed you to use degtorad() in the past to work, then why were the newer degree-capable sister functions like dcos() and dsin() not called degcos() and degsin()? Why is it sqr() instead of sq() if its sister function is sqrt() instead of sqrrt()? Why is it round() instead of rnd()? Why is it random() instead of rand()? Why do they use frac() instead of fraction()?

(Some of these really do bother me. I keep getting my GM and Excel functions mixed up. lol)
Those are all abbreviations you're talking about.
dTex asked about what the "index" of a sprite is, which as the forums have shown on multiple occasions, is something that confuses more people when they start working with GameMaker.
They weren't asking why "sprite_index" wouldn't just be abbreviated to "sprite", they were asking what the additional part to the name means.

Also, people that hate abbreviations might like the spoiler in this post:
https://forum.yoyogames.com/index.php?threads/making-things-clear-for-readers.48916/#post-300632
:D
 
Top