Getting a custom Action to work with parameters

If you haven't already, please follow the previous two tutorials on custom Actions before continuing.

An advanced feature of Adventure Creator is the ability to replace fields within Actions with parameters, allowing us to change them dynamically at runtime. In this tutorial, we'll give our custom Action the ability to accept a GameObject parameter in place of the field we have in the GUI.

Parameters involve the use of Lists, which means we must first include a new library. In our script's header, add the following:

using System.Collections.Generic;

We'll store the parameter as an ID number, much the same way as we record a Constant ID. However, we must ensure it's set to -1 by default. Add to the list of declared variables:

public int parameterID = -1;

Our ShowGUI function's header must be amended to accept a list of parameters.

override public void ShowGUI (List<ActionParameter> parameters)

We will then call a function that will display a parameter selector, if any are available. If one is chosen, we will reset the other variables - otherwise, we'll display the GUI as normal:

parameterID = Action.ChooseParameterGUI
       ("GameObject to affect:", parameters, parameterID, ParameterType.GameObject);
if (parameterID >= 0)
  constantID = 0;
  objectToAffect = null;
  objectToAffect = (GameObject) EditorGUILayout.ObjectField
       ("GameObject to affect:", objectToAffect, typeof (GameObject), true);
  constantID = FieldToID (objectToAffect, constantID);
  objectToAffect = IDToField (objectToAffect, constantID, true);

Finally, we'll update the AssignValues function to also accept parameters, and re-assign the GameObject if appropriate:

override public void AssignValues (List<ActionParameter> parameters)
  objectToAffect = AssignFile (parameters, parameterID, constantID, objectToAffect);

Our Action is now complete - it will work with GameObjects, prefabs and parameters.

Adventure Creator and this website are copyright Chris Burton, ICEBOX Studios

Go to top
Do NOT follow this link or you will be banned from the site!