Example of replacing external events by a function

External events can be a good way of sharing/reusing events in your game. Functions are going one step further as they are able to receive parameters, for example objects.

Example using external events

Let's say we have this event to position objects on a virtual grid:

You could use them then by including them in your scene:

The only problem is that they are not really reusable: the only objects that will have their position aligned with the virtual grid are objects called “Tile”. What if instead you had a function that you could call with the object that you want to snap to the grid?

Example with a function

Instead, we can declare a function with a parameter, the object to snap:

We called the object “Tile”, but we could have changed the name for anything more generic!

It's then super simple to call the function in your events. It's available as a new action!

The advantage is that you can now use this new action to snap any object to the grid. You could even extract it and reuse it in another game!

Turns out that this was already done, there is a useful extension “Snap Objects to a virtual grid” providing a similar action.

Learn more about functions here.