Subscribing Methods to Actions
Quick guide to making pre existing actions call methods automatically when triggered by the base game
Last updated
Quick guide to making pre existing actions call methods automatically when triggered by the base game
Last updated
Gungeon comes with a large number of events and actions already set up, subscribing methods to events is what enables a large amount of items more complex than a simple passive stat modifier to exist. Here I'll walk you through making a simple bullet modifier as well as the general principles involved
The first step in subscribing an action to a method is identifying which parameters the action will output, we need to know this because the arguments of methods subscribed always need to match with the action's output. Lucky for us finding this information is as simple as hovering over the action with the cursor in VisualStudio
here we are looking at the PostProcessProjectile action, which is triggered by the game when a projectile is fired by the player and is one of the easiest and most common ways to handle projectile modifiers. The parameters inside Action<>, in this case, Projectile and float, are the ones we need to know.
An action will call all methods subscribed to it with its parameters as arguments, for this reason, it is important that they match exactly.
Here we're simply increasing the projectile's size, so even though we have no need of the float, which for this action is the base chance for a projectile to trigger an effect like say, frost bullets', it must be part of the method's parameters regardless. Likewise, the method cannot have any extra parameters.
Now that we have a valid method all that we need to do is simply attach it to the action through the use of the += operator. When done our method is successfully subscribed to the PostProcessProjectile action and will be called every time the action is triggered. Methods can be subscribed anywhere, but when making passive items Pickup is a common place for it.
Note how the method does NOT have any arguments being passed to it, that is because we are not calling the method directly and the action will take care of that. You CANNOT pass arguments to a method (at least when subscribing it this way).
Once subscribed, a method will be called every time the action is triggered, but there are a variety of reasons why you could want that to stop being the case, a common one would be stopping a projectile modifier from being applied when the item that grants it is dropped. Unsubscribing methods can be done through the use of the -= operator.
Methods MUST be unsubscribed manually or they will keep being called when an action is triggered even after the item is dropped.