Loading a string from JSON file but want to convert to a global variable

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lgmeister

I’m trying to pull a variable from my dictionary and then print/put it in a textbox as what the variable equals. Instead, it’s printing the string “globals.shipName”. Ideally I would like to have both a string and variable (i.e. “Welcome to the %s” %globals.shipName) but I can’t for the life of me figure out how to put a variable in a string in a dictionary. Any help would be nice.

In a separate script that is autoloaded loaded as “globals”

var shipName = "U.S.S Enterprise"

main script

func dialogLoad():   ### Pulls the data from testdialog.json
	var file = File.new()
	if file.file_exists("res://RSdialog/testdialog.json"):
		file.open("res://RSdialog/testdialog.json", File.READ)
		var tmp_data = file.get_as_text()
		file.close()
		globals.dict = {}
		globals.dict = parse_json(tmp_data)
		print(globals.dict) ### The results are {1:{final:0, name:Mr. Spock, text:globals.shipName}, 2:{final:0, name:Captain Rambo, text:Let's go for a spin!!}}


func dialogStart(): ### Starts the conversation	
	textboxHeader = get_node("/root/main/dialogbox/dialog_med/dialogLabelHeader")
	textboxHeader.set_text(str(globals.dict[str(globals.convoID)]["name"]))
	textboxMain = get_node("/root/main/dialogbox/dialog_med/dialogMessageBox")


	var messageConvert = globals.dict[str(globals.convoID)]["text"]
	messageConvert = str2var(messageConvert)
	print (messageConvert)
	textboxMain.message = messageConvert
:bust_in_silhouette: Reply From: exuin

This explains how to use “get()”, however, this won’t let you access variables from another script if the name of the script is in the string. You might have to make your own parser function (e.g., if there’s “globals” in the text, get the variable after the dot). If all variables that you’re going to use in text are in the global script, you could possibly just drop the “global.” part and use get() directly without any parsing.