Forum rules - please read before posting.

Could someone provide an example of how to trigger/run an ActionList from script?

These seems like it should be super easy and straighforward, but I'm not finding a direct answer...

I'm hoping to find something akin to:

"using.AC;

...

void RunAnActionList(){
----- what goes here ??? -------
}
"
And how do I identify the target ActionList?

Regards context: AC fires off a conversation in Pixel Crushers Dialogue System, from DS Sequence call the RunAnActionList method. The purpose of the method is to add an item to the Player's AC Inventory (bonus point for directly explaining how to fire an ActionList from DS from Sequencer. Super Mega Bonus Points for adding inventory item directly from DS Sequencer.)

Thanks so much for any and all help!

Comments

  • edited January 2022

    To run an ActionList through script, call its Interact function.

    See the Manual's "Interaction scripting" chapter for an overview of what's available. For a more detailed look, see the Scripting Guide's entry on the ActionList class.

    And how do I identify the target ActionList?

    This is more of a general C# question, but the most straightforward way is to create a public ActionList variable so that you can assign it in its Inspector:

    using UnityEngine;
    using AC;
    
    public class RunActionListExample : MonoBehaviour
    {
    
        public ActionList myActionList;
    
        public void RunAnActionList ()
        {
            myActionList.Interact ();
        }
    
    }
    

    Super Mega Bonus Points for adding inventory item directly from DS Sequencer

    I can't comment on working directly with DS Sequencer, but inventory items too can be added through script:

    public void AddInventoryItem ()
    {
        InvInstance invInstance = new InvInstance ("MyItem");
        KickStarter.runtimeInventory.PlayerInvCollection.Add (invInstance);
    }
    

    More on this topic can be found in the Manual's "Inventory scripting" chapter.

  • I'm trying to run an actionlist from a script, but I'm stuck here.

    "This is more of a general C# question, but the most straightforward way is to create a public ActionList variable so that you can assign it in its Inspector:"

    I copied your code snippet. Where do I assign the ActionList, and which inspector are talking about? I feel lost in the terminology.

  • edited April 2023

    Ok, I found where to assign the ActionList.asset, but I can't drag my action list into the window. In fact, when I click the circle next to it there are no actionlist assets listed at all.
    What am I doing wrong?

    Update:
    Ok, I solved the first part by changing
    public ActionList
    to
    public ActionListAsset

    And now the action list can be added, and the code compiles.

    However, when I change the content of a container the game freezes and gives me a

    "NullReferenceException: Object reference not set to an instance of an object"

    The code looks like this, currently:

    private void OnContainerAdd(Container container, InvInstance containerItem)
    {

        if (containers.Contains(container))
        {
            UpdateStats();
            myActionList.Interact();
        }
    

    }

  • edited April 2023

    Can you share your full script, a shot of the component's Inspector, and also share the error message in full (stacktrace included)?

  • I'm struggling to show code properly, but hopefully this should be legible:

    `using UnityEngine;
    using System.Collections.Generic;
    using AC;

    public class StatUpdateExample : MonoBehaviour
    {

    [SerializeField] private List<Container> containers = new List<Container>();
    [SerializeField] private Stat[] stats;
    public ActionListAsset myActionList;
    
    private void Start()
    {
        UpdateStats();
    }
    
    private void OnEnable()
    {
        EventManager.OnContainerAdd += OnContainerAdd;
        EventManager.OnContainerRemove += OnContainerRemove;
        UpdateStats();
    }
    
    private void OnDisable()
    {
        EventManager.OnContainerAdd -= OnContainerAdd;
        EventManager.OnContainerRemove -= OnContainerRemove;
    }
    
    private void OnContainerAdd(Container container, InvInstance containerItem)
    {
    
        if (containers.Contains(container))
        {
            UpdateStats();
            myActionList.Interact();
        }
    
    
    
    }
    
    private void OnContainerRemove(Container container, InvInstance containerItem)
    {
        if (containers.Contains(container))
        {
            UpdateStats();
        }
        if (container.gameObject.name == "HeadContainer")
        {
    
            GameObject[] gear = GameObject.FindGameObjectsWithTag("HeadGear");
    
    
            foreach (GameObject obj in gear)
            {
                obj.GetComponent<Renderer>().enabled = false;
            }
        }
        else if (container.gameObject.name == "BodyContainer")
        {
    
            GameObject[] gear2 = GameObject.FindGameObjectsWithTag("BodyGear");
    
    
            foreach (GameObject obj in gear2)
            {
                obj.GetComponent<Renderer>().enabled = false;
            }
        }
        else if (container.gameObject.name == "FaceContainer")
        {
    
            GameObject[] gear2 = GameObject.FindGameObjectsWithTag("FaceGear");
    
    
            foreach (GameObject obj in gear2)
            {
                obj.GetComponent<Renderer>().enabled = false;
            }
        }
        else if (container.gameObject.name == "NeckContainer")
        {
    
            GameObject[] gear3 = GameObject.FindGameObjectsWithTag("NeckGear");
    
    
            foreach (GameObject obj in gear3)
            {
                obj.GetComponent<Renderer>().enabled = false;
            }
        }
    
    }
    
    private void UpdateStats()
    {
        foreach (Stat stat in stats)
        {
            UpdateStats(stat);
        }
    }
    
    private void UpdateStats(Stat stat)
    {
        int totalValue = GlobalVariables.GetVariable(stat.baseVariableName).IntegerValue;
    
        foreach (Container container in containers)
        {
            if (container.InvCollection.InvItems.Count == 0 || container.InvCollection.InvItems[0] == null)
            {
                continue;
            }
    
            if (InvInstance.IsValid(container.InvCollection.GetInstanceAtIndex(0)))
            {
                int statValue = container.InvCollection.GetInstanceAtIndex(0).GetProperty(stat.propertyName).IntegerValue;
                totalValue += statValue;
            }
        }
    
        GlobalVariables.GetVariable(stat.propertyName).IntegerValue = totalValue;
    }
    
    
    [System.Serializable]
    private class Stat
    {
    
        public string propertyName;
        public string baseVariableName;
    
    }
    

    }`

    Some clarification:
    If you remember, this is a script connected to the equipment system I have, where the containers are where the player put the items to gear up the character.

    I want to be able to check if certain combinations of items have been equipment, so I figured I should put that under the OnContainerAdd.

    Under the remove-part I've put my own code.

  • Screenshots of error report and inspector:

    https://imgur.com/YvaZU4Z
    https://imgur.com/jiyrdLS

  • Thank you, but I'll still need a shot of the component's Inspector, and the error message in full, i.e. with the stacktrace included.

  • I think I posted just before your reply

  • Indeed, thanks. The Inspector I'm looking for, though, is that of the component in the scene - not the script asset file itself.

    The line numbers also don't seem to quite match - can you confirm what's on line 39 of the script?

  • edited April 2023

    The script is attached to a prefab (the one with the containers, if you recall), maybe that's an issue?

    Here are screenshots of the prefab, as well as the script attached to it:
    https://imgur.com/nr2DspZ
    https://imgur.com/VunrXbq

    As can be seen in the screenshot, line 39 has the line:
    if (containers.Contains(container))

    and previously only held
    UpdateStats ();

    which you created to make sure that the characters "stats" were updated each time a piece of equipment was added in the containers.

  • You can replace that line with:

    if (containers != null && containers.Contains (container))
    

    Does the issue persist having commented out the new code? As it is now, it should be as it was before you made any changes - which I'm presuming had no issue beforehand.

  • edited April 2023

    There's no issue when I comment out the new code, no, it's always been working perfectly.

    But replacing the line with
    if (containers != null && containers.Contains (container))

    has no effect - if I take put back the new code it has the same null reference error.

    PS:
    If I keep the MyActionList - stuff commented out, the script compiles neatly with your latest replacement.

  • If I keep the MyActionList - stuff commented out, the script compiles neatly with your latest replacement.

    Sorry, but I must be clear. Was there an issue before the addition of MyActionList, which is solved by the line change I just suggested?

    If not, it suggests that the issue is to do with the MyActionList variable not being set in the component's Inspector. With it uncommented, check that it is assigned in the component.

  • The script has always worked before I started experimenting with the MyActionList stuff.
    I currently have the action list assigned in the window in the script's inspector. Does it also have to be assigned in the container prefab's component somehow?
  • Lol, my mistake. I even said it myself:
    "Does it also have to be assigned in the container prefab's component somehow?"

    But I just didn't look carefully enough, or the placement of the assignment box was a but unexpected.

    Still a bit confusing that the asset file can be assigned in the script's inspector, even though that is not enough (or even necessary?), but it's all good now!

    Thanks!

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.