YAML Lite

Monkey Forums/Monkey Code/YAML Lite

Soap(Posted 2011) [#1]
Here's a file I converted from BlitzMax, it can take simple YAML files and saves/loads them. I am basically using this as a way to be able to store complex information into SaveState() and LoadState() Please note I have only tested it on HTML5 so far, and it's works on my Firefox aswell as Chrome broswers. There may (probably?) be bugs still within the code, I just wanted to get this out as soon as I confirmed it's working.

Here's a sample usage:
local stateFile := New YAMLFile("state")
local lastState:String = LoadState()
Print "'"+lastState+"'"
if lastState = ""
	local tmpEle:YAMLElement = stateFile.GetElement("PlayerData",true)
	tmpEle.GetChildElement("position",true).data = playerShip.pos.ToString()
	tmpEle.GetChildElement("target",true).data = target.ToString()
else
	stateFile.__debug = true
	stateFile.ParseFromString( lastState )
	if not stateFile.elements.IsEmpty() then
		local parts:String[] = stateFile.GetElement("PlayerData").GetChildElement("position").data.Split(",")
		playerShip.pos.x = float(parts[0])
		playerShip.pos.y = float(parts[1])
		parts = stateFile.GetElement("PlayerData").GetChildElement("target").data.Split(",")
		target.x = float(parts[0])
		target.y = float(parts[1])
	else
		local tmpEle:YAMLElement = stateFile.GetElement("PlayerData",true)
		tmpEle.GetChildElement("position",true).data = playerShip.pos.ToString()
		tmpEle.GetChildElement("target",true).data = target.ToString()
	end
	stateFile.__debug = false
end


Vector is my own simple class with just x and y fields and ToString() outputs "x,y".




Soap(Posted 2011) [#2]
Unlike skn3's XML example, you can easily save the data back to a string. Continuing on the first example, inside of OnUpdate():

If saveTimer < Millisecs() then ''' saveTimer is just a Int field
	saveTimer = Millisecs() + 500
	local tmpEle:YAMLElement = stateFile.GetElement("PlayerData",true)
	tmpEle.GetChildElement("position",true).data = playerShip.pos.ToString()
	tmpEle.GetChildElement("target",true).data = target.ToString()
	SaveState( stateFile.SaveToString() )
end



Brüggemann(Posted 2011) [#3]
Hi Soap, Nice YAML Implementation ;)

Can you give me your blitzmax code for YAML
Would be nice :-)

Thx