Wowie Jam! Update #3 Global Variables in Godot

I’ve read this documentation page on Singletons so many times. And so many other online Q&A’s about making Global variables in Godot. And those usually point back to that same documentation page.

It’s actually SO simple that it needs to be explained more simply:

Here’s what worked:

Make a new script called globals.gd and save it. It will look like:

extends Node

# class member variables go here, for example:
# var a = 2
# var b = "textvar"

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	pass

#func _process(delta):
#	# Called every frame. Delta is time since last frame.
#	# Update game logic here.
#	pass

I put in my variables and commented out the part I’m not using so now mine looks like:

extends Node

# how far away to make the boom
var boom_loc = 5
# direction of the boom
var boom_force = 15

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	pass

#func _process(delta):
#	# Called every frame. Delta is time since last frame.
#	# Update game logic here.
#	pass

Now go to the Project Settings AutoLoad tab and add the globals.gd script

Now to USE those global variables in a different script they get called as:

globals.boom_loc and globals.boom_force

# example - printing the value to the debug console
print (globals.boom_loc)

Crazy simple, right?

So this is my current globals.gd – I’m putting in all the variables for fine-tuning the gameplay

extends Node

# how far away to make the boom
var boom_loc = 20
# direction of the boom
var boom_force = 200
# delay before the next boom can go off
var boom_delay = 15

# for the spaceships thrust - the thrust amount is how strong the engine is
var expected_FPS=60
# thrust_amount was 10 originally
var thrust_amount = 9 * expected_FPS

# how bouncy are the spaceships
# bounce was .3 originally
var bounce = .1

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	pass

#func _process(delta):
#	# Called every frame. Delta is time since last frame.
#	# Update game logic here.
#	pass

and this becomes my script for the green spaceship so that it is grabbing all the values from the globals.gd

extends RigidBody2D

var boomtimer = 0
var playershipmotion = Vector2 ()

var thrust_amount = globals.thrust_amount

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	set_bounce(globals.bounce)
	pass

func _physics_process(delta):
#	# Called every frame. Delta is time since last frame.
#	# Update game logic here.
	boomtimer = boomtimer + 1
	if Input.is_key_pressed(KEY_SLASH):
		var rotation = self.get_global_rotation()
		var forwardNormal = Vector2(0,1).rotated(rotation)
		self.apply_impulse(Vector2(),forwardNormal * -thrust_amount * delta)
	if Input.is_key_pressed(KEY_APOSTROPHE) and (boomtimer>globals.boom_delay):
		var boomforce = Vector2(randf()*globals.boom_force,randf()*globals.boom_force)
		var boomloc = Vector2(randf()*globals.boom_loc,randf()*globals.boom_loc)
		self.apply_impulse(boomforce,boomloc)
		boomtimer = 0
		pass

OK – so now I’m working on figuring out the collision stuff so that when a checkpoint is reached the program knows it and signals the player. You can see the #5 checkpoint on screen now.

The I need to make a full race level with obstacles and 5 checkpoints and have it work and declare a winner.

Then fine polishing would include a title screen and setup and then being able to load other game levels. But I think I may need sleep more than being able to do that.

Sharing is caring 81766 style!