Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update some C# examples for 4.0 #6693

Merged
merged 2 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions tutorials/2d/2d_transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ way:
.. code-tab:: csharp

var localPos = new Vector2(10,20); // local to Control/Node2D
var ie = new InputEventMouseButton();
ie.ButtonIndex = (int)ButtonList.Left;
ie.Position = (GetViewportTransform() * GetGlobalTransform()).Xform(localPos);
var ie = new InputEventMouseButton()
{
ButtonIndex = MouseButton.Left,
Position = GetViewportTransform() * (GetGlobalTransform() * localPos),
};
GetTree().InputEvent(ie);
14 changes: 7 additions & 7 deletions tutorials/2d/custom_drawing_in_2d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ redrawn if modified:

using Godot;

public partial class CustomNode2D : Node2D
public partial class MyNode2D : Node2D
{
private Texture _texture;
public Texture Texture
Expand Down Expand Up @@ -133,7 +133,7 @@ call ``queue_redraw()`` from the ``_process()`` callback, like this:
// Your draw commands here
}

public override void _Process(float delta)
public override void _Process(double delta)
{
QueueRedraw();
}
Expand Down Expand Up @@ -387,7 +387,7 @@ using ``get_node()``.

using Godot;

public partial class CustomNode2D : Node2D
public partial class MyNode2D : Node2D
{
private float _rotationAngle = 50;
private float _angleFrom = 75;
Expand Down Expand Up @@ -421,7 +421,7 @@ calls ``_draw()``. This way, you can control when you want to refresh the frame.

.. code-tab:: csharp

public override void _Process(float delta)
public override void _Process(double delta)
{
_angleFrom += _rotationAngle;
_angleTo += _rotationAngle;
Expand Down Expand Up @@ -490,10 +490,10 @@ smaller value, which directly depends on the rendering speed.

.. code-tab:: csharp

public override void _Process(float delta)
public override void _Process(double delta)
{
_angleFrom += _rotationAngle * delta;
_angleTo += _rotationAngle * delta;
_angleFrom += _rotationAngle * (float)delta;
_angleTo += _rotationAngle * (float)delta;

// We only wrap angles when both of them are bigger than 360.
if (_angleFrom > 360 && _angleTo > 360)
Expand Down
32 changes: 16 additions & 16 deletions tutorials/3d/using_transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ A default basis (unmodified) is akin to:

// Instead we can use the Identity property.
var identityBasis = Basis.Identity;
GD.Print(identityBasis.x); // prints: (1, 0, 0)
GD.Print(identityBasis.y); // prints: (0, 1, 0)
GD.Print(identityBasis.z); // prints: (0, 0, 1)
GD.Print(identityBasis.X); // prints: (1, 0, 0)
GD.Print(identityBasis.Y); // prints: (0, 1, 0)
GD.Print(identityBasis.Z); // prints: (0, 0, 1)

// The Identity basis is equivalent to:
var basis = new Basis(Vector3.Right, Vector3.Up, Vector3.Back);
Expand Down Expand Up @@ -161,9 +161,9 @@ It is possible to rotate a transform, either by multiplying its basis by another
Vector3 axis = new Vector3(1, 0, 0); // Or Vector3.Right
float rotationAmount = 0.1f;
// Rotate the transform around the X axis by 0.1 radians.
transform.basis = new Basis(axis, rotationAmount) * transform.basis;
transform.Basis = new Basis(axis, rotationAmount) * transform.Basis;
// shortened
transform.basis = transform.basis.Rotated(axis, rotationAmount);
transform.Basis = transform.Basis.Rotated(axis, rotationAmount);

A method in Node3D simplifies this:

Expand Down Expand Up @@ -246,7 +246,7 @@ Imagine you need to shoot a bullet in the direction your player is facing. Just
.. code-tab:: csharp

bullet.Transform = transform;
bullet.LinearVelocity = transform.basis.z * BulletSpeed;
bullet.LinearVelocity = transform.Basis.Z * BulletSpeed;

Is the enemy looking at the player? Use the dot product for this (see the :ref:`doc_vector_math` tutorial for an explanation of the dot product):

Expand All @@ -261,8 +261,8 @@ Is the enemy looking at the player? Use the dot product for this (see the :ref:`
.. code-tab:: csharp

// Get the direction vector from player to enemy
Vector3 direction = enemy.Transform.origin - player.Transform.origin;
if (direction.Dot(enemy.Transform.basis.z) > 0)
Vector3 direction = enemy.Transform.Origin - player.Transform.Origin;
if (direction.Dot(enemy.Transform.Basis.Z) > 0)
{
enemy.ImWatchingYou(player);
}
Expand All @@ -281,7 +281,7 @@ Strafe left:
// Remember that +X is right
if (Input.IsActionPressed("strafe_left"))
{
TranslateObjectLocal(-Transform.basis.x);
TranslateObjectLocal(-Transform.Basis.X);
}

Jump:
Expand All @@ -299,7 +299,7 @@ Jump:

// Keep in mind Y is up-axis
if (Input.IsActionJustPressed("jump"))
velocity.y = JumpSpeed;
velocity.Y = JumpSpeed;

velocity = MoveAndSlide(velocity);

Expand Down Expand Up @@ -341,12 +341,12 @@ Example of looking around, FPS style:
if (@event is InputEventMouseMotion mouseMotion)
{
// modify accumulated mouse rotation
_rotationX += mouseMotion.Relative.x * LookAroundSpeed;
_rotationY += mouseMotion.Relative.y * LookAroundSpeed;
_rotationX += mouseMotion.Relative.X * LookAroundSpeed;
_rotationY += mouseMotion.Relative.Y * LookAroundSpeed;

// reset rotation
Transform3D transform = Transform;
transform.basis = Basis.Identity;
transform.Basis = Basis.Identity;
Transform = transform;

RotateObjectLocal(Vector3.Up, _rotationX); // first rotate about Y
Expand Down Expand Up @@ -377,12 +377,12 @@ Converting a rotation to quaternion is straightforward.
.. code-tab:: csharp

// Convert basis to quaternion, keep in mind scale is lost
var a = transform.basis.Quaternion();
var b = transform2.basis.Quaternion();
var a = transform.Basis.GetQuaternion();
var b = transform2.Basis.GetQuaternion();
// Interpolate using spherical-linear interpolation (SLERP).
var c = a.Slerp(b, 0.5f); // find halfway point between a and b
// Apply back
transform.basis = new Basis(c);
transform.Basis = new Basis(c);

The :ref:`class_Quaternion` type reference has more information on the datatype (it
can also do transform accumulation, transform points, etc., though this is used
Expand Down
15 changes: 6 additions & 9 deletions tutorials/best_practices/data_preferences.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions tutorials/best_practices/godot_interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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");
}

Expand All @@ -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());
}
};

Expand Down
8 changes: 3 additions & 5 deletions tutorials/best_practices/godot_notifications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -187,7 +185,7 @@ instantiation:
set
{
_test = value;
GD.Print("Setting: " + _test);
GD.Print($"Setting: {_test}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion tutorials/inputs/handling_quit_requests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ program termination, you should send the notification yourself:

.. code-tab:: csharp

GetTree().Root.PropagateNotification((int)NotificationWmCloseRequest)
GetTree().Root.PropagateNotification((int)NotificationWmCloseRequest);

Sending this notification will inform all nodes about the program termination,
but will not terminate the program itself *unlike in 3.X*. In order to achieve
Expand Down
Loading