Forum rules - please read before posting.

Referencing script elements from a Custom Action

edited October 2017 in Technical Q&A
I have started writing various scripts and custom actions and have a specific question regarding accessing scripts from actions.

I have attached a Script component to a Hotspot which declares some properties (variables) which I can manipulate from PlayMaker (works fine):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetHotspotVars : MonoBehaviour {
  public int NextAction;
  public int NextID;

  // Use this for initialization
  void Start () {
    NextAction = 0;
    NextID = 0;
  }
}

The Hotspot's "Use" interaction calls an ActionList which has one parameter, a Gameobject (meaning the ActionList should receive the Hotspot as a parameter).

I then wrote a Custom Action which is designed to ask me for an AC variable from the list of existing variables, a script name and a property name during edit and when running, it should seek the property within the script and transfer its value into the selected AC variable.

All the GUI code is working fine (I used many of the existing ActionLists to see how to do this, perhaps you could add more comments into these sometime to make it easier for people new to AC's classes?) and the code to set the AC variable should work fine also. My questions are the following:

1) How do I refer to the Hotspot object that has been passed onto the ActionList, like this within AssignValues (List<ActionParameter> parameters), like this:

theHotspot = GetParameterWithID (parameters, 0);

2) Once I have this, I suppose I can refer to the script this way:

theScript = theHotspot.Find(scriptname) // scriptname is the name of script specified at edit

3) Once I have the script referenced, how can I refer back to its properties (variables)?

Can I write theScript.SetHotSpotVars.NextAction ? Probably not, as theScript is not defined until runtime.


Thanks for any help with this. By the way, this should make things much easier than using arrays to reference the hotspots as I had initially planned.

Comments

  • 1) How do I refer to the Hotspot object that has been passed onto the ActionList?

    GetParameterWithID returns an ActionParameter class instance - you'll need to then read that parameter's gameObject value, and get the Hotspot component from it:

    ActionParameter parameter = GetParameterWithID (parameters, _parameterID);
    if (parameter != null && parameter.parameterType == ParameterType.GameObject)
    {
      theHotspot = parameter.gameObject.GetComponent <Hotspot>();
    }



    2) Once I have this, I suppose I can refer to the script this way:

    This is more a general Unity scripting question, but you can use:

    theScript = theHotspot.GetComponent (scriptname);

    However, if you don't need to access the Hotspot class directly, you can skip it in 1):

    theScript = parameter.gameObject.GetComponent (scriptname);


    3) Once I have the script referenced, how can I refer back to its properties (variables)?

    Again, this is a general scripting question, and you should refer to Unity's documentation for more detailed advice.  You have a number of choices, but I'd recommend you go with interfaces since they allow you to make assumptions about the class beforehand.
  • Thank you Chris for your great feedback. I know most of this was Unity related, but as I'm just starting with the platform and trying to differentiate what is AC and what is Unity based, it really helps.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.