the delegates have not yet understood what they can serve, I read about books, websites but I do not know how to use them properly in games. Do you have any examples?
It depends on what you want to do. Delegates are just a structure for functions and methods. For example, you can store a method in a variable and call it using the delegate structure. Hope this helps. Thanks.
151/5000 Sincerely, I'm not clear yet. I'm used to using methods and variables normally. I have never used delegates and events in the games field
Events and Delegates are useful. For example you can use a delegate to specify what a back button should do: Code (CSharp): Action backButtonDelegate; if (gameIsRunning) { backButtonDelegate = PauseGame(); } else if (isPaused) { backButtonDelegate = ResumeGame(); } Button myBackButton = GetBackButton(); myBackButton.onClick.AddListener(backButtonDelegate); Here are some useful docs: Delegates (C# Programming Guide) Using Delegates (C# Programming Guide) When to Use Delegates Instead of Interfaces (C# Programming Guide) Events (C# Programming Guide) How to: Subscribe to and Unsubscribe from Events Hope this helps. Thanks.
It is confusing. Microsoft made up the name Delegate just for C# - the name everyone else uses is "function pointers" or just "variables that can stand for functions." Microsoft also uses the word delegate in three different, similar ways AND there's a "design pattern" named delegate. So it's a hard word to look up and get consistent answers. I haven't seen any of the links above. Well some. But a text-hooky explanation is at http://www.taxesforcatses.com/TOC.shtml and go down to the 2nd part where it says "function pointers" (it's a PDF.)
Simplest example I can come up with on how to use delegates: Code (csharp): public class PlayerStats { // Create an event that contains no delegate to avoid null checking before calling it public event Action<int> OnHealthChanged = delegate{}; private int _health; public int Health { get { return _health; } set { _health = value; OnHealthChanged(_health); } } public class HealthBar : MonoBehaviour { private PlayerStats _stats; private Slider _slider; private void Start() { _slider = GetComponent<Slider>(); _stats = //.... assign it somehow - maybe find a Player gameobject and get stats from it _stats.OnHealthChanged += PlayerHealthChanged; } private void PlayerHealthChanged(int newValue) { _slider.value = newValue; } } Disclaimer: May or may not compile as I typed this by hand. You should get the idea, though. Action/Func are a much nicer way to handle delegates rather than using the (IMO) clumsy delegate keyword. But yes, as said, delegates are just another way to say function pointers. Create events, or pass as a parameter to functions to use as a callback method.