I can't get updated values from global variables

Godot Version

Godot 4.3

Question

I have been working on a console script, and i ran into an issue that when i want to access a variable in a globally declared scene with a node using Project > Project Settings > Globals.

Within this global scene with a node, there is a function that is called everytime a player hits “Enter” that updates the RichTextLabel text ConsoleLog.text and passes that updated variable along to CLogText which is one of the ways i attempted to fix the problem, but to no avail.

I read somewhere about how there are basically 2 Autoloads for a global variable, the one you can modify in real time and the other one is stored and i assume its the one i keep refrencing instead of the updated autoload values.

Here is the code for the method that is supposed to update the displayed variables in the global class:

extends CONSOLECOMMAND

func RUN(args: Array[String]) -> String:
	var myvar: String = ConsoleUi.CLogText
	var myvar2 = ConsoleUi.CCHistoryIndex
	ConsoleUi.ConsoleLog.text = ""
	return ""

func _ready():
	self.CC_NAME = "clear"
	self.CC_DESC = "Clears the console. Nice and tidy."
	self.CC_PREFIX = "c_"

Here is the function that calls the CONSOLECOMMAND script, aswell as the variables i am attempting to change. This is set within a global control node:

extends Control

var CCHistoryIndex: int = -1
var CCHistory: Array[String]

func Execute(Command: Array[String]):
	print("[CONSOLE] Player Inputed: ",Command)
	if CCDictionary.has(Command[0]):
		var COutput_text: String = CCDictionary[Command[0]].RUN(Command)
		print_log(COutput_text)
		print("[DEVCONSOLE] COutput_text : ", COutput_text)
	else:
		print("[DEVCONSOLE][Execute] Returning function")
		print_log("Invalid Command.\n")

I ran this multiple times through debug, step by step and have found out that the CONSOLECOMMAND’s myvar’s just keep grabbing null variables or empty strings even though i could observe the output in the global script and it updates in real time. How do i go about fixing this?

Could you post your ConsoleUI script?

Sure. Here is the script:

extends Control
signal ui_open(PassedValue)

@onready var CCmands = $ConsoleCommands
@onready var MMCaptured = 0
@onready var ConsoleLog = $PanelContainer/VSplitContainer/MarginContainer2/Panel/ConsoleLogContainer/ConsoleLog
var CLogText: String
var CCHistory: Array[String]
var CCHistoryIndex: int = -1
@onready var ExecuteButton = $PanelContainer/VSplitContainer/MarginContainer/HBoxContainer/ExecuteButton
@onready var ConsoleInput = $PanelContainer/VSplitContainer/MarginContainer/HBoxContainer/ConsoleBar/CInputHolder/ConsoleInput
var LineColors = [Color(165, 48, 48),Color(194, 133, 33),Color(92, 176, 152)]
var LastChar = ""
var NewLine = Label.new()
var CCDictionary: Dictionary = {}
var CC = CONSOLECOMMAND
var ConsoleActive = 0
var ConsoleVis = 0
var once = 0

func AddCommand(cmdname: String, cmd: CONSOLECOMMAND) -> void:
	if CCDictionary.has(cmdname):
		print("[DEVCONSOLE] Duplicate command has been attempted to be added! Nothing ever happens.")
		return 
	CCDictionary[cmdname] = cmd
	print("[DEVCONSOLE] : Added to Dictionary : ", ConsoleUi.CCDictionary)

func Execute(Command: Array[String]):
	print("[CONSOLE] Player Inputed: ",Command)
	if CCDictionary.has(Command[0]):
		var COutput_text: String = CCDictionary[Command[0]].RUN(Command)
		print_log(COutput_text)
		print("[DEVCONSOLE] COutput_text : ", COutput_text)
	else:
		print("[DEVCONSOLE][Execute] Returning function")
		print_log("Invalid Command.\n")


func _on_player_plr_open_console():
	if ConsoleVis == 0:
		emit_signal("ui_open", 1)
		ConsoleVis = 1
	else:
		emit_signal("ui_open", 0)
		CCHistoryIndex = -1
		ConsoleVis = 0

func _input(event: InputEvent):
	if ConsoleVis == 1:
		if event is InputEventKey:
			if event.pressed == true:
				if event.physical_keycode == KEY_ENTER:
					var CInputText: String = ConsoleInput.text
					ConsoleInput.text = ""
					if CInputText == "": return
					CCHistoryIndex = -1
					add_to_history(CInputText)
					print_log("--> %s\n" % CInputText)
					print("[CONSOLE] Player Inputed: ",CInputText)
					var CITSplit = CInputText.split(" ", false, 2)
					print("[DEVCONSOLE] CITSPLIT :", CITSplit)
					Execute(CITSplit)
				if event.physical_keycode == KEY_UP:
					if CCHistoryIndex == -1:
						ConsoleInput.text = ""
					if CCHistoryIndex == 2:
						CCHistoryIndex = 0
						ConsoleInput.text = CCHistory[CCHistoryIndex]
						return
					CCHistoryIndex += 1
					ConsoleInput.text = CCHistory[CCHistoryIndex]
				if event.physical_keycode == KEY_DOWN:
					if CCHistoryIndex == -1:
						ConsoleInput.text = ""
					if CCHistoryIndex == -2:
						CCHistoryIndex = 2
						ConsoleInput.text = CCHistory[CCHistoryIndex]
						return
					CCHistoryIndex -= 1
					ConsoleInput.text = CCHistory[CCHistoryIndex]
			else:
				pass
	else:
		pass

func _process(delta: float) -> void:
	if ConsoleVis == 0:
		self.visible = false
	if ConsoleVis == 1:
		self.visible = true


func _on_player_send_mouse_mode(MouseMode: Variant) -> void:
	if MouseMode == Input.MOUSE_MODE_CAPTURED:
		Input.mouse_mode = Input.MOUSE_MODE_CONFINED


func _on_execute_button_pressed() -> void:
	var CInputText: String = ConsoleInput.text
	ConsoleInput.text = ""
	if CInputText == "": return # the command begins HERE
	CCHistoryIndex = -1
	add_to_history(CInputText)
	print_log("--> $s\n" % CInputText)
	print("[CONSOLE] Player Inputed: ",CInputText)
	var CITSplit = CInputText.split(" ", false, 2)
	Execute(CITSplit)

func print_log(text: String):
	ConsoleLog.text += text
	CLogText += ConsoleLog.text

func add_to_history(text: String):
	var max = 3
	if CCHistory.size() == max:
		CCHistory[0] = CCHistory[1]
		CCHistory[1] = CCHistory[2]
		CCHistory[2] = text
	if CCHistory.size() < max:
		CCHistory.append(text)
	print("[CONSOLE] CCHistory : ", CCHistory)
	print("[CONSOLE] CCHistory : ", CCHistory.size())

func _ready() -> void:
	for child in CCmands.get_children():
		if child is CONSOLECOMMAND:
			AddCommand(child.CC_NAME, child) ```

Offhand I can’t see anything obvious. Are you sure ConsoleUI is hooked up? Maybe try adding a print(delta) to _process() to see if it’s actually there.

Here is the code and the results. _process seems to be working fine with it.

func _process(delta: float) -> void:
	print("[DEVCONSOLE] Delta : ", delta)
	if ConsoleVis == 0:
		self.visible = false
	if ConsoleVis == 1:
		self.visible = true

Here is the output screenshot. Is this normal? I haven’t tried printing delta, im pretty sure its like the time inbetween rendering frames.

Interesting. Are you getting a reasonable value for CCHistoryIndex in your myvar2?

It seems to be the same value everytime too. Its like the global variable im picking from is the one thats not getting updated. I tested out what if i change the default value of CCHistoryIndex, and after going up and down a bunch ending on the 2nd recorded index i ran the clear function and it said that myval2 was what i typed it out in the scipt but not what i updated it to if that makes sense
Screenshot_208

Its like there is a version of the global script thats the default version thats not running any functions and just servers as something to read only, and the other one is the one you can tamper with and currently interact with the console ui and functions within it