Forum rules - please read before posting.

Array in an Action

edited December 2018 in Technical Q&A

Hi!

I'm new to AC, but I can already see how fantastic this tool is!

I've been playing around with everything for a while now and I'm still getting used to everything. I'm currently trying to get arrays to work in an Action. I have a lot of data, and I would really rather not create 50+ variables in the menu. I was planning on creating an Action that I can run at the start of the game to set up a bunch of arrays. Later, other Actions would access and change them dynamically in the game - is that possible at all?

The part I'm struggling with at the moment, is setting up the GUI for the Action in ShowGUI() method - I can't seem to find the best way to show array in the GUI. I tried using EditorGUILayout.IntPopup() but as far I understand it takes in strings, which later need to be converted? Is there a better method?

Any guidance in the right direction would be greatly appreciated! Thank you!

Unity Version: 2018.2.18f1
AC Version: 1.65.2

Comments

  • i'm sure chris will give you more (and better) info, but i can confirm that it is possible to use arrays in AC, because i do it myself

    that's the good news. the bad news is that it's by no means straightforward. the way i do it is by encoding each array as a string variable, which has the advantage that the data is automatically included in AC's save/load system. the array then obviously needs to be decoded whenever you want to use it

    i've set up a couple of new ActionLists: ActionArrayElementCheck (to check the value of an array element) and ActionArrayElementSet (to set a value), plus one script to set the whole system up. my ShowGui method converts the string into the appropriate variable type in order to properly display it in the relevant ActionList

    unfortunately it's not a very friendly or flexible system (and i'm sure there will be better ways to approach the problem), so probably not worth sharing

    a discussion on the topic can be found here: https://adventurecreator.org/forum/discussion/7485/storing-and-processing-actor-specific-data/p1

  • Thank you for the response!

    To deal with displaying the array in an Action, instead of using EditorGUILayout.IntPopup() I simply set the array as separate variables in GUI by using EditorGUILayout.IntField();. This results in a really long Action, but it only needs to be ran once to set things up, so I'm fine with that.

    Is encoding to string necessary only if you're using AC's save/load system? Would it be possible to use your own Save/Load?

    Just seems like the ActionList will get cluttered a lot if you have to run "encode/decode" actions every time you want to change things up.

    Basically what I'm trying to do, is to get characters to have a number of variables assigned to them - arrays seemed like best bet, but maybe there's a way that's tied to AC I'm not aware of?

    Thanks!

  • edited December 2018

    Welcome to the community, @bulka_tarta.

    The Action class's ShowGUI method relies on Unity's EditorGUI API, so displaying an array is a topic that Unity's own forums will also cover, if you'd like to search them for an example too.

    Generally though, you can just iterate through the array to set each value individually:

    for (int i=0; i<myIntArray.Length; i++)
    {
        myIntArray[i] = EditorGUILayout.IntField ("Index " + i, myIntArray[i]);
    }
    

    If you'd like to describe in more detail what you're ultimately trying to do, it may be possible to recommend the best approach. Note that you don't have to define an array within an Action itself - you can, if appropriate, define a public array in a regular MonoBehaviour, configure it in its Inspector, and then modify/check that array using a custom Action that refers to it's component.

    If you're trying to give each character an array of their own, it might be better to give each one their own copy of a new component. If you only need to set up the array values once, that's probably the better option. A custom Action could then check these values and act accordingly.

    Is encoding to string necessary only if you're using AC's save/load system? Would it be possible to use your own Save/Load?

    Only if you wish to include the data inside AC's "global" saved data - see this tutorial for more. For saving your own local (per-scene) data, you can write a custom "Remember" script - see this tutorial.

  • i went for the encoded string option mainly because of the convenience of having the data automatically included in AC's saved data, thus saving myself the bother of having to write my own save/load system

    the string encode/decode is done outside the actionlists, in my array handling code, so the actionlists don't get any more cluttered up than they would normally do

    the big downside of my approach is that i need to write a whole load of new code every time i want to add a new array into the mix - which is why i'm sure there must be a better way of doing it than mine

    but, as some examples, i store ActorDialoguePoint (int), ActorName (string), ActorNameKnown (bool) plus some game-specific enum types as well. i also store Location specific data in the same way (although, i think it would now be possible to use the Scene Attributes feature to store that data)

  • edited December 2018

    Thank you for the replies!

    EditorGUILayout.IntField is the approach I ended up going for, thanks for clarification though!

    I tried adding a script in regular MonoBehaviour, but AC would complain that it must derive from Action class to be used as an Action. I thought AC won't let me create a script that isn't an action so I was really struggling with grasping how I can get it done. I just realised though, that this was due to the fact I had the MonoBehaviour script in my Custom Action Scripts folder. I moved it and I get no errors!

    My original idea was to use a separate component and attach it to the objects. Because of the my confusion with custom scripts, I didn't think it would be possible. Now that I can get my own scripts working, I just attached a component to each object and I'm able to do the rest through custom Actions.

    Thank you for the links to the save data. I will go over them when I get a chance and determine what's more complicated (encoding arrays or custom saving), if I need to use arrays in the future.

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.