Forum rules - please read before posting.

Changing global variables via custom action scripts? The change doesn't seem to apply globally

Hello again! One more question regarding something I'm attempting to build and struggling with. Apologies if my code is completely wrong/junk, I am an artist primarily and have pretty much no Unity programming experience outside of just learning as I go making my game.

I'm trying to create an email system that will shift all of the emails you currently have down a "slot" once you get a new one. I was hoping to accomplish this by running custom actions that would cause all email slot variables to copy the value of the variables assigned to the slots above them, and then finally replace the top slot with the new email. I want to do it this way because the emails can be triggered to appear at different times based on the player's gameplay decisions, so there's not a guaranteed order in which they may appear. I thought about using the pre-built Documents function for this, but I actually have two separate systems that would need to behave like this (emails AND forum posts), and I'm not sure if I can use the document system to do what I want to achieve with both of these separate systems.

I am currently trying to execute the logic like this, with each line happening in their own custom action. (I tried putting the whole command into one custom action, but it executed all of the commands at the same time, resulting in each email slot getting the EmailNumber variable's number. With separate custom actions called sequentially it behaves more as I expected.)

        EmailSlot5 = EmailSlot4;
        EmailSlot4 = EmailSlot3;
        EmailSlot3 = EmailSlot2;
        EmailSlot2 = EmailSlot1;
        EmailSlot1 = EmailSlot0;
        EmailSlot0.IntegerValue = EmailNumber;

So each of the Email Slot 5 - 1 actions are scripted like this, on separate actions for each line:

using UnityEngine;
using System.Collections.Generic;
using AC;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

[System.Serializable]
public class ActionShiftEmail1Down: Action
{

    public override ActionCategory Category { get { return ActionCategory.Object; } }
    public override string Title { get { return "Shift Email 1 Down"; } }
    public override string Description { get { return "Shifts Email 1 Down a slot"; } }


    GVar EmailSlot0 = GlobalVariables.GetVariable(14);
    GVar EmailSlot1 = GlobalVariables.GetVariable(15);
    GVar EmailSlot2 = GlobalVariables.GetVariable(16);
    GVar EmailSlot3 = GlobalVariables.GetVariable(17);
    GVar EmailSlot4 = GlobalVariables.GetVariable(18);
    GVar EmailSlot5 = GlobalVariables.GetVariable(19);


    public override float Run()
    {
        EmailSlot1.IntegerValue = EmailSlot0.IntegerValue;
        Debug.Log("| Email Slot 0 =" + EmailSlot0.IntegerValue + " | Email Slot 1 =" + EmailSlot1.IntegerValue + " | Email Slot 2 =" + EmailSlot2.IntegerValue);
        return 0f;
    }


#if UNITY_EDITOR

    public override void ShowGUI()
    {

    }

#endif

}

}

For clarity, "Email Number" variable is one I define through the last custom action I call, set up like this:

using UnityEngine;
using System.Collections.Generic;
using AC;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

[System.Serializable]
public class ActionAddEmail : Action
{

    public override ActionCategory Category { get { return ActionCategory.Object; } }
    public override string Title { get { return "Add Email"; } }
    public override string Description { get { return "Add a new email."; } }

    public int EmailNumber;

    GVar EmailSlot0 = GlobalVariables.GetVariable(14);
    GVar EmailSlot1 = GlobalVariables.GetVariable(15);
    GVar EmailSlot2 = GlobalVariables.GetVariable(16);
    GVar EmailSlot3 = GlobalVariables.GetVariable(17);
    GVar EmailSlot4 = GlobalVariables.GetVariable(18);
    GVar EmailSlot5 = GlobalVariables.GetVariable(19);


    public override float Run()
    {

        EmailSlot0.IntegerValue = EmailNumber;
        Debug.Log("EmailNumber = " + EmailNumber + " | Email Slot 0 =" + EmailSlot0.IntegerValue + " | Email Slot 1 =" + EmailSlot1.IntegerValue + " | Email Slot 2 =" + EmailSlot2.IntegerValue);
        return 0f;
    }


#if UNITY_EDITOR

    public override void ShowGUI()
    {
        EmailNumber = EditorGUILayout.IntField("Email Number:", EmailNumber);

    }

#endif

}

The issue I'm having is that in my Debug Log Console command, I'm seeing the numbers of the variables change and shift down the way I expect them to, but in my actual AdventureCreator assets, the global variables aren't changing. I know this because I have a temporary text asset that lists all of the Email slot variables, and they don't change on screen, despite my debug log saying that they have changed. The emails also don't show up where they're actually supposed to show up. If I make the global variables change with a standard Action List > Variable:Set, the emails DO appear as they should, and the variables in my temporary text asset on screen change as well.

It seems to me that the variable change in my custom action is staying inside of the script its' self and not reaching the actual Global Variable being referenced by other menus in my game. Is there something specific I need to include in my custom action to ensure that the global variable is actually changed? Or do I have this set up fully wrong?

Comments

  • I think the issue here is that you need to use the SetIntegerValue() method instead of changing IntegerValue directly.

  • edited April 6

    Either should work, but it's worth trying. The equivalent would be:

    GlobalVariables.SetIntegerValue (15, GlobalVariables.GetIntegerValue (14));
    

    What's your AC version, and are the variable values also wrong when displayed in the Variables Manager?

    This should be possible using Documents, however. With the latest release, Documents can be assigned a Category, allowing them to be filtered into different Document menus as needed.

  • Oh! So you're saying in the latest release I could theoretically have two different "types" of documents, and then have two different menus that display/refer to each separate "category" of document only? If that's the case, I'll absolutely just use the document system than as opposed to doing it this way! Thanks for letting me know! If that doesn't work for what I need, I will try to implement the code lines you've both provided. Thanks a lot!

  • edited April 7

    Sorry to double post but just wanted to clarify--is there a way for me to display the collected documents so that the newest collected document appears at the top and shifts others down a slot? I have now created the email system with the Document system and it's working working swimmingly using an InventoryBox and Journal element in a menu. The only thing I can't get to work is getting the emails to show up in the order I want (the way actual emails would appear in an inbox.)

    Here are some images that show how I want it to function:

    What's happening instead of this is that the emails just show up in the order they appear in the documents list on the AC Editor. When I have more than six emails collected, anything above the sixth document just doesn't show up in the inventory at all, because there isn't a slot for it. Is there any built in functionality that would allow me to display the documents this way? I definitely would prefer to use the document system for this cuz it'd be a lot easier on me but having the email inbox in my game feel like an actual inbox feels like an important QoL detail I'd like to pay attention to.

    Thank you again for your help!

  • edited April 7

    Switch over your Menu's Source to Unity UI Prefab, if not already, so that the Menu's rendering is handled by Unity UI components.

    You can then reverse the order using layout components. There's a couple of ways you can do this:

    1. Use a Vertical Layout Group, with its X rotation set to 180, and Y-scale of the children to -1
    2. Use a custom layout component that reverses the order (as found here)
  • Thank you for your help! Unfortunately, this also does not behave the way I am trying to get it to behave. However, I think I have a better way to explain what I'm trying to achieve.

    The item-based inventory menu I have already behaves the way I would like my documents menu to behave. When my player receives a new inventory item, the newest inventory item appears in the first slot of the inventory box, and pushes all of the other ones back a slot. If the current items they have exceed the amount of slots on the inventory box, it pushes the oldest one out. I think it's because of the "Add to front" option in the ActionList command for adding inventory items.

    Adding documents does not have this option to add to front. If I had this option on my add document function, it would fully solve this problem. Would it be possible for me to edit the action script of my add document function to allow me to add it to the front of the document list the same way the inventory items are allowed to be?

  • I understood your issue - my suggestion is a workaround to the lack of an "Add to front" option for Documents.

    I can certainly look into providing such an option in an update, but the ReverseVerticalLayoutGroup component I linked to above will do the same thing: the last-added Document will appear at the top of the list.

  • I just can't seem to get the ReverseVerticalLayoutGroup to display them in the way I'm describing. It just continues to show the first collected email at the bottom of the list instead of at the top. It also doesn't solve my issue with overflow--when a new email beyond the six visible slots come in, it doesn't push old emails out of the way.

    I think for now I will continue to implement the document system and just let it be from the bottom up, but I would super love to have the "add to front" document option in the future if that's something feasible to add in another update! Thanks again for your help on this!

  • @ChrisIceBox I just saw the recent update added the Add to Front option for Documents! If you haven't heard it enough already, I am incredibly impressed by and grateful for how receptive and supportive you are for people developing games with AdventureCreator. Thank you for all that you do!

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.