GML [Solved] Add to Sprite During Runtime

J

Jackson Oliver

Guest
Hello! I'm new to the Game Maker platform.

  • Windows 10
  • Game Maker 2.2.3.436 | Runtime 2.2.3.344

I'm trying to figure out how I can load (at runtime) images into a sprite. This code does do that, but I want to be able to load lots of images (from disk) into the sprite frames, instead of having to make a tilemap and specifying the amount.

Code:
var createdSprite = sprite_add("workingset/example.png", 1, false, false, 0, 0);
sprite_assign(sprite_index, createdSprite);
I'm wonder if theres a way to either create a tilemap from images then load it into a sprite, or construct a sprite then recursively add images to the sprite frames.

Maybe something like this:

Code:
var createdSprite = sprite_add("workingset/example0.png", 1, false, false, 0, 0);

sprite_add_frame(createdSprite, "workingset/example1.png");//made up function as an example
sprite_add_frame(createdSprite, "workingset/example2.png");//made up function as an example
sprite_add_frame(createdSprite, "workingset/example3.png");//made up function as an example

sprite_assign(sprite_index, createdSprite);
In short:
I would like a way to take a bunch of images, and load them into a sprite.


Thanks for helping in advance!
 

Greenwitch

Member
Hello! I'm new to the Game Maker platform.

  • Windows 10
  • Game Maker 2.2.3.436 | Runtime 2.2.3.344

I'm trying to figure out how I can load (at runtime) images into a sprite. This code does do that, but I want to be able to load lots of images (from disk) into the sprite frames, instead of having to make a tilemap and specifying the amount.

Code:
var createdSprite = sprite_add("workingset/example.png", 1, false, false, 0, 0);
sprite_assign(sprite_index, createdSprite);
I'm wonder if theres a way to either create a tilemap from images then load it into a sprite, or construct a sprite then recursively add images to the sprite frames.

Maybe something like this:

Code:
var createdSprite = sprite_add("workingset/example0.png", 1, false, false, 0, 0);

sprite_add_frame(createdSprite, "workingset/example1.png");//made up function as an example
sprite_add_frame(createdSprite, "workingset/example2.png");//made up function as an example
sprite_add_frame(createdSprite, "workingset/example3.png");//made up function as an example

sprite_assign(sprite_index, createdSprite);
In short:
I would like a way to take a bunch of images, and load them into a sprite.


Thanks for helping in advance!
A simple loop would do the trick.

Code:
for (var i = 1; i <= 5; i++) {
   sprite_add_frame(createdSprite, "workingset/example" + string(i) + ".png");
}
 
J

Jackson Oliver

Guest
Hello, you must have misunderstood me.

Code:
sprite_add_frame();
was somthing I made up to discribe somthing that might be avalible.
Im trying to load lots of separate images into a sprite during runtime. Ive been looking and couldnt find anything that can give me that end result.
 

Bruchpilot

Member
Edit: Ah, I misread.

If you want to add to an existing sprite, you can create another with sprite_add() and then merge them with sprite_merge().
 
Last edited:
J

Jackson Oliver

Guest
thank you for your response, I have the following code that works and does what I want.

Code:
// scr_merge_sprite
/// merges 2 sprites

// take arguments
var path = argument[1];
var startSprite = argument[0];

// create and merge
var createdSprite = sprite_add(path, 1, false, false, 0, 0);
sprite_merge(startSprite, createdSprite);

return startSprite;
and I call it using
Code:
// how I call it
var newSprite = sprite_add("workingset/example0.png", 1, false, false, 0, 0);
newSprite = script_execute(scr_merge_sprite, newSprite, "workingset/example1.png");
newSprite = script_execute(scr_merge_sprite, newSprite, "workingset/example2.png");
newSprite = script_execute(scr_merge_sprite, newSprite, "workingset/example3.png");
newSprite = script_execute(scr_merge_sprite, newSprite, "workingset/example4.png");
It works fine and does exactly what I want, but is there a better way to do it or is this how I should be doing it?
 

TsukaYuriko

☄️
Forum Staff
Moderator
You could use an external texture packing software (which usually generates a map file that translates specific sprite/frame identifiers to coordinates on the texture) to get this working as smoothly as possible - place related sprites onto a texture page, let the packer optimize the placement to save as much space as possible, then dynamically load the entire generated texture page via sprite_add and handle drawing the correct bits via the generated map file.

Beware that the sprite_add* family of functions create a new texture page every time they're called, which is not only a killer for performance (depending on what's drawn), but also memory usage (depending on how much of it is loaded at the same time). The manual makes no mention of texture pages being merged, so I'm afraid that's not the magical shortcut it seems to be - at least not if you're concerned about performance and efficiency in the long run.
 
Top