decryptedchaos / GodotCSharpTemplate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Template

The template I use when starting a new Godot 4 C# game.

Feature Summary

FAQ

Q: Why am I not seeing any messages appear in console?

A: The AddMessage(...) function has been commented out 3 times in Logger.cs because what if a project is using GodotUtils submodule where the Logger.cs is defined but it's not using Template where UIConsole.Instance.AddMessage(...) is defined. I have not found a solution to this so for now please just uncomment the calls to AddMessage(...) in Logger.cs.

Setup

  1. Download and install the latest Godot 4 C# release
  2. Clone this repository with all its submodules
git clone --recursive https://github.com/ValksGodotTools/Template

Main Menu

1

Options

2

Credits

3

Hotkeys

Untitled

Old Video Previews
2023-04-23.11-50-35.mp4
2023-04-24.13-51-07.mp4

Console Commands

// Simply add the "ConsoleCommand" attribute to any function
// it will be registered as a new console command

// Note to bring up the console in-game press F12

[ConsoleCommand("help")]
void Help()
{
    IEnumerable<string> cmds =
        UIConsole.Instance.Commands.Select(x => x.Name);

    Logger.Log(cmds.Print());
}

// Console commands can have aliases, this command has a
// alias called "exit"

[ConsoleCommand("quit", "exit")]
void Quit()
{
    GetTree().Root.GetNode<Global>("/root/Global").Quit();
}

// Method parameters are supported

[ConsoleCommand("debug")]
void Debug(int x, string y)
{
    Logger.Log($"Debug {x}, {y}");
}

Prefabs

// Load all your scene prefabs here. This script can be found in
// "res://Scripts/Static/Prefabs.cs". Note that music and sounds are
// loaded in very similarily and these scripts can be found in the
// static folder as well.

// NOTICE:
// I realize now that Godot only loads a resource one time no matter
// how many times it's used in different export variables so this
// way of doing things may likely be scrapped in the future
public static class Prefabs
{
    public static PackedScene Options { get; } = Load("UI/options");

    static PackedScene Load(string path) =>
        GD.Load<PackedScene>($"res://Scenes/Prefabs/{path}.tscn");
}

// Prefabs are instantiated like this
UIOptions options = Prefabs.Options.Instantiate<UIOptions>();

AudioManager

// Play a soundtrack
AudioManager.Instance.PlayMusic(Music.Menu);

// Play a sound
AudioManager.Instance.PlaySFX(Sounds.GameOver);

// Set the music volume
AudioManager.Instance.SetMusicVolume(75);

// Set the sound volume
AudioManager.Instance.SetSFXVolume(100);

// Gradually fade out all sounds
AudioManager.Instance.FadeOutSFX();

SceneManager

// Switch to a scene instantly
SceneManager.Instance.SwitchScene("main_menu");

// Switch to a scene with a fade transition
SceneManager.Instance.SwitchScene("level_2D_top_down", 
    SceneManager.TransType.Fade);

Experimental EventManager

This is one way of programming events, it may not be the best way but there is no harm in trying it out.

In most tutorials about this on the internet you will find that they pass in a Dictionary<string, string> for the event params. This is very ugly but with the use of dynamic now you can pass in any kind of object you want and have beautiful params if you so desire.

Event Enums

public enum EventGeneric
{
    OnKeyboardInput
}

public enum EventPlayer
{
    OnPlayerSpawn
}

Event Dictionaries

public static class Events
{
    public static EventManager<EventGeneric> Generic { get; } = new();
    public static EventManager<EventPlayer> Player { get; } = new();
}

Example #1

Events.Generic.AddListener(EventGeneric.OnKeyboardInput, (args) => 
{
    GD.Print(args[0]);
    GD.Print(args[1]);
    GD.Print(args[2]);
}, "someId");

Events.Generic.RemoveListeners(EventGeneric.OnKeyboardInput, "someId");

// Listener is never called because it was removed
Events.Generic.Notify(EventGeneric.OnKeyboardInput, 1, 2, 3);

Example #2

Events.Player.AddListener<PlayerSpawnArgs>(EventPlayer.OnPlayerSpawn, (args) => 
{
    GD.Print(args.Name);
    GD.Print(args.Location);
    GD.Print(args.Player);
});

Events.Player.Notify(EventPlayer.OnPlayerSpawn, new PlayerSpawnArgs(name, location, player));

Contributing

Any kind of contributions are very much welcomed!

Projects Coding Style

If you have any questions, talk to me over Discord (valk2023)

About

License:MIT License


Languages

Language:C# 100.0%