Teach me how to learn the Lua interface

Teach me how to learn the Lua interface

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

Moderator: Moderators

Post Reply
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Teach me how to learn the Lua interface

Post by PauloMorfeo »

From this thread: viewtopic.php?f=15&t=34296 came the idea of creating a Lua "Widget" to watch a replay and extract game-state data while the simulation is replaying the match.

I moved on to learn Lua and troll thoroughly around the Wiki and did my first experiments. Ex. the one found in: https://springrts.com/wiki/Lua:Tutorial_GettingStarted

Then I moved to something serious and from the Callins list: https://springrts.com/wiki/Lua:Callins I found:

Code: Select all

*.DefaultCommand (type, id)
*.CommandNotify (cmdID, cmdParams, cmdOptions)
Which I promptly experimented with by:

Code: Select all

function widget:CommandNotify (cmdID, cmdParams, cmdOptions)
	--Spring.Echo ("CommandNotify(): " .. cmdID .. " | " .. cmdParams .. " | " .. cmdOptions)
	Spring.Echo ("CommandNotify(): " .. cmdID)
	return false
end
Only to find out the code (commented) would blow up, complaining it was trying to concatenate something that was a table (parameter cmdParams, if I'm not mistaken). And suddenly I realized, I have no idea what type of objects (nor really their meanings) those parameters are so I need to start with that.

I could ask what the parameters are, and that would be helpful, but that would only help me solve this issue. What about when I run into not knowing the params of the next function and the next and the next? I don't want to spend all my time nagging people here in the forums nor have to wait.

So, my question is, how can I find out more info about the Lua interface - how can I learn what types of Lua objects the engine is providing/expecting from my Lua script?


Note: I am a professional Software developer and have basic but decentish knowledge of C++. I can troll through some of the engine's source code, if it doesn't require to correlate a maze of functions in files here and there.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6194
Joined: 29 Apr 2005, 01:14

Re: Teach me how to learn the Lua interface

Post by FLOZi »

If a parameter ends in an s expect a table, anything ending in 'ID' is likely an integer number and anything else is either gonna be a string, float or boolean.

You can avoid your original problem by not concatenating but providing multiple arguments:

Code: Select all

Spring.Echo ("CommandNotify(): ", cmdID, " | ", cmdParams, " | " , cmdOptions)
If you're really desperate just use

Code: Select all

Spring.Echo(type(foo))
:P
User avatar
Silentwings
Moderator
Posts: 3668
Joined: 25 Oct 2008, 00:23

Re: Teach me how to learn the Lua interface

Post by Silentwings »

For callins, from https://springrts.com/wiki/Lua:Callins you infer the type from the name. Since there are only numbers, strings, bools, and tables to pick from, that's generally enough. If you are actually stuck, there is type(...) which returns (as a string) the type of the variable in question.

For callouts, from e.g. https://springrts.com/wiki/Lua_SyncedCtrl, the wiki tells you.

For generic lua functions (such as the concaternation operator) see Luas own lua wiki & tutorials, http://lua-users.org/wiki/LuaTutorial or just google it.

For what its worth, I dislike dynamically typed languages too, but this is by far not the maddest thing in lua's syntax.
8611z
Posts: 169
Joined: 08 Jul 2015, 20:20

Re: Teach me how to learn the Lua interface

Post by 8611z »

What about when I run into not knowing the params of the next function and the next and the next?
trial & error, looking how gadgets of other mods use the same function (github has in-browser search), looking at engine.

The parameters of the callin-functions are not documentated in detail, but you can learn about them by looking at their "active" control-counter-parts.
For example the control-counter-part of CommandNotify() would be GiveOrderToUnit() :
https://springrts.com/wiki/Lua_SyncedCtrl#Give_Order
parameters of GiveOrderToUnit wrote: ( number unitID,
number cmdID,
params = {number, etc...},
options = {"alt", "ctrl", "shift", "right"} ) -> nil
The parameters are often similiar enough.

Only to find out the code (commented) would blow up, complaining it was trying to concatenate something that was a table (parameter cmdParams, if I'm not mistaken)
Instead of
Spring.Echo ("cmdParams=" .. cmdParams)
try:
Spring.Echo ("cmdParams=" , cmdParams)
then it will not do the can-not-concatenate error, but instead output: <table> which gives you the type.
Another reason why that is useful is that common event are variable with value nil (empty, null) which would cause an error if concatenate via .. is used but with comma , it nicely outputs <nil>
gajop
Moderator
Posts: 3040
Joined: 05 Aug 2009, 20:42

Re: Teach me how to learn the Lua interface

Post by gajop »

I agree with everyone else regarding the comma separation, but just so you know, you can also use tostring(x) which will always produce a string (even if x is nil).
User avatar
Beherith
Moderator
Posts: 4983
Joined: 26 Oct 2007, 16:21

Re: Teach me how to learn the Lua interface

Post by Beherith »

Very helpful lua function to deal with pretty-printing nested tables and all sorts of unknown data structures:

I recall that gajop made a lua chili console replacement widget that allowed arbitrary execution of lua code directly from the console, its name is 'chonsole'
viewtopic.php?t=34069

Code: Select all

function to_string(data, indent)
	local str = ""

	if(indent == nil) then
		indent = 0
	end

	-- Check the type
	if(type(data) == "string") then
		str = str .. ("    "):rep(indent) .. data .. "\n"
	elseif(type(data) == "number") then
		str = str .. ("    "):rep(indent) .. data .. "\n"
	elseif(type(data) == "boolean") then
		if(data == true) then
			str = str .. "true"
		else
			str = str .. "false"
		end
	elseif(type(data) == "table") then
		local i, v
		for i, v in pairs(data) do
			-- Check for a table in a table
			if(type(v) == "table") then
				str = str .. ("    "):rep(indent) .. i .. ":\n"
				str = str .. to_string(v, indent + 2)
			else
				str = str .. ("    "):rep(indent) .. i .. ": " .. to_string(v, 0)
			end
		end
	elseif (data ==nil) then
		str=str..'nil'
	else
		--print_debug(1, "Error: unknown data type: %s", type(data))
		str=str.. "Error: unknown data type:" .. type(data)
		Spring.Echo('X data type')
	end

	return str
end
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: Teach me how to learn the Lua interface

Post by PauloMorfeo »

Thanks everyone. This has been really helpful.
Silentwings wrote:... For what its worth, I dislike dynamically typed languages too, ...
Not only I dislike weakly typed languages (because of this problem), I also dislike jumping into a new language head-on because it's going to take quite a while until I know the language well and I'll always run into these kinds of annoying little things:
FLOZi wrote:...
You can avoid your original problem by not concatenating but providing multiple arguments:

Code: Select all

Spring.Echo ("CommandNotify(): ", cmdID, " | ", cmdParams, " | " , cmdOptions)
...
Of course, having said that, it's very cool that Spring allows us to execute pure code (not just a few pre-defined functions and a few functionalities).


8611z, you sent me a PM but apparently you've configured your account to reject receiving PMs. This is what I tried to PM you:
Eia, those wiki pages look really nice, very helpful. Why didn't you posted them on the public thread? Also, bla bla bla bla bla
gajop
Moderator
Posts: 3040
Joined: 05 Aug 2009, 20:42

Re: Teach me how to learn the Lua interface

Post by gajop »

PauloMorfeo wrote:Not only I dislike weakly typed languages (because of this problem)
Lua is still strongly typed though (although less than Python for example), it's just not statically typed.
Post Reply

Return to “Lua Scripts”

cron