|
1 |
| -# gms-struct-event |
2 |
| - Adds a event structure for the game engine GameMaker Studio 2 |
| 1 | +Event is a package for your project, with a handy implementation of trees for GameMaker Studio 2 using constructors. |
| 2 | +The package was originally written for the personal work of TornadoTech projects, but was uploaded to GitHub for the convenience of all developers. |
| 3 | + |
| 4 | +To create an event, you must create a new instance of the ``Event`` constructor, |
| 5 | +which will allow us to carry out further manipulations. |
| 6 | +You can also create global events in global variables, which can be very useful. |
| 7 | +```js |
| 8 | +var event = new Event(); |
| 9 | +global.event = new Event(); |
| 10 | +``` |
| 11 | + |
| 12 | +To subscribe to an event, the ``connect`` method is used; it also returns the function being subscribed. |
| 13 | +This allows you to not separate methods for subscription into separate variables, |
| 14 | +but to declare and sign them in one place. |
| 15 | + |
| 16 | +And to trigger an event, just call the ``invoke`` method. (We'll talk about his arguments later) |
| 17 | +```js |
| 18 | +var event = new Event(); |
| 19 | + |
| 20 | +var func1 = event.connect(function() { |
| 21 | + show_debug_message("Called func 1!"); |
| 22 | +}); |
| 23 | + |
| 24 | +var func2 = event.connect(function() { |
| 25 | + show_debug_message("Called func 2!"); |
| 26 | +}); |
| 27 | + |
| 28 | +var func3 = event.connect(function() { |
| 29 | + show_debug_message("Called func 3!"); |
| 30 | +}); |
| 31 | + |
| 32 | +event.connect(function() { |
| 33 | + show_debug_message("Anonymous function"); |
| 34 | +}); |
| 35 | + |
| 36 | +event.invoke(); |
| 37 | +// Result: |
| 38 | +// Called func 1! |
| 39 | +// Called func 2! |
| 40 | +// Called func 3! |
| 41 | +// Anonymous function |
| 42 | +``` |
| 43 | + |
| 44 | +If we need to unsubscribe an event, we can use ``disconnect``, but for this we must save a reference to the method. |
| 45 | +This means that it will not be possible to unsubscribe from specific anonymous methods, |
| 46 | +which is why they are anonymous. |
| 47 | +Also, ``disconnect`` returns the result of whether it was possible to unsubscribe from the event or not. |
| 48 | + |
| 49 | +**It is also IMPORTANT for you to remember that if you signed a listener, |
| 50 | +then you need to delete it so that memory leaks do not occur. |
| 51 | +For example: if you subscribe to the global translation update event in an object, |
| 52 | +then you need to unsubscribe in the object’s delete method.** |
| 53 | +```js |
| 54 | +var event = new Event(); |
| 55 | + |
| 56 | +var func1 = event.connect(function() { |
| 57 | + show_debug_message("Called func 1!"); |
| 58 | +}); |
| 59 | + |
| 60 | +var func2 = event.connect(function() { |
| 61 | + show_debug_message("Called func 2!"); |
| 62 | +}); |
| 63 | + |
| 64 | +var func3 = event.connect(function() { |
| 65 | + show_debug_message("Called func 3!"); |
| 66 | +}); |
| 67 | + |
| 68 | +event.connect(function() { |
| 69 | + show_debug_message("Anonymous function"); |
| 70 | +}); |
| 71 | + |
| 72 | +event.disconnect(func2); |
| 73 | +event.disconnect(func3); |
| 74 | +event.invoke(); |
| 75 | +// Result: |
| 76 | +// Called func 1! |
| 77 | +// Anonymous function |
| 78 | +``` |
| 79 | + |
| 80 | +The ``disconnect_all`` method will allow you to unsubscribe from all listeners, |
| 81 | +including anonymous listeners. |
| 82 | +This functionality is very specific and sometimes I can't find a use for it myself, |
| 83 | +but it can be extremely useful in specific situations. |
| 84 | +```js |
| 85 | +var event = new Event(); |
| 86 | + |
| 87 | +var func1 = event.connect(function() { |
| 88 | + show_debug_message("Called func 1!"); |
| 89 | +}); |
| 90 | + |
| 91 | +var func2 = event.connect(function() { |
| 92 | + show_debug_message("Called func 2!"); |
| 93 | +}); |
| 94 | + |
| 95 | +var func3 = event.connect(function() { |
| 96 | + show_debug_message("Called func 3!"); |
| 97 | +}); |
| 98 | + |
| 99 | +event.connect(function() { |
| 100 | + show_debug_message("Anonymous function"); |
| 101 | +}); |
| 102 | + |
| 103 | +event.disconnect_all(); |
| 104 | +event.invoke(); |
| 105 | +// Result: |
| 106 | +``` |
| 107 | + |
| 108 | +As stated before, the ``invoke`` method accepts an unbounded number of arguments to be sent as listener arguments. |
| 109 | +**It is IMPORTANT** to remember that the method signature must be the same as what you pass to ``invoke``, |
| 110 | +otherwise you will get an error or your code will not work correctly. |
| 111 | + |
| 112 | +``invoke_for_array`` works identically to ``invoke``, but instead of a set of arguments, |
| 113 | +it accepts 1 array, which will be expanded and substituted into the listener's arguments. |
| 114 | +```js |
| 115 | +var event = new Event(); |
| 116 | + |
| 117 | +event.connect(function(a, b) { |
| 118 | + show_debug_message($"{a} + {b} = {a + b}"); |
| 119 | +}); |
| 120 | + |
| 121 | +event.connect(function(a, b) { |
| 122 | + show_debug_message($"{a} - {b} = {a - b}"); |
| 123 | +}); |
| 124 | + |
| 125 | +event.invoke(10, 20); |
| 126 | +event.invoke(-23, 1); |
| 127 | +event.invoke_for_array([1, 7]); |
| 128 | + |
| 129 | +// Result: |
| 130 | +// 10 + 20 = 30 |
| 131 | +// 10 - 20 = -10 |
| 132 | +// -23 + 1 = -22 |
| 133 | +// -23 - 1 = -24 |
| 134 | +// 1 + 7 = 8 |
| 135 | +// 1 - 7 = -6 |
| 136 | +``` |
0 commit comments