Create multiple objects that circle around a centre object


#1

I’m trying to create 3 objects “graveskull” that moves around in a circular pattern based on a central object “skullposition” and to spawn multiple copies of “skullposition”.

1st issue
I can only get one copy of “graveskull” to rotate at one time

2nd issue
When another copy of “skullposition” (green square) spawns the “graveskull” disappears from the 1st “skullposition” appears on the next.

Example screen of what I want to achieve

Events


#2

So, let me try to sum that up… you want for each skullposition, to repeat three times the following: create a graveskull and put it around skullposition at an angle of (spawnangle+=120)°. And then, for each graveskull, you want to put graveskull around skullpos at (angle+=5).
Is that it? Then do it :stuck_out_tongue:


#3

I don’t know how to create multiple copies of “graveskull” objects and put them around “skullposition” at specific angles for example object 1 at 120° around centre object, object 2 at 240° and 3 at 360°?

also I don’t know how to have multiple copies of “skullposition” active at the same time without the “graveskull” objects resetting to the next “skullposition” created which also causes the rotation speed of “graveskull” to double?

seems like allot of events required overly complicated for what I want to achieve or I’m going about this the wrong way?

The action works but I don’t know how add more than one “graveskull” or the initial position in degrees around “skullposition”


#4

All that I wrote in italics is really what you need to use inside GDevelop. You just need to modify the variables I wrote inside ( ) in a separate action, because you can’t change their value in the “put object around” action like I did.

Maybe you’re not aware of the Repeat event, though:
image


#6

I think you need to take a step back and think about the high level design of what you are trying to accomplish. I will refer to your rotating skull thing as an “entity” below.
Think of it in phases:
1 - Spawn (this is done once for each entity)
2 - Animate / Move (this is done every frame for each entity)
3 - Clean up - (This is done whenever you want an entity to go away)

Spawn
You could make this a function, or you can set a global variable, but the most important part is that this code only executes once when you create a new entity. You will likely just pass in a position. Or a skullpos object to center around. This is where you will call all of the create calls on the skulls and put them at the starting positions or set and instance variable on them with their starting angle. I reccommend also setting an instance variable called id on each skullpos, and an instance vriable on each skull object called “parent” which refers to the id of the skullpos it belongs to. Remeber this code should only run once per new object… maybe a big “TriggerOnce” block.

Animate / Move
This is done every frame, which is the default for all the code blocks. Use a Foreach skullpos object like Gruk explained. Move each skullpos if they are moving around. Then add an inner For each skull object where “skull.parent == skullpos.id”, and bump the angle of each of those skulls and reposition them around the skullpos.

Clean Up
When removing a skullpos from the scene, your can use a single condition like “skull.parent == skullpos.id” to select all the skulls associated with the skullpos and then call delete on “skull” and it will apply to all of them. Them you can delete the skullpos.

These are just design tips. It’s best to really think about these types of things before you start coding, but I often code first too, so no shame!


#9

Thanks for your reply… I got this to work as intended but using allot of events. I also had to make duplicates of graveskull as I don’t know how use instance variables on an object or parent and child objects.

The other issue is creating more than one instance of skullposition without having to duplicate all the events for each instance.

Events

Any suggestion to do this with less events would be helpful or how to create more than one instance of skullposition without duplicating the same events.


#10

Indeed, your solution is far from ideal.
Here’s the correct answer:

Looking back at my first message, I should have mentioned that you need 1. to link the objects if you have several skullpos, and 2. use object variables to avoid value conflicts between objects.