Game development's the hobby

Code Snippet

Godot 3.2

Godot 3.2 is an exciting new launch with many new features.  I did encounter a little issue with my current project when switching to Godot 3.2 that my solution may help people out.

I wrote a finite state machine for use with my game.  In this state machine, a state calls on the player controller.  For some reason, the state machine is loaded before the onready commands are called in the player controller, causing any onready vars to not be prepared.  To fix this, and to add a little bit of lazy programming into the player controller, I implemented the following code:

onready var kinematic_controller : KinematicBody2D = get_node("Character_Controller")
var kinematic_controller : KinematicBody2D = null

...

func get_kinematic_controller() -> KinematicBody2D:
	if kinematic_controller == null:
		kinematic_controller = get_node("Character_Controller")
	return kinematic_controller

This way, if for any reason the onready call to set a controller is not called, we can call it in the get function.  I got my game from a non-running state to running perfectly again.

Advertisement

Code snippet for Tiled tilesets to AGK

This is a code snippet that creates a file that can be used for tilesets, created in the AGK expected format for tilesets. I will write a full tutorial on how to use this code in a future post.  See this post on use of the snippet.

//Change these values if your tiles are a different size in pixels
tileWidth = 128
tileHeight = tileWidth
numHorizontalTiles = 16
numVerticalTiles = 16

//Open a new file to write.  I found no success writing to .txt.
fileid = OpenToWrite ( "myfile" )

//Variables used in the loop.
string$ = "" //This will be what gets written to the file.
tileid=0 //Used to count up and assign each tile an ID

//This is the same as saying for(int i=0;i<numHorizontalTiles;i++) with the i++ at the end of loop.
i=0 //Start at 0
While i<numHorizontalTiles

	//Same as above, nested for loops.
	j=0 //Start at 0 after each loop.
	While j<numVerticalTiles

		//increment tileid each loop. In Tiled 0 is no tile, so we start with 1.
		tileid = tileid + 1

		//Assign the line to be written to string$
		string$ =  Str(tileid) + ":" + Str(j*tileWidth) + ":" + Str(i*tileHeight) + ":" + Str(tileWidth) + ":" + Str(tileHeight)

		//Write string$ to file.  WriteToLine saves it in ASCII readable format.
		WriteLine(fileid, string$)
		/* Remove to watch progress.
		Print(string$)
		Print(GetWritePath())
		Sync()
		*/ //Remove to watch progress.

		//j++ for the loop
		j = j + 1
	EndWhile

	//i++ for the loop
	i = i + 1
EndWhile

//Close the file
CloseFile ( fileid )