C# Actions

So, you’ve probably done this at-least once as a developer, you’ve got a value or element of your game that needs regularly checking and updating, like; player health, score or if a level has ended. Now you can just check for these in update which in small games is fine, but you’ll find you are calling this code way more than you really need to, if only there was a way to eliminate that xD.

Introducing Actions

This is where actions come into play. They work like events such as the OnClick on a Unity UI button. When you invoke them they run, otherwise to stay as disabled. What makes them handy, like how you can have multiple methods run at once of a button is that you can add and remove methods or delegations on the fly as well as listen for actions being invoked from other scripts. Here’s the four main steps to using actions in you code:

  • Initialise the action, you can pass varied parameters into the action to pass through information you may need to use in the actions subscribers.
// Its recommended to make the action null be default, 
// as you can check for this when you call the action. 
public Action<float, float> OnHealthChange = null;
  • Subscribe our method or delegate our code to run at this point. Methods are easier to manage and are generally recommended, but if you just need to do a quick value change a delegate will work too. Its normally best to do this when the action has no parameters though. It is important to note that your method or delegation must have the same parameters defined in the initialisation.
// Subscribing to the action 
OnHealthChange += UpdateHealthBar;
// The method setup, note the parameters are the same as the action init
private void UpdateHealthBar(float health, float maxHealth)
  • Invoke the action, doing this will call all the methods subscribed to the action.
// Invoking the action... (NOTE: that we call with a null check ("?.") 
// So it doesn't invoke if nothing is subscribed to it. 
OnHealthChange?.Invoke();
  • Un-Subscribe from the action to avoid null exception errors. Recommended to run this on OnDisable or similar to avoid errors.
// Un-subscribes from the action 
OnHealthChange -= UpdateHealthBar;

Usage Example

Here’s an example which is what I used to learn how to use actions.

Health.cs (on a player)

using System;
using UnityEngine;
public class Health : MonoBehaviuor
{
	[SerialzeField] private float maxHealth = 10f;
	private float currentHealth = 10f;
	// init actions
	public Action <float, float> OnHealthChange = null;
	public Action OnNoHealth = null;
	
	public void TakeDamage(float dmg)
	{
		currentHealth -= dmg;
		// Invoking action....
		OnHealthChange?.Invoke(currentHealth, maxHealth);
		if (currentHealth <= 0)
			OnNoHealth?.Invoke();
	}
}

Healthbar.cs (on the health bar UI for the player)

using System;
using UnityEngine;
public class HealthBar : Monobehaviour
{
	[SerialzeField] private Image hpImage;
	
	private Health health;
	private void OnDisable()
	{
		// Un-subscribing from action...
		health.OnHealthChange -= UpdateHPBar;
	}
	private void Start()
	{
		// subscribing to action
		health.OnHealthChange += UpdateHPBar;
		hpSlider = GetComponent<Slider>();
	}
	// Method that has subscribed to the OnHealthChange method
       // Note: It has the same parameters!
	private void UpdateHPBar(float currentHealth, float maxHealth)
	{
		hpSlider.fillAmount = currentHealth / maxHealth;
	}
}

End of 2021 Plans

End of 2021 Plans

With the end of the year fast approaching, we feel it is a good time to announce our plans for the rest of the year. We achieved most of our plans from the summer roadmap, though it did take longer than we initially planned. Because of this we are behind on making a new game project which we will be postponing until the new year.

Spooky Logo

You may have also noticed a logo change recently, this is our yearly bit of fun where we make logo variations based on the time of year. With Halloween fast approaching, we felt we needed to make the logo a little spooky for the fun of it.

The Carter Games 2021 Halloween Banner

The Plan

Those of you who follow our social media will know that we have had a few new assets in the works for a while now. Well we plan to finish these up and release them before the year is up. Those who have looked at our Unity Asset Store page recently will note that the Leaderboard Manager is still out of date. So this is also on the plate for the end of year work. We held off on the Leaderboard Manager for a bit as the asset needed a total re-write, so the new version will be a 2.0.0 release as and when we release it.

So a breakdown of what each asset does and when to expect the update roughlty:

Leaderboard Manager | End of year

Leaderboard Manager is a scripting system that allows users to implement leaderboard into their games with reletive ease. The current asset works, but is rather basic and has some limitations that we would like to remove. Given the amount of work needed to make the Leaderboard Manager work as we want. We expect this asset to be the last one we release this year.

Drop Tables | November

This asset is one of the new ones. The idea is to provided users with a way of creating multiple loot tables with ease with a 1 in X style which are user friendly. So a change of 1 in 4 would be common while 1 in 5000 would be super rare for example. The asset also allows the user to define drops that always get rolled if needed. The asset is currently at the testing stage and we are working out the kinks with the backend to make it usable in projects of any kind of the gate. We expect this asset to be ready sometime in November.

Build Versions | End of October

This is a lightweight asset that is designed to just help the developer automate updating their game build numbers. Which will happen automatically each time the developer makes a build. We plan to have options to change when the build is incremented, options to edit the player settings x.x.x number and options to edit the “type” of build. All of data is available in a scriptable object which can be easily implemented into the developers project. This asset is the one we are currently focusing our efforts and we expect this asset to be out by the end of October at the latest.

Closing Notes

So that is the plan 😀 Whether or not we achieve it is another question entirely. But we hope that getting these assets out this year will clear our schedules a little so we can start some new projects going into the new year.