Beginner Question - A player node with multiple units that can move independently

Godot Version

4.3

Question

In 2D top-down

I have a player node:
The player node has it’s own set of variables such as movement speed and radius
So this player can move around as the main control but doesn’t actually consist of anything, or do anything - It has a collider to prevent the main player from moving through walls etc
The radius of this player node sets a boundary for which multiple sub units can move around in
Each sub unit has it’s own properties, colliders etc, they generally follow the main player movement but have their own programming to do certain functions
Each sub unit should not collide/overlap with other sub units

  1. What is the best way to arrange this in the node tree?
  2. If they get spawned overlapping an existing unit, will they automatically separate?

Thanks in advance - I’ll be around to reply to responses

It is virtually impossible to answer this question because the answer depends on so many unknowns.

For instance, what sorts of behaviours are you expecting your sub units to do? Are they always limited in area to what you are calling the ‘player node’.

I think you should consider your units as actors. They should all have their own states and state machine to control their behaviours and all be their own self contained scenes. The player can then control any of these actors (through a UI or dragging around the ‘focal point’ for their activities). Some of your actor states will be ’ return to the focal area’, or ‘move away from other actors’ etc. So if an actor is chopping down a tree, say, it can finish chopping and then return to the focal area before deciding on the next action to take.

As I said, so many unknowns and so many possibilities. I hoped that helped in some way.

Paul

I certainly agree that the player should be a scene and each unit should be a scene. Absolutely each unit will have it’s own states etc.

To answer your question, the units won’t venture out of the player range but they could accidentally find themselves out of range if they are involved in a collision and the player moves away. In this case they would need some kind of path-finding to get back to the player.

Right now, I can’t even get another unit inside the player node.

The player node draws fine by itself but as soon as I add a sub unit as a node inside it, nothing draws

Game Dev, especially when you are learning, can be very frustrating. Keep with it, you will get there in the end :slight_smile:

Is it even allowed to have a CharacterBody2D inside a CharacterBody2D?

I would imagine so, but why? I know you are thinking of your sub-units as sub units but I would not put them as children of the player node. I would imagine a set up like this:

main_game
– Environment
– Player
– SubUnits
– – Worker
– – Digger
– – Fighter
– UI
…etc

So make a player scene. Get it to move around with whatever inputs you want. Add the entire Player scene to your main Game as the Player node.

Then make a sub unit. Get it working so when played on its own you can tell it to to dig, roam, or whatever states you have created. You could give it a target variable that it uses as a base position to head to, or keep in range of. Then add that to your main scene SubUnits node. When you are testing it the target you can set manually to say Vector2(300,300) so it wanders around that one point in the screen.

Then in your Game node, when gameplay_started = true (say) you can update the actors target nodes with the player position.

For instance, I don’t know, this is all very high level.

Just get one bit working first, before moving onto something else. For instance do you have an environment that the player can move around in?

Or try making something more simple to begin with.

Firstly, thanks a lot for that really detailed answer.

I was able to get everything working really well. Currently I have the units as children of the parent which is working fine but I’m open to consider other set ups.

Is there a specific reason why you wouldn’t put the units as children of the player?