Getting Started with a Logging Gadget/Widget

Getting Started with a Logging Gadget/Widget

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

Moderator: Moderators

Post Reply
GamerSg
Posts: 13
Joined: 23 Aug 2010, 09:50

Getting Started with a Logging Gadget/Widget

Post by GamerSg »

Im trying to build a logging gadget/widget which can log all events in the game. I believe a widget is not suited for this purpose since it cannot be notified of the events of the opposing player.

My approach atm is to use the relevant call ins like gadget:UnitCreated to write to a log file. However i keep getting errors that io is a nil value. I suppose Spring disallows access to the Lua API for gadgets?

Code: Select all

function gadget:Initialize()
	Spring.Echo("UAI Logger Inited")
	Spring.Echo(gadget)
	file = assert( io.open("UAILog.txt", "w") )  --Error:io is nil
end
Can someone guide me on how i can get started for logging?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Getting Started with a Logging Gadget/Widget

Post by knorke »

Hi
I believe a widget is not suited for this purpose since it cannot be notified of the events of the opposing player.
yes, otherwise cheating would be very easy ;)
As spec or in replays widgets can see everything though.
Can someone guide me on how i can get started for logging?
http://springrts.com/phpbb/viewtopic.ph ... 1&start=42
See the attachment in this post.

I have not tried gagdets yet but as far as I understand you can not just put them in your spring\luaui\widgets folder, they must be bundled into the .sdz of the mod.
GamerSg
Posts: 13
Joined: 23 Aug 2010, 09:50

Re: Getting Started with a Logging Gadget/Widget

Post by GamerSg »

Hi knorke

Thanks for your reply. The gadget part is a non-issue as the game is intended to be played internally(locally), so all players will have the gadget installed. I have no problems atm to retrieve information about what the enemy/myself have built. The problem seems to be simply that i do not have access to the io libraries. I get the following error
LuaRules::RunCallIn: error = 2, GameOver, [string "LuaRules/Gadgets/uailogger.lua"]:40: attempt to index global 'io' (a nil value)
stack traceback:
[string "LuaRules/Gadgets/uailogger.lua"]:40: in function 'writeLog'
[string "LuaRules/Gadgets/uailogger.lua"]:50: in function 'GameOver'
[string "LuaRules/gadgets.lua"]:1029: in function <[string "LuaRules/gadgets.lua"]:1027>
(tail call): ?
Just to clarify, im using the Spring:1944 mod. Could it be that io access in Gadgets is prevented by Spring or the 1944 team? Here are the relevant 2 functions

Code: Select all

function writeLog()
	local replayInfo = Game.mapName .. "_" .. Game.modShortName .. "_" .. tostring(game_id) .. "_"
	local file = assert( io.open("Logs\\UAILog " .. replayInfo .. ".txt", "w") )
	for k,v in pairs(log) do
		file:write(v)
		print(v)
	end
	file:close()
end

function gadget:GameOver()
	Spring.Echo("Shutting Down UAILogger\n")
	writeLog()

end

The error occurs at io.open.
User avatar
BrainDamage
Lobby Developer
Posts: 1164
Joined: 25 Sep 2006, 13:56

Re: Getting Started with a Logging Gadget/Widget

Post by BrainDamage »

basically, while synced part of gadgets can access the info for the whole map, they cannot do operations that differ from pc to a pc ( direct file access is one, the file could be different between machines ) by design, therefore you cannot access files using the synced part

to do what you want, you need to make the synced part send the data to unsynced, then the unsynced part will write it in a file
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Getting Started with a Logging Gadget/Widget

Post by AF »

an alternative is to replay the game afterwards as a demo and run the widget then with all players in view.
GamerSg
Posts: 13
Joined: 23 Aug 2010, 09:50

Re: Getting Started with a Logging Gadget/Widget

Post by GamerSg »

Thanks for the replies. I guess i will go with the widget route then and log it during the replay.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Getting Started with a Logging Gadget/Widget

Post by knorke »

as the maker of the only partly awesome demonaut* i hope you too want to create awesome game statistics and not make a boring debug tool or something! :)

* http://knorke.net23.net/demonaut.html , http://knorke.net23.net/battlereports
Post Reply

Return to “Lua Scripts”