Writing a custom Action 2

In the previous tutorial, we created a custom Action that affects the Intensity value of any Light in our scene. However, it will only work for objects that exist in the scene in Edit mode: it will not work on prefabs that are instantiated during gameplay, nor will it work when used in an ActionList asset file. We will fix this now by using Constant IDs.

Constant IDs are Adventure Creator's way of keeping track of GameObjects between scenes. When the ConstantID script is attached to a GameObject or prefab, it assigns itself a Constant ID number, which we can incorporate into our Action. This will allow our Action to work when asset files are involved.

We'll first declare a new integer, to record the ID number, among the existing two:

public Light light;
public int constantID;
public float intensity;

When we assign the Light compoent in the Action's GUI, we want to update this ID number reference automatically. AC provides a variant of Unity's ObjectField functions to handle this automatically. In ShowGUI, we can replace the ObjectField call with ComponentField:

override public void ShowGUI ()
{
	ComponentField ("Light:", ref light, ref constantID);
	intensity = EditorGUILayout.FloatField ("Intensity:", intensity);
}

This will cause a Constant ID to be created and recorded if appropriate. Try placing a scene-based GameObject into the GameObject to affect field of an asset-based instance of the Action. You'll see that a Constant ID number will be automatically generated and displayed in the GUI:

To have the Constant ID number be used at runtime to re-assign the rigidbody variable, we need to declare an AssignValues function, and have it call the AssignFile function:

override public void AssignValues ()
{
	light = AssignFile (constantID, light);
}

Our Action is now set up to work with asset files. In the next tutorial, we'll upgrade it further to accept parameters.

The full script is as follows:

using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

	[System.Serializable]
	public class ActionSetLightIntensity : Action
	{
		
		public override ActionCategory Category { get { return ActionCategory.Object; }}
		public override string Title { get { return "Light intensity"; }}
		public override string Description { get { return "Sets a Light's intensity value."; }}

		public Light light;
		public int constantID;
		public float intensity;


		override public void AssignValues ()
		{
			light = AssignFile (constantID, light);
		}


		public override float Run ()
		{
			if (light)
			{
				light.intensity = intensity;
			}
			return 0f;
		}

		
		#if UNITY_EDITOR

		public override void ShowGUI ()
		{
			ComponentField ("Light:", ref light, ref constantID);
			intensity = EditorGUILayout.FloatField ("Intensity:", intensity);
		}

		#endif
		
	}

}