[SOLVED] How to combine separate images of noses, eyes, mouth, and overall face together randomly

:wave:

I’m trying to do something similar to Papers, Please: Drawing Faces and am reading Lua random image on how to script it. How would you set up the separate facial parts in Defold, then have the game mix them to create new faces, how would you code the placement of the facial parts for instance? All I can think of is importing images and having separate atlases for each of the facial parts, then write in a separate script to randomly grab an id from one of the tiles in the atlas, then smush them altogether somehow and have that be a sprite for a game object?

Is there a way to reference atlases as variables in Defold? Like local AtlasReference = insert way to grab atlases.

Thanks in advance to anyone who replies, I appreciate it.

I would use either one game object with multiple sprites (nose, eyes, ears, hair, mouth etc) or multiple game objects with one sprite each.

I would use one or more atlases (depending on how many images you need) and use consistent naming (nose_1, nose_2, nose_3 etc).

I would use sprite.play_flipbook() to set the sprite image for each part.

-- pick a random nose image from the atlas
local nose = "nose_" .. math.random(1, 3)

-- set the image on the sprite (assuming one game object with many sprites)
sprite.play_flipbook("face#nose", nose)

Thank you britzl, I’ll get back to you on the results after I manage to try to implement what you’re saying. :+1: But I’m curious, how would you grab the atlas from the code, if it’s possible, or is there a different way.

Not sure I understand what you mean here? If you layout your sprites (on one or more game objects) in a collection and assign each sprite a default image from an atlas then there is no need to “grab the atlas”. The sprite already has an atlas assigned. All you need to do is pick an image from the atlas.

Ah I see, and that’s what the

local nose = "nose_" .. math.random(1, 3)

is for. I’m slightly confused (probably due to being new to Lua), how does that piece of code know where it’s grabbing the image from, because from what I’m understanding, it’s creating a local variable, and that it’s randomly choosing from 1 to 3, so is “nose_” the name of the image? Or the name of the sprite?

If you create an atlas and add a bunch of different noses named nose_1.png, nose_2.png, nose_3.png etc these will become available for use by sprites as flipbook animations (even if they are only a single image). The name of the flipbook animation will be the name of the image (without file extension). This means that the atlas will contain three “animations” named nose_1, nose_2, nose_3.

This line of code results in a string with either “nose_1”, “nose_2” or “nose_3”, assigned to the variable nose:

local nose = "nose_" .. math.random(1, 3)

Next we use this string when we play the “flipbook animation” on the sprite, effectively picking the correct image from the atlas.

sprite.play_flipbook("face#nose", nose)

Edit:

"face#nose"

This assumes a game object with id face and a sprite with id nose.

2 Likes

Alright, thank you so much for the explanation and help. :+1: I will return to this thread, if I am snagged or I manage to implement what you said.

1 Like