c_sharp_features.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. .. _doc_c_sharp_features:
  2. Features
  3. ============
  4. This page provied an overview over the commonly used features of both C# and Godot
  5. and how they are used together.
  6. Type Conversion and Casting
  7. ---------------------------
  8. C# is a statically typed language. Therefore you can't do the following:
  9. .. code-block:: csharp
  10. var mySprite = GetNode("MySprite")
  11. mySprite.SetFrame(0)
  12. The method ``GetNode()`` returns a ``Node`` instance.
  13. You must explicitly convert it to the desired derived type, ``Sprite`` in this case.
  14. For this, you have various options in C#.
  15. **Casting and Type Checking**
  16. Throws ``InvalidCastException`` if the returned node cannot be casted to Sprite.
  17. You would use it instead of the ``as`` operator if you are pretty sure it won't fail.
  18. .. code-block:: csharp
  19. Sprite mySprite = (Sprite)GetNode("MySprite");
  20. mySprite.SetFrame(0);
  21. **Using the AS operator**
  22. The ``as`` operator returns null if the node cannot be casted to Sprite,
  23. and for this reason it cannot be used with value types.
  24. .. code-block:: csharp
  25. Sprite mySprite = GetNode("MySprite") as Sprite;
  26. // Only call SetFrame() is mySprite is not null
  27. mySprite?.SetFrame(0);;
  28. **Type checking using the IS operator**
  29. To check if the node can be casted to Sprite, you can use the ``is`` operator.
  30. The ``is`` operator returns false if the node cannot be casted to Sprite,
  31. otherwise it returns true.
  32. .. code-block:: csharp
  33. if (GetNode("MySprite") is Sprite)
  34. {
  35. // Yup, it's a sprite!
  36. }
  37. For more advanced type checking, you can look into `Pattern Matching <https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching>`_.
  38. Signals
  39. -------
  40. For a complete C# example, see the **Handling a signal** section in the step by step :ref:`doc_scripting` tutorial.
  41. You can use ``Connect("SomeSignal", someObject, "SomeMethod")`` to connect to signals.
  42. ``AddUserSignal("SignalName")`` is used to define custom signals.
  43. Emitting signals is done with ``EmitSignal("SignalName")``. Params can be given, like ``EmitSignal("SignalName", arg1, arg2, ...)``.
  44. **Custom signals**
  45. .. code-block:: csharp
  46. :emphasize-lines: 5, 10
  47. :linenos:
  48. public class ExampleScript : Node
  49. {
  50. public override void _Ready()
  51. {
  52. AddUserSignal("YourSignal");
  53. }
  54. public override void _Process(float delta)
  55. {
  56. EmitSignal("YourSignal");
  57. }
  58. }
  59. Above in line 5, ``AddUserSignal()`` is used to define the new custom signal ``YourSignal``.
  60. In line 10 ``EmitSignal()`` is used to emit that custom signal on every frame in ``_Process()``.
  61. Make sure that ``AddUserSignal()`` is always executed before any calls using that signal (``EmitSignal()`` and ``Connect()``).
  62. If you are using both ``AddUserSignal()`` and ``Connect()`` or ``EmitSignal()`` in ``_Ready()``, this is especially important as load order of your node may change,
  63. and thus the order in which your various ``_Ready()`` functions are called.