GameMaker [SOLVED]Storing sprite_index in a ds_map

MCHLV

Member
Hello,

I am sure there is an obvious reason ...
I can store sprite_index in a ds_list (and use for drawing later)
But this does not work with ds_map... I have tried string / asset_get_index...<edit> it does in fact </edit>

This works fine
Code:
// create_event
list = ds_list_create();
ds_list_add(list, sNPC_4_Attack);
// draw_event
draw_sprite(ds_list_find_value(list, 0), 0, x, y)
<edit>This works also fine - Thanks for your help !
Code:
 // create_event
map = ds_map_create();
ds_map_add(map, "sprite", sNPC_4_Attack);
// draw_event
draw_sprite(map[? "sprite"]), 0, x, y)
</edit>

<edit>This is the code I tried which does not work
Code:
 // create_event
map = ds_map_create();
ds_map_add(map, "sprite", sNPC_4_Attack);
// draw_event
draw_sprite(ds_map_read(map,"sprite"), 0, x, y)
--> this is where I was wrong as ds_map_read is not to get the value from the map...
</edit>

I would like to use ds_map to organize attacks properties for my players, monsters, bosses. Storing multiple attacks with all variables I need to manage them in terms of timing, damages, sprites...

Thanks for your help,

M.
 
Last edited:

rIKmAN

Member
Hello,

I am sure there is an obvious reason ...
I can store sprite_index in a ds_list (and use for drawing later)
But this does not work with ds_map... I have tried string / asset_get_index...

This works fine
Code:
// create_event
list = ds_list_create();
ds_list_add(list, sNPC_4_Attack);
// draw_event
draw_sprite(ds_list_find_value(list, 0), 0, x, y)
This does not
Code:
 // create_event
map = ds_map_create();
ds_map_add(map, "sprite", sNPC_4_Attack);
// draw_event
draw_sprite(ds_map_read(map,"sprite"), 0, x, y)
I would like to use ds_map to organize attacks properties for my players, monsters, bosses. Storing multiple attacks with all variables I need to manage them in terms of timing, damages, sprites...

Thanks for your help,

M.
I couldn't see any reason why your code wouldn't work so I just copied your map code into GMS2 to test, changed the sprite name in the Create Event and ran it, and as expected it worked fine.
Also works fine when using accessors: draw_sprite(map[? "sprite"], 0, x, y)

Are you sure the sprite isn't being drawn behind something else?
Are you sure x/y are valid on-screen coordinates?

Remember it will be drawn at depth and x/y of the object drawing the sprite, so if you've got an empty manager object or similar placed outside of the room then that's why you aren't seeing it.
 
Last edited:

MCHLV

Member
Thank you. That is fast.
<edit>
In fact it is drawing another sprite. What ever the sprite index I am trying to set in the ds_map, the draw_event always draw the same sprite. The first one in my IDE. I have tried in another empty project and I have the same results.
</edit>
 

rIKmAN

Member
Thank you. That is fast.
<edit>
In fact it is drawing another sprite. What ever the sprite index I am trying to set in the ds_map, the draw_event always draw the same sprite. The first one in my IDE. I have tried in another empty project and I have the same results.
</edit>
Try clearing the cache (broom icon at the top of the IDE) and running again to make sure it isn't a caching issue.

If that doesn't fix it post the code for the object you are using to draw if it is different that what you have posted so far.
As I said I just ran the code from your OP and it worked fine.
 

MCHLV

Member
Thanks. So I "Broomed". And also created an executable for the same results. The first sprite in my IDE is always the onde displayed. And If I want to dispaly the index it gives me 0 (which is consistent but not helping me). It is the exact same code I copied-pasted in a new project. Except I created 3 sprites and changed the name sNPC_4_Attack to sprite_2...
 

TsukaYuriko

☄️
Forum Staff
Moderator
If a drawn sprite is always the first one, the alleged sprite ID you're using to draw it is usually a string (as strings are treated as 0 in such contexts). Make sure you're not referring to any sprite by name, but by resource ID. (spr_player, not "spr_player")
 

MCHLV

Member
Thanks. Yes. I used the sprite resource ID (color coding and autocompletion). In a brand new project.
I also tried asset_get_index() in the create of the ds_map - draw the first sprite
Then tried asset_get_index() in the draw (and of course not in the create)
(ds_map_add also gives me true)
 
Last edited:

rIKmAN

Member
Thanks. Yes. I used the sprite resource ID (color coding and autocompletion).
I also tried asset_get_index() in the create of the ds_map - draw the first sprite
Then tried asset_get_index() in the draw (and of course not in the create)
(ds_map_add also gives me true)
Have you tried this in a fresh project to isolate it?

The code in your OP works - I just added 2 more sprites, added them to the map and draw all 3 of them using draw_sprite() without any issues.

EDIT:
Does your object that is drawing the sprites already have a sprite attached to it?
Are you also using draw_self() after the draw_sprite() code that could be drawing over the top of it?

EDIT2:
I'm using an empty object (no sprite attached) dragged into the middle of a room.
I have 2 sprites called sprite0 and sprite1 in the Resource Tree.

Here's the code:
Code:
// Create Event
map = ds_map_create();
ds_map_add(map, "sprite0", sprite0);
ds_map_add(map, "sprite1", sprite1);
Code:
// Draw Event
draw_sprite(map[? "sprite0"], 0, x, y)
draw_sprite(map[? "sprite1"], 0, x+100, y)
The result:
 
Last edited:

MCHLV

Member
@rIKmAN, yes I have done that in a new project with only that code.
My drawing_sprite has no sprite attached to it... I just tried with sprite set and have the same result...
There is only the draw_sprite() in draw event nothing after (even if I included also a draw_text to understand what is going on...)

The test project : <deleted>

1 room, 2 objects :
- object DSLIST displays the sprite stored in a list
- object DSMAP displays the sprite stored in a map
object DSMAP displays a message upon creation to confirm the ds_map_add worked...

3 sprites sprite1, sprite2, sprite3... showing 1, 2, 3

Both object should display sprite2 (the green one with a 2 :p). But the DSMAP object displays sprite1
 
Last edited:

MCHLV

Member
@rIKmAN Thank you very much. This is the second time you help me out in like a week. I feel so dumb for misunderstanding that.. Thanks
 

rIKmAN

Member
@rIKmAN Thank you very much. Your code works on my side.... This is the second time you help me out in like a week !!
What I do not understand is that :
draw_sprite(map[? "sprite0"], 0, x, y) works fine
draw_sprite(ds_map_read(map, "sprite0"), 0, x, y) does not works
Because like I just said, you are using ds_map_read() to try and read the value from the map which is not the correct function for doing that - you need to use ds_map_find_value()
Read the manual page for ds_map_read() and you will see why it doesn't work.

The name of the functions catch a lot of beginners out who think they are functions to read/write values to the map - but that's what the manual is for! ;)
 

MCHLV

Member
@rIKmAN Thank you very much. I edited my post (my answer crossed yours). This is now totaly clear to me, thanks for your patience. I can't see how I spent 2 hours on this
 

rIKmAN

Member
@rIKmAN Thank you very much. I edited my post (my answer crossed yours). This is now totaly clear to me, thanks for your patience. I can't see how I spent 2 hours on this
Don't worry, we've all been there with something or another!

I did something similar looking for bugs in a completely different script than I should have been just last week!
 
Top