Update some C# examples for 4.0 (#6693)

* Update some C# examples

- Rename members that have been renamed in Godot's C# API for 4.0.
- Change `delta` parameter type to `double`.
- Ensure parameters match base declaration.
- Other minor code fixes.

---------

Co-authored-by: Paul Joannon <437025+paulloz@users.noreply.github.com>
This commit is contained in:
Raul Santos
2023-02-04 17:03:03 +01:00
committed by GitHub
parent d6b4fe8ab9
commit b319da3f07
30 changed files with 236 additions and 219 deletions

View File

@@ -249,28 +249,25 @@ tree structures.
.. code-tab:: csharp
using Godot;
using System.Collections.Generic;
// Can decide whether to expose getters/setters for properties later
public partial class TreeNode : Object
public partial class TreeNode : GodotObject
{
private TreeNode _parent = null;
private object[] _children = new object[0];
private List<TreeNode> _children = new();
public override void Notification(int what)
public override void _Notification(int what)
{
switch (what)
{
case NotificationPredelete:
foreach (object child in _children)
foreach (TreeNode child in _children)
{
TreeNode node = child as TreeNode;
if (node != null)
node.Free();
node.Free();
}
break;
default:
break;
}
}
}

View File

@@ -26,8 +26,8 @@ is to get a reference to an existing object from another acquired instance.
.. code-tab:: csharp
Object obj = node.Object; // Property access.
Object obj = node.GetObject(); // Method access.
GodotObject obj = node.Object; // Property access.
GodotObject obj = node.GetObject(); // Method access.
The same principle applies for :ref:`RefCounted <class_RefCounted>` objects.
While users often access :ref:`Node <class_Node>` and
@@ -181,31 +181,35 @@ Nodes likewise have an alternative access point: the SceneTree.
.. code-tab:: csharp
public class MyNode
using Godot;
using System;
using System.Diagnostics;
public class MyNode : Node
{
// Slow
public void DynamicLookupWithDynamicNodePath()
{
GD.Print(GetNode(NodePath("Child")));
GD.Print(GetNode("Child"));
}
// Fastest. Lookup node and cache for future access.
// Doesn't break if node moves later.
public Node Child;
private Node _child;
public void _Ready()
{
Child = GetNode(NodePath("Child"));
_child = GetNode("Child");
}
public void LookupAndCacheForFutureAccess()
{
GD.Print(Child);
GD.Print(_child);
}
// Delegate reference assignment to an external source.
// Con: need to perform a validation check.
// Pro: node makes no requirements of its external structure.
// 'prop' can come from anywhere.
public object Prop;
public object Prop { get; set; }
public void CallMeAfterPropIsInitializedByParent()
{
// Validate prop in one of three ways.
@@ -223,7 +227,15 @@ Nodes likewise have an alternative access point: the SceneTree.
return;
}
// Fail with an exception.
if (prop == null)
{
throw new InvalidOperationException("'Prop' wasn't initialized.");
}
// Fail and terminate.
// Note: Scripts run from a release export template don't
// run `Debug.Assert` statements.
Debug.Assert(Prop, "'Prop' wasn't initialized");
}
@@ -232,10 +244,10 @@ Nodes likewise have an alternative access point: the SceneTree.
// that manage their own data and don't interfere with other objects.
public void ReferenceAGlobalAutoloadedVariable()
{
Node globals = GetNode(NodePath("/root/Globals"));
MyNode globals = GetNode<MyNode>("/root/Globals");
GD.Print(globals);
GD.Print(globals.prop);
GD.Print(globals.my_getter());
GD.Print(globals.Prop);
GD.Print(globals.MyGetter());
}
};

View File

@@ -119,7 +119,7 @@ deltatime methods as needed.
{
// Called every frame, even when the engine detects no input.
public void _Process(float delta)
public void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_select"))
GD.Print(delta);
@@ -130,12 +130,10 @@ deltatime methods as needed.
{
switch (event)
{
case InputEventKey keyEvent:
case InputEventKey:
if (Input.IsActionJustPressed("ui_accept"))
GD.Print(GetProcessDeltaTime());
break;
default:
break;
}
}
@@ -187,7 +185,7 @@ instantiation:
set
{
_test = value;
GD.Print("Setting: " + _test);
GD.Print($"Setting: {_test}");
}
}