this post was submitted on 07 Oct 2024
1 points (100.0% liked)

Godot: The open source game engine

21 readers
1 users here now

A community for discussion and support in development with the Godot game engine.

founded 1 year ago
MODERATORS
 
This is an automated archive made by the Lemmit Bot.

The original was posted on /r/godot by /u/KetsuiReddit on 2024-10-07 06:36:33+00:00.


Hello, I am buildig my first game in Godot. It is a platformer game where the player can die either:

  • When the timer reach 0 seconds
  • When he touches spikes in the level

For the timer, I am just sending a signal to the player when the timeout happen

However, I don't know what to do for when the player touches the spikes. There are many ways to achieve what I want. They all work but Idk which one is the best practice in terms of game dev with godot.

1. In Spikes.cs , when the player enter in the Area3D, I use:

    public void OnBodyEntered(Node3D body)
    {
        if (body.HasMethod("Die"))
        {
            body.Call("Die");
        }
    }

Not a huge fan of this one since it seems to be using reflection. A better version I found:

    public void OnBodyEntered(Node3D body)
    {       
        if (body is player player)
        {
            player.Die();
        }
    }

This one seems to be the best version so far.

2. Event bus

It was a bit hard to create in C# since most tutorials are doing it using godot but I managed to make it work. This way is good but the player needs to subscribe/unsubscribe to a global script which creates a strong dependency between the player and a singleton. So it will be harder to tests and every time I change/reload a scene, I must make sure the player unsubscribe.

3. Connect through scripts

The thrid one attempt was to connect with scripts. So basically, on the OnReady method of the Spike.cs, I find the player and create the same setup as the timer. I just do this using scripts because they will be a lot of spikes in the level.

4. Detect collision in the Character.cs

If I do this, I only have one script running checking for collision instead of multiple one. However, I did not find the signal OnBodyEnteredOnBodyEntered in the editor for the CharacterBody3d. Plus player hitbox can change when the player crouch so it complicates things.

I would appreciate some guidelines here please

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here