Spawn object at random position on another object (Solved)

In my game, I’m making a function that creates a special effect.
I would like for the function to repeatedly spawn sparkles on another object at a random position to make it look like the object is sparkling all over.
However, when I tried it, it only repeatedly created the sparkles in the center of the object, not all over.
My question is how do I make it so the sparkles create themselves in random positions, but ONLY within the host object’s sprite?

Also note, the host object is actually an object group since this function will pertain to multiple objects (Each different sizes), not just one single object.

Here is a screenshot of my function. I just need to know what specific parameter goes into the .X and .Y so that it is positioned within the sprite of each object in my group.

Thanks in advance.

You have already you answer in your question.

Use random in a range.
Your range is the bounding box of the object.

And you know already where you should write your expression.

If you don’t know how is writtent a expression don’t worries search it in the expression list in GD.
Tips : Prefer use random integer instead random float.
And the dimensions of the object and positions are useful for define a bounding box.

1 Like

Thanks. However, the targeted object is actually an entire object group. Some of the objects in the group are bigger or smaller than the object I’m currently working with. If I use the RandomInRange for the bounding box for one object, would the RandomInRange scale up or down to the bigger and smaller objects in my group?

Also, I tried your suggestion and it didn’t work.

What do I do for this? Remember, this is for MULTIPLE objects of different sizes you should use for each to apply your actions on each instance in the group individually. Then do like Bouh said. You didn’t do it correctly,as you try to give the function that gets X an argument, where the random should have the boundaries in argument.

FrozenGuy is the group of objects ?

You have found the correct function for random, it’s a first step.
But indeed you use wrong way.
You say it yourself, this must be functionnal for multiples objects with differents size. But in your random function i see you hardcoded your values between 0 and 32.
You have said this must be functionnal with multiple object when use the dimensions of the objects.
Here if your objects are in group FrozenGuy use FrozenGuy position & dimensions (remember the bounding box in my previous comment)

I guess the correct way to create the sparkles is this:

Sorry if it’s not correct.

1 Like
min = 0
max = object width
object position X + random(min, max)


min = object position X
max = object position X+object width
random(min, max)

I tried your suggestion, but it didn’t work. The sparkles aren’t positioned on top of any of the enemies, just all over the screen.

However, I tried the other option on the top of your most recent suggestion…

…and it ALMOST works! The sparkles are positioned on each enemy I have on screen, and they even scale up accordingly to their size, width, height, EVERYTHING.
However, there is one small problem left… the sparkles are positioned to the bottom right side of the pivot point of my sprites, so the sparkles are a little bit off centered.

This is probably a stupid question, but how do I center the sparkles? Do I put “center” or “centre” in the parameter of FrozenGuy.X()?

Thanks btw :slight_smile:

1 Like

Using this way, you actually want the X and Y to point to the bottom left. No idea how you can do it except modifying the origin point (not sure if that actually exists I don’t use GDevelop to create real games so I neve used such advanced features)

Neverind, I realized that I had the “Center” point on top of the “Origin” point for this sprite. It works perfectly now. Thanks mate. I think you should make an example of this in the next update since the information was so helpful!

1 Like