Hello, I'm working on UI for my game where I load levels from the web as an array of dictionaries. I pass the dictionaries in lots of nodes depending on context (level selection screen / level list screen / level scores screen etc.).

Here's how I do:

# Load.gd
var screen = Screen.instance()
screen.initialize(level)
add_child(screen)

# Screen.gd
func initialize(pLevel : Dictionary) -> void:
	yield(self, "ready")
	level = pLevel
	$VBoxContainer/LevelTitleLabel.text = level["Name"]
	$VBoxContainer/CreatorNameLabel.text = "By " + level["Author"]

	$HBoxContainer/Stars1.set_texture(StarFull if level["Stars"] > 0 else StarEmpty)
	$HBoxContainer/Stars2.set_texture(StarFull if level["Stars"] > 1 else StarEmpty)
	$HBoxContainer/Stars3.set_texture(StarFull if level["Stars"] > 2 else StarEmpty)
	$HBoxContainer/Stars4.set_texture(StarFull if level["Stars"] > 3 else StarEmpty)

	$TimeLabel.text = Utils.format_time(level["BestTime"]/1000)

My issue is that Dictionary are pretty error-prone. I tried to convert it to a inner class but I did not succeed since we cannot share inner class (I think?) in different nodes.

What can I do to have the compiler help me with auto-completion and compilation-time exception instead of run-time exception?

My issue is that Dictionary are pretty error-prone.

Do you mean that if you make a typo like level["Starz"], the compiler won't detect it?

cannot share inner class (I think?) in different nodes.

Why not use a global class?

Maybe I'm using "level" in a different context than you, but I have an autoload Level that contains an Array of Dictionaries that define the level parameters, and functions that return the parameter values, such as Level.scale_factor().

If I were loading levels from the web, I think I'd rather use json. If nothing else, it's easier to spot errors. However, your game should thoroughly parse your level description for errors either way. You shouldn't just count on any external data being correct, even if you made it.

@DaveTheCoder said: Why not use a global class?

What is a global class? Thanks to your comment I tried something that is I believe is a great solution:

I can put my innerclass in a singleton so I can use it from anywhere (with SINGLETON.CLASS syntax).

#Load.gd 

#Domain.gd (AUTOLOAD)
extends Node

class Level:
	var Id: int
	var Name: String
	var Author: String
	var Preview: String
	var Description: String
	var Stars: int
	var Finishes: int
	var Attempts: int
	var BestTime: int


#Screen.gd
func initialize(pLevel : Domain.Level) -> void:
	yield(self, "ready")
	level = pLevel
	$VBoxContainer/LevelTitleLabel.text = level.Name
	$VBoxContainer/CreatorNameLabel.text = "By " + level.Author

	$HBoxContainer/Stars1.set_texture(StarFull if level.Stars > 0 else StarEmpty)
	$HBoxContainer/Stars2.set_texture(StarFull if level.Stars > 1 else StarEmpty)
	$HBoxContainer/Stars3.set_texture(StarFull if level.Stars > 2 else StarEmpty)
	$HBoxContainer/Stars4.set_texture(StarFull if level.Stars > 3 else StarEmpty)

	$TimeLabel.text = Utils.format_time(level.BestTime/1000)

I'm really happy with this solution

7 months later