#Reference a variable inside another variable

17 messages · Page 1 of 1 (latest)

nimble thicket
#

Hi!

I have a global script with loads of boolean game variables called GAME. I have an Area2D scene with a text @export variable, where I can manually enter some text. I want to reference the corresponding variable in the GAME script from this Area2D scene. Here is my code on the Area2D node....

@export var game_section = ""

func _ready():
    if GAME.game_section == true:
        queue_free()

However, I get an error....

Invalid get index 'game_section' (on base: 'Node()')

Any ideas?

bright reef
#

If you do object.name it treats name as if it were just a string, rather than loading the value. What you want to do here is GAME[game_section] == true as that will do what you are thinking and use the value of the variable

nimble thicket
bright reef
#

Try the get function. GAME.get(game_section) == true

#

You could also set the export's type as StringName to avoid having to implicitly cast it every time. Better performance, and just generally good policy if you don't intend to modify the string.

nimble thicket
#

Thanks WeaverSong, that does not throw errors anymore, but the intended outcome is not being realised. Hmmm, When I do a print(game_section) in the process function, I can't see any output, but to the right of this output screen, I can see a tally of all the messages, and it balloons to 10s of thousands in just a matter of seconds, that's probs why the console output is blank, the messages are too frequent as there are so many.

I have copied this scene hundreds of times throughout the level, so this **game_section** @exported variable is unique in each and every instance of this scene throughout the level. That might explain all the print messages. Thinking maybe I need to a for loop somewhere? Back to the drawing board if you're out of ideas, thanks for your help!

signal torrent
#

@nimble thicket Does GAME have the variable game_section ??

nimble thicket
signal torrent
#

@nimble thicket Ok, so in this case you would compare your exported variable to the GAME variable. Unless when your checking for true or false, who is setting it, if game_section is set to be for different meanings??

#

If GAME.game_section == game_section:

#

May be easier to have a get function in your global script. You could just say if GAME.return_game_section(exported var):

nimble thicket
# signal torrent <@486839012116398091> Ok, so in this case you would compare your exported variab...

Hey raistlintheweezymage thanks for the reply. I tried using that but get an error "Invalid get index 'game_section' (on base: 'Node ()')". I am indeed just looking for a true/false (boolean).

This Area2D scene is an item the player picks up, at which point this global variable is set to true and I want this item to be gone, if the player restarts the level. Currently, this item is present every time the level is restarted, even if picked up multiple times. So the code basically is just to check if this variable it true, and if it is, to queue_free() this item.

Here is the code on this Area2D node (the item)

@export var game_section = ""

func _ready():
    if GAME.game_section == true:
        queue_free()

And here are some of the variables in the global GAME script just to give you an idea

var one = false
var two = false
var three = false
var four = false
var five = false
var six = false
var seven = false
var eight = false
var nine = false
var ten = false
var eleven = false
var twelve = false
var thirteen = false
var fourteen = false
var fifteen = false
var sixteen = false
var seventeen = false
signal torrent
#

So, I did a little example of a global script you might be able to use.

#
extends Node

var game_section: int = 0

func return_game_section(check_section)-> bool:
    if !check_section == game_section:
        return false
    return true
#
@export var game_section: int = 0

func _ready():

    print(GAME.return_game_section(game_section))
#

You can set the game_section variable anywhere with GAME.game_section = blah blah