Forum rules - please read before posting.

Trouble accessing int value of a Local Variable

Hi all, I've been scratching my head about this for a while so thought I would see if anyone can help.

For context, I've been trying to set up a health bar that updates based on the value of a local variable in AC. I've set up the Unity UI side of it and hooked that into a corresponding menu in the Menu Manager. The bit where I've come unstuck is retrieving the variable value from AC to pass into the UI update.

I have very little experience coding, so I'm sure I'm making a very basic error here, but here's what I have (apologies in advance for the awful formatting):

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

public class DashHealthBarScript : MonoBehaviour
{

public Slider slider;
int health;

public void SetMaxHealth(int health)
{
    slider.maxValue = health;
    slider.value = health;
}

public void SetHealth(int health)
{
    slider.value = health;
}

GVar myVar = LocalVariables.GetVariable(8);
health = myVar.GetIntegerValue(8);
SetHealth(int health);

}

I get an error on this line and I can't for the life of me work out why:
health = myVar.GetIntegerValue(8);

I'm sure this is a rookie mistake I'm making but any help would be very much appreciated!

Comments

  • GetIntegerValue is a function inside LocalVariables, which access GVar itself.

    Within GVar, you can get the integer value with IntegerValue:

    health = myVar.IntegerValue;
    
  • Hi Chris, thanks for the rapid response! That has resolved my scripting issue, however I am having issues calling my scripts from my existing action list in scene. I think this is a problem to do with my setup though, so I'm going to go away and do some Unity fundamentals before going back to it.

    Just to sanity check, this is my current approach:

    1) Set up Unity UI Canvas
    2) Set up AC Menu & connect to Unity UI Canvas
    3) Write scripts to read AC variables & update Unity UI health bar
    4) Set up Action Lists for gameplay & call scripts via Object: Send Message or Object: Call Event

    Does this make sense?

  • That should work.

    Though, if the only Actions involved are being used to trigger your scripts, you may be able to remove AC from the equation and just have e.g. the Unity Button components call the script functions directly via Events.

    Generally when it comes to UI and variables, though, it's often easier to work with Global variables - even if they're only used in a particular scene. Global Variables can be set to sync with e.g. Slider elements automatically - no need to involve scripting. It also allows the UI to be prefabbed and removed from the scene file, which keeps things a little tidier.

  • Hi Chris, can I check what the process for linking global variables to a slider directly is please? Thinking this might be a better solution for me at this stage!

  • A Slider element defined in the Menu Manager can have its Slider affects property set to Float Variable. An additional field then appears beneath, allowing you to select a Global Float variable defined in the Variables Manager.

    To have this affect your UI Slider component, you'll then have to link this Menu to the UI Canvas - see this tutorial for details.

  • Brilliant, thanks Chris, that worked a treat!

  • I feel kind of stupid, but how do I reflect the value of a global int in a slider?

    I can easily link it to a float, but if the variable already exists and is an int, how do I best proceed?

    Since I want the slider to always be visible, I need it to instantly update if the variable changes.

    I tried simply changing the variables in question into floats, but that caused other bugs (I guess there's stuff in the code that relies on the variables being ints)

  • A Slider's value is a float - it can't be linked to a Global Integer directly.

    If you're using Unity UI, you can check the Slider's Whole Numbers option to make the float effectively an integer.

    If you need it to be a literal Integer, though, you'd have to use a proxy and copy it. That is, link the Slider to a Float, and then have the Slider's ActionList on change value asset run the Variable: Copy Action to transfer the Float variable to the Integer variable.

  • Thanks, that's exactly what I wanted :)

  • edited November 2023

    Nothing happens.

    Your explanation sounds a bit backwards - surely I must copy the Int to the Float? I don't want to change the original value.

    And will the whole ActionList on change value really trigger when it is the actionlist itself that changes the variable?

    I feel like I need to instead make sure that every time the original Int variable changes, I need to copy the values into the Float ones.

    Update:
    Ok so if I make an action list with all the Variable:Copy actions and put it in a room's On Variable Change, it works as intended, but there's unfortunately no similar thing for the game in general...

  • there's unfortunately no similar thing for the game in general...

    The Events Editor will let you do this, with a Variable: Change Global event.

    The Events Editor can be accessed from the top toolbar, under "Adventure Creator -> Editors -> Events Editor". See the Manual's "Custom events" chapter for details.

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.