User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 : signals

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 signal gif 10b

Dot net 8 Csharp testing with Godot : signals

 

In this article, i will explain what is a signal used in godot engine to connect to a C# method.

 

 

About the author...

ident021 180 facebook linkedin insider mcp learn metadatas sans cravate 128x128 autocolors 18052006 191123 mcse microsoft logomcsa microsoft logomcdba logo

since 2001, Insider since 2014.

 

Thanks to Microsoft EMEA GBS to let me participate in the year 2014 to the Insider project during my work. Then, This Windows test project, here in version 11, is based on the cycle of the insider Preview Builds, now in the Dev Channel: it is fun to test the new features. I can learn new features, validate them and share them with you :-)

 

This tutorial is made in a test environment, for pleasure and not to earn money. It is a non-profit project. It shows that the concept is possible, for a demontration, artistic, or educational purposes, school learning course. Don't test in a real environment in production.

 

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 : signals, os testing in the last article

 

The Windows 11 Operating System version 23606 was tested in the last article. Check article version 26502 here . Now that operating system is ready, i can test an application.

 

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 : signals,  application testing

 

Now, OS Windows 11 version 23606 is ready, so i can test the middleware apps, frameworks...

 

Recently, i saw a cool session by Michael Hawker at .NET Conf 2023 about creating a game with C# and a middleware called "Godot engine".

 

And i said to myself : that’s could be interesting to test this godot engine. it’s an application to test if this new version of the  Windows operating system works like a charm. And, with Godo we can create C# programs to do funny things, animations, instead of creating « business » applications less funy.

 

The idea is simple: you buy a Windows 11 PC at the supermarket, you plug it, and you can create cool things like this :-)

 

Csharp testing on Windows 11 version 23606 with Dot net 8.0.100 and Godot engine 4.2.1: signals, last articles

 

Read this article to see how to install Godot engine

Last time i didn’t explain very well how i built the scene with the bouncing ball. I received messages asking me to explain with more details how i’ve done this. So this time i will explain step by step how to build the scene.

 

 

 

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1: signals, the red ball scene

 

 

I launch the Godot engine application by double clicking the Godot_v4.2.1-stable_mono_win64.exe.

The Godot project manager is launching. I clic « new » to create a new project.

By default, the renderer is « mobile » : it’s nice for this C# test godot signal project.

I choose the name « BouncingBallSignal » . Be carefull when choosing the name of your project, beacause last time i included a # , C# , and there was a failure at the compilation, so now i ovoid specials caracters.

 

 Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine create a new project

I hit the « Create and edit » button.

The Godot game engine user interface launchs. I get the user interface of the godot application engine. The application is ready now. good job!

I see the label: "Create a root node".

There is three categories of nodes: blue for 2D scene, red node for 3D scene, green node for UI user interface.

I click on the red node to create a 3D Scene. I understand that it is my first object who will contain child objects.

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine create root node

 

So i create a child node by left clicking on the Node3D and +Add Child Node.

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine add a child node

 

I double click « Node3D » and rename it « Node3D Scene RedBall »

I create a child node by calling it "MeshInstance3D".

In this new node, in the inspector, mesh section, i attached a new sphereMesh.

I can edit the new sphere object with the inspector. by opening the surface material override, on the <empty> section i add New and StandardMaterial3D. I click on this new StandardMaterial3D and in the inspector secion albedo, i change his color to red by using HEX ff0000 for example.

I save this scene and i call it « redball ». the « redball.tscn » file appreas in the FileSystem.

Now, i create a New scene : in the scene menu, i clic « new scene ».

I select 3D Node, i double click the Node3D and rename it « Scene Root ».

I create the ground. New child object, MeshInstance3D. And in his inspector mesh section, i choose « PlaneMesh ». I rename it « MeshInstance3D Ground »


MeshInstance3D ground > inspector > scale : x=2 y=2 z=2
MeshInstance3D ground > inspector > transform : y=0

Now, i add in this new scene the ball i created : i drag’n drop it from the FileSystem dock in thisnew scene.

The « Node3D » (redball) appears in the scene. I rename « Node3D » in « RedBall »

I move the ball position y=2m

I click the « play icon » in the upper left corner (Run the project, play the project).

Godot ask me « No main scene has been defined, select one ? (You can change it later in « Project Settings »under the « application » category) ». I click « Select current », and root.tscn.

I see a pop up and a blanck screen, because, there is no camera and no light.

I close this pop up.

In the Root scene, i add a child node, « Camera3D »
I move this « Camera3D » for a translation of Z=8 by dragging the object ad moving it with Shift down.
I move this « Camera3D » for a translation of y=1, in the inspector/Transform.


In the Root scene, i add a child node, « DirectionalLight3D »
I move this « DirectionalLight3D » for a translation of Z=2 by dragging the object ad moving it with Shift down. Or inspector. Rotation X=-45.

Now i’ll show the Camera3D view.

I click the « view » button and select « 2 viewports ». I select the Camera3D node, and in the bottow view, and click the « preview » button.

In the inspector (Camera3D selected) i change the FOV 75 to 30.

I want to add gravity to the ball.

Now i edit the RedBall scene by clicking the little triangle icon in the same line of the RedBall object.

I add a child node to the Node3D RedBall:
-The rigid body, RigiBody3D (to have forces applied to this object)

I add a child node to the RigiBody3D :
-The collider, CollisionShape3D (So that this object can interact with other objects). This CollisionShape3D need to be a child of the RigiBody3D.

In the CollisionShape3D, Inspector, shape, i choose « NewSphereShape3D »
I adjust the NewShpereShape3D to match the MeshInstance3D RedBall object

I go back to the main scene by clicking the « root » in the viewport.

I want to see the hierarchy of the ball scene. So i rightclick on the « Redball » node, and select the « editable children » option.

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine redball in rootscene

 

To enable gravity, the MeshInstance3D node must be a child of the RigidBody3D. We can do that by editing the Redball scene.

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine MeshInstance3D child of RigidBody3D

 

I click the « play icon » in the upper left corner (Run the project, play the project).

Now i see that the ball is falling because of the gravity.


The ground needs physics too in order to see the ball bouncing on it.

I add a StaticBody3D to be the parent of the MeshInstance3D ground object (to be static, not be moved by the gravity).
I add the CollisionShape3D as a child of the StaticBody3D.
I choose the shape BoxShape3D of the CollisionShape3D in the inspector, physics material override.
I adjust the Boxshape 3D to match the MeshInstance3D ground object
(transform, position, y=-05m)

I click the « play icon » in the upper left corner (Run the project, play the project).

The ball is stopped by the ground.

Now, i need to make the ground type « bouncy ».

On FileSystem, res://, right click and create new, resource. Select : PhysicsMaterial and i name it bouncy.tres .

On FileSystem, i select bouncy.tres file, and in the inspector, i assign bounce=0.9

I select the groung StaticBody3D and in physics_mateial_oveide i click <empty> and load : bouncy.tres

I click the « play icon » in the upper left corner (Run the project, play the project).

The ball now fall on the ground and bounces a few times and stops.


Now i want to add a signal, so that, each time the Red Sphere touch the ground, it write the message « Boing it bounces ! » in the console.


I will choose to use the signal _on_body_entered() of AREA3D.

I add a child node object Area3D to the RigidBody3D RedBall.
I add a child node object CollisionShape3D to this Area3D object.

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine CollisionShape3D child of Area3D

 

I select this Area3D of the RedBall. Right clic, attach script

Language : C#

Path : res://Area3D.cs

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine attach script Area3D

 

Note : the class you wish to attach to your node should have the same name as the .cs file.

->Keep in mind that the class you wish to attach to your node should have the same name as the .cs file. Otherwise, you will get the following error:

"Cannot find class XXX for script res://XXX.cs"

https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_basics.html

The script opens in visual studio code. A question comes : « Do you trust the authors of the files in this folder ? » I must trust the author and i click this button.

The Godot engine automatically create the *.sln et the *.csproj , nice!

Now i see this code :

 

using Godot;

using System;

 

public partial class Area3D : Godot.Area3D

{

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

 

    }

 

    // Called every frame. 'delta' is the elapsed time since the previous frame.

    public override void _Process(double delta)

    {

    }

}

 

Now, the node contains a C# script, so i can add my signal method.

 

I create the callback method (called Slot) so, when the signal is emitted, the slot method is called.

 

I add the method « _on_body_entered() » : my C# script looks like that now :

 

using Godot;

using System;

 

public partial class Area3D : Godot.Area3D

{

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

 

                GD.Print("ConsultingIT.fr is starting the simulation");

 

    }

 

        void _on_body_entered(Node root)

    {

        GD.Print("Boing it bounces!");

    }

 

    // Called every frame. 'delta' is the elapsed time since the previous frame.

    public override void _Process(double delta)

    {

    }

}

 

 

I add this line to see when the script starts :

 

                GD.Print("ConsultingIT.fr is starting the simulation");

 

I select RigidBody3D RedBall / Area3D. In Inspector, i switch to node, and select the signals tab.

 

I right  click the body_entered signal and « connect » and this window pop up :

 

G:\teamviewer\Windows-11-Csharp-godot\signals\Csharp testing on Windows 11 version 23620 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine attach signal body_entered.JPG

 

I notice the receiver method is « _on_body_entered » ( that’s why i inserted it in my C# script.)

 

I click « connect »

 

I run the scene and each time the ball hits the ground, the signal is emited and a line « Boing it bounces ! » arrives in the console.

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1: add a pause in the game

 

I need to add a pause before the scene plays. 

 

I try to add a readResult to read a key from the keyboard :

 

        string? readResult;

        bool validEntry = false;

        Console.WriteLine("Press a key to start the demo");

        do

        {

            readResult = Console.ReadLine();

            if (readResult != null)

            {

 

                GD.Print("ConsultingIT.fr is starting the simulation");

 

            }

        } while (validEntry == false);

 

I compile, run the scene, and the debug screen freezes…

 

What’s wrong?

 

So i decide to ask Copilot « in godot engine how to add a pause before scene rendering »

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1: asking Copilot

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1 godot engine ask copilot add a pause

 

AI-generated code. Review and use! Awesome!

Copilot answers me with a C# script sample 

To pause the game, you need to set the pause mode. This is done by calling SceneTree.set_pause(true) with a true argument. Here’s an example of how you can do it in your script:

 

// In your script (e.g., attached to a Node or a RigidBody3D):

var tree = GetTree();

tree.SetPause(true);

 

Next, in copilot, i read :

« Remember that pausing the game affects all processing, including rendering. Adjust the pause mode according to your specific requirements. For more details, you can refer to the official Godot documentation on pausing games1. Happy game development! »

 

So i can browse the official Godot documentation

 

https://docs.godotengine.org/en/stable/tutorials/scripting/pausing_games.html 

 

I read the better way is to create a menu with a pause/unpause button

 

In my Root scene i create a new Node : Control

In this Control node, i create a child node : Button.

I rename It Button Pause

In the inspector, i add the text property: Pause

In the signals node, i attach the button_down() signal with the Receiver Method : _on_button_pause_button_down

On the button node, i attach the script ButtonPause.cs

I add the method _on_button_down with 

var tree = GetTree();

tree.SetPause(true);

 

In this Control node, i create a child node : Button.

I rename It Button Play

In the inspector, i add the text property: Play

In the inspector, i add the text property: Transform/Position X=100, and the Node process mode to « When Pause » (Because when the Pause Button is pressed, all nodes are freezed);

In the signals node, i attach the button_down() signal with the Receiver Method : _on_button_play_button_down

On the button node, i attach the script ButtonPlay.cs

I add the method _on_button_down with 

var tree = GetTree();

tree.SetPause(true);

Now i want the game be paused as soon as it starts.

In the ButtonPlay.cs i add a tree.Paused=true ;

Done

 

 

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1: resize debug window

 

i ask Copilot and it answers me that :

To resize the debug window in Godot Engine, follow these steps:

Open your project in the Godot editor.

Go to Project Settings (usually found under the “Scene” menu or by pressing F8).

In the Display section, navigate to Window.

Adjust the Size settings by specifying the desired width and height for the debug screen.

Save your changes.

This will modify the resolution of the debug screen within the IDE without affecting your project layout size. Now you can better match your target resolution while testing your game! 🎮

 

Default :1152x648

I put 640x480  

  

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1: signals,done

 

Done! We can use C# in Godot 4.2.1 on windows 11 version 23606 to create a scene with a signal. A signal is a method called by the godot scene in the C# script when an action is detected. We call that a "callback" pattern like in javascript.

Csharp testing on Windows 11 version 26052 with Dot net 8.0.100 and Godot engine 4.2.1: signals, need help ?

 

Need help to use godot engine on Windows 11 or write C# code?

Fill this form or send an email on loic @consultingit.fr and i will be happy to help you :)

 

using Godot;

using System;

public partial class Area3D : Godot.Area3D

{

    // Called when the node enters the scene tree for the first time.

    public override void _Ready()

    {

    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.

    public override void _Process(double delta)

    {

    }

}