lua table -> script.txt generator

lua table -> script.txt generator

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Funkencool
Posts: 542
Joined: 02 Dec 2011, 22:31

lua table -> script.txt generator

Post by Funkencool »

I couldn't really find this anywhere, so I thought I'd post it.

It takes a lua table and writes a standard script.txt. It's simple stuff but useful.

Code: Select all

function WriteScript(script)
	local txt = io.open('script.txt', 'w+')

	txt:write('[Game]\n{\n\n')
	-- First write Tables
	for key, value in pairs(script) do
		if type(value) == 'table' then
			txt:write('\t['..key..']\n\t{\n')
			for key, value in pairs(value) do
				txt:write('\t\t'..key..' = '..value..';\n')
			end
			txt:write('\t}\n\n')
		end
	end
	-- Then the rest (purely for aesthetics)
	for key, value in pairs(script) do
		if type(value) ~= 'table' then
			txt:write('\t'..key..' = '..value..';\n')
		end
	end
	txt:write('}')

	txt:close()
end
and for the sake of it, here's an example usage

Code: Select all

local script = {
	player0  =  {
		isfromdemo = 0,
		name = 'Local',
		rank = 0,
		spectator = 0,
		team = 0,
	},
	
	team0  =  {
		allyteam = 0,
		rgbcolor = '0.99609375 0.546875 0',
		side = 'CORE',
		teamleader = 0,
	},
	
	allyteam0  =  {
		numallies = 0,
	},

	gametype = 'Balanced Annihilation Reloaded Core $VERSION',
	hostip = '127.0.0.1',
	hostport = 8452,
	ishost = 1,
	mapname = 'Icy_Shell_v01',
	myplayername = 'Local',
	nohelperais = 0,
	numplayers = 1,
	numusers = 2,
	startpostype = 2,
}
WriteScript(script)
Spring.Restart('script.txt','')
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: lua table -> script.txt generator

Post by zwzsg »

I've had something like that for years, as part of my mission & savegame code:
http://pastebin.com/tf4ZhCui

But instead of a any table, it requires table with specific fields (like like GtW.players[p].bot.ShortName) which I didn't document anywhere. They're just written by my other gadget and widgets.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: lua table -> script.txt generator

Post by knorke »

good snippet, imo worth adding at http://springrts.com/wiki/Script.txt

random thoughts:

1) Looping over table like this is quite nice in Lua:

Code: Select all

for key, value in pairs(value) do
 txt:write('\t\t'..key..' = '..value..';\n')
end
and has very many uses.
Why: For example many Luas want to do something with all entries in a table and thus do:

Code: Select all

Echo ("bla1=" .. table.bla1)
Echo ("bla=" .. table.bla)
Echo ("bla2=" .. table.bla2)
Echo ("bla3=" .. table.bla2) -- oh oh
...
And as more things get added that list gets longer and longer, copypaste mistakes and general fail etc.
Not always possible but more elegant is the loop thing.

2) Before one can neatly write startscript.txt like above, one has to get it into table format.
If the script.txt is supposed to be generated from the options that player has selected in some Singleplayer Menu, then that might be place where some "bla2=" .. table.bla2 is hard to avoid and things get less elegant...

3) Spring.Restart() can either be called with file as arguement or with content of file.
(But vague memories that some startscripts were too long to be passed like that or something...?)
User avatar
Funkencool
Posts: 542
Joined: 02 Dec 2011, 22:31

Re: lua table -> script.txt generator

Post by Funkencool »

1) I completely agree, I try avoid copy/paste when I can.

2) is basically unavoidable if there's going to be any configuring, but it can be pretty straight forward for scripts
here's the basic snippet I'm using for adding AI's to the above script

Code: Select all

Script['ai' .. team - 1] = {
	host = 0,
	isfromdemo = 0,
	name = bot.name or 'AI',
	shortname = 'KAIK',
	spectator = 0,
	team = team,
}

Script['team'.. team]  =  {
	allyteam = bot.allyTeam,
	rgbcolor = ''..bot.color[1]..' '..bot.color[2]..' '..bot.color[3],
	side = bot.side,
	teamleader = 0,
}

if not Script['allyteam' .. bot.allyTeam] then 
	Script['allyteam' .. bot.allyTeam]  =  {
		numallies = 0,
	}
end
which gets it's values from the User/UI on a mousepress

3) I've had a lot of trouble in the past using Spring.Restart() in that fashion, so I quite trying.
If it did work, however, It would be easy to change the behavior to that.
Post Reply

Return to “Lua Scripts”