• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to use sprite_add

N

nicei7

Guest
How to use sprite_add?
I have
i7=sprite_add(working_directory+"\6.gif",1,0,0,0,0)
sprite_index=i7
draw_sprite(sprite_index,-1,x,y)

I have error message"
Trying to draw non-existing sprite.
at gml_Object_ob1_DrawEvent_1 (line 3) - draw_sprite(sprite_index,-1,x,y)"
Working_directory is not where is my gmx file and can't do this.

Where use

i7=sprite_add("C:\Project2.gmx\6.gif",1,0,0,0,0)
sprite_index=i7
draw_sprite(sprite_index,-1,x,y)

again have same problem.

What to use in sprite_add(" ",1,0,0,0,0) to get a sprite?
 

Tsa05

Member
I guess I'll ask the obvious one:
Do you have a file called 6.gif located in C:\Project2.gmx\ ?

Sometimes working_directory doesn't quite work as intended when testing your game, too. The testing environment runs from a temp folder instead of from wherever the exported game would normally be. If you put 6.gif in the localappdata folder for the game, that should work, though.
 
N

nicei7

Guest
Of course I have 6.gif in my c:\Project2.gmx directory.
Long time ago I use use some code to open gif file like this with file_open and then use sprite_add to work.
 
N

nicei7

Guest
is there a method to use gif or png files to my project or not? sprite_add is not work because "C:\Project2.gmx\6.gif" is not work. I don't know how to get path to my gif file.
Can I use ini or file_open or some other GM function to solve this problem. GM can use C++ code, or ini, difficult things. And I am stypid and can't upload and use some gif file.
 

Tthecreator

Your Creator!
What you should do instead is add the file 6.gif to your included files.
Then you should just be able to use:

i7=sprite_add("6.gif",1,0,0,0,0)
 
N

nicei7

Guest
It is not work again with i7=sprite_add("6.gif",1,0,0,0,0).

I have example gmk
spr_player=sprite_add(working_directory+"\gmlt.png",1,0,0,0,0)
sprite_index=spr_player

Because working_directory is wrong and not the same like Project2.project.gmx or this example. How to change working_directory.
if use in Gm working_directory.= "C:\Project2.gmx\", can,t change it.
 

FrostyCat

Redemption Seeker
Add that file as an Included File, simply dropping it into the project directory won't do anything.
 
N

nicei7

Guest
What to do to work? I have file 6.gif in my directory where is Project2.project.gmx file. Is there examples with sprite_add without working_directory to know is this work or not?
What is "Included File"?
 

FrostyCat

Redemption Seeker
Let me say it one more time: Simply dropping it into the project directory won't do anything.

Add your 6.gif as an included file. If you don't know what that means, read the Manual. Once that is done, sprite_add(working_directory + "6.gif", 1, 0, 0, 0, 0) would work.
 
N

nicei7

Guest
I use sprite_add(working_directory + "6.gif", 1, 0, 0, 0, 0) and this is not work because every time when start GM to test my project, GM create random working_directory name. create names like
C:\Users\win\AppData\Local\gm_ttt_31978\gm_ttt_61120
C:\Users\win\AppData\Local\gm_ttt_31978\gm_ttt_65367


Cant, work. I have draw_text(100,y,working_directory) and working directory every time when start game is change number.
Draw text every time when start game is not same. Working_directory is temporary and change name.
 
N

nicei7

Guest
It works but have a problem.
 
Last edited by a moderator:
N

nicei7

Guest
When change my image 5.gif with another picture when start program there is only previous 5.gif, not new. I want when change my 5.gif with other and start game to change picture.
 
W

Wraithious

Guest
Once you change the sprite with sprite_add the original sprite is gone until you restart the game. You must make an empty sprite (create new sprite, go to file bar and select new sprite which makes an empty sprite 32x32 by default, leave it like that and name it sprite_empty or whatever) now use that sprite to load your new sprite into, and when you want to go back to the original sprite, your original sprite will be there.
 
N

nicei7

Guest
You don't understand me. I want when use including files and gif 5.gif, when change gif in my game directory in C:, when start game to change 5.gif in my game. Also if I have many gifs in my game, when start game with including files all gifs are in my Ram Memory and not in my game directory.

How to refresh image files when use it with including files?

Now when click in GM, then click in included files and click in 5.gif is open windows photo review program and can refresh it.

I can refresh gif with GM, but if I create Game can't refresh it in game.
Where is Difference between Sprites and Including files, if I can't refresh my gif files and gif Sprites are in my exe file, but not in my game directory?
 

otterZ

Member
Let me say it one more time: Simply dropping it into the project directory won't do anything.

Add your 6.gif as an included file. If you don't know what that means, read the Manual. Once that is done, sprite_add(working_directory + "6.gif", 1, 0, 0, 0, 0) would work.
This is helpful Frosty Cat.
(This reply was originally longer with a question I decided to move to a separate thread - about whether assigning an external sprite to different variables would mean having to clean up each variable with sprite_delete, then after shortening this post I noticed TheouAegis's excellent explanation and reply below which answers this question)
 
Last edited:

TheouAegis

Member
spr in your code would hold the index of the sprite asset created by sprite_add(). So if you have 30 sprites in your project by default and then add a new sprite using sprite_add(), the value of spr will be 30. Likewise, the value of global.variable will be 30. Whether you did sprite_delete(spr), sprite_delete(global.variable), or sprite_delete(30), it wouldn't matter -- they'd all have the same effect of deleting the sprite asset with index 30. None of these would change the value of spr or global.variable -- both would still be set to 30 even after using sprite_delete(). There would be no memory leak, because no data has been made irretrievable.
 

otterZ

Member
Thank you, TheouAegis, I just noticed your reply after I shortened a rather lengthy post I made but moved to a separate thread.
Your reply makes perfect sense and now gives me the confidence to assign the external sprite as a reference value within different variables and be sure that sprite_delete has done its job.
This is great and thank you for taking the time to explain this to me.
 
Top