Forum rules - please read before posting.

Running action on variable change

Hi,

I've seen that you can run a cutscene on a variable change, which is very nice. However, I was wondering if I can run arbitrary actions or "ActionList"s (sorry if that's not the right term) on a variable change as well.

Thanks in advance.

Comments

  • Welcome to the community, @arcnor.  Do you mean an ActionList asset, that can be run from any scene?  There's a few ways you can do this:

    1. A Cutscene can run an ActionList asset via the ActionList: Run Action.  Your "On variable change" Cutscene could run an asset file this way.
    2. The Scene Manager can be made to reference ActionList assets instead of scene-based Cutscenes via the ActionList source property just above
    3. A custom script can run an ActionList asset (by invoking its Interact method) whenever a variable is changed by hooking into the OnVariableChange custom event.  More on this method be found in the "Custom events" chapter of the Manual.
  • Thank you! I especially like that last one, it looks less "hacky", specially if I really want to make a cutscene that uses a variable change + also want to have other non-cutscene related changes.
  • I didn't see the first part of your message. You said "an ActionList asset, that can be run from any scene?", but no, actually I wanted to run some code on a local variable change.

    Specifically (and this might be the wrong way of doing things, but this is actually just a test) I have a variable that I set from a few different places, and I want some object in the scene to be visible depending on that variable's value. I don't want to set the visibility manually every time I change the variable, as that is highly prone to errors.

    And by using those methods, I cannot access local variables (AFAIK). So is there any better way of doing what I want? I imagine I can also add some component to the object that sets its own visibility depending on the variable value, but again I have the problem of receiving events when the variable changes.

    I'll check the chapter on "Custom events" anyway, that might just be it.

    Thanks again!
  • You can certainly read and write both local and global variables - see the "Variable scripting" chapter as well as the front page of the Scripting guide for a primer.

    What may be more appropriate in this case is to place the variable setting / visibility Actions in a single Cutscene, and call that as necessary.  As with regular coding, it's generally better to avoid recycling Actions where possible, and the ActionList: Run Action can be used to temporarily divert to a separate ActionList / Cutscene midway through another.

    Another feature to be aware of is that of ActionList parameters.  Similar to function parameters, they allow you to change Action field values at runtime - allowing you to make a "generic" list that performs commands in a slightly different way each time.  For example, you can make a generic "take item" list that removes an item from the scene, adds it to your inventory, plays a sound.  Again, this can be found in the Manual, under "ActionList parameters".
  • Thanks for your detailed answer, that's great.

    I said I cannot access local variables because when I created an asset file (to try the 2nd option you mentioned above) with my ActionList, the only variables that appear on the list are global ones (and the "Source" field that specifies where the variables are coming from is gone, so I cannot change that to "Local" or "Scene", can't remember what it said). This makes sense, as the ActionList asset is not really associated with a scene, but again, I'm probably missing something again :).

    What I have for now (that works great, but might not be the best solution for it) is a script component that has the variable name typed in, then finds that variable ID `OnCreate` (by going through all variables, it seems there is no `LocalVariables.GetVariableByName()` or similar) and adds itself as `OnVariableChange` handler. I add that component to every graphic that needs its visibility changed depending on variable and everything is fine.

    I'll check those ActionList parameters, that sounds like a good thing, maybe it will make this easier, or at least more correct.

    Thank you again, I'm probably sure you hear this all the time, but this is great support!


  • Since ActionList assets run scene-independently, they can't access local variables.  However, you can convert a local variable to global via the "cog" icon to the right in the Variables Manager, which will update Actions that reference it.

    If you're dealing with local variables and scene-specific objects, though, then you should stick to Cutscenes - no need for ActionList assets.

    There is no "GetVariableByName", no - a variable's name is just a label, and multiple variables can have the same one.  The ID number, to the left of their entry in the Variables Manager, is unique and is the recommended way to retrieve them.
  • Yeah, I prefer to keep things local, I'll try some of the other methods or keep my component.

    And yeah, I understand the ID is the way to reference them, but it's very cumbersome for me to reference stuff by ID, and I know I won't have duplicates in my case.

    It will be nice if I can make my component list the variables in a dropdown instead, but I don't know enough about Unity yet to make that happen. Maybe that's a better way of doing it (and probably more performant than finding the ID at runtime, but for now I just have a demo, so it's fine :smile: )

    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.