Hello World in Lua ?

Hello World in Lua ?

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

Moderator: Moderators

Post Reply
simboy
Posts: 2
Joined: 25 Jan 2007, 08:29

Hello World in Lua ?

Post by simboy »

Hi !

I'm trying to start playing with LUA in Spring, in order to create custom commands callable in game, but I'm having troubles in finding a starting point. I didn't find any tutorial for such a thing, but I may not have looked at the proper place... Does anyone know where I can find documentation about that ? Alternatively, what I would like to be able to do, to start with, is just a "Hello World", somehting that would print "Hello World" on the Spring screen if I write e.g.:

/luaui hello

Any help would be greatly appreciated.

Thanks a lot,
User avatar
quantum
Posts: 590
Joined: 19 Sep 2006, 22:48

Post by quantum »

Hello, you can start by going through the following tutorial:

http://lua-users.org/wiki/TutorialDirectory

After that, you can check out the .lua widget files and look up in \Spring\LuaUI\API.txt the functions they use. When you start writing your own widgets, don't hesitate to borrow as much code as you can from existing widgets!

Writing "hello world" in Spring is slightly more complex than stand-alone Lua.

Code: Select all

Spring.SendCommands({"echo hello world"})
This function sends the commands to the spring prompt as though you typed in directly:

Code: Select all

/echo hello world
Your need to insert it in a widget to execute it.

Code: Select all

function widget:GetInfo() -- Required: the widget won't load without it.
	return {
		name      = "My First Widget",
		desc      = "Displays 'hello world'",
		author    = "quantum",
		date      = "Jan 27, 2007",
		license   = "GNU GPL, v2 or later",
		layer     = 0,
		enabled   = true  --  loaded by default?
	}
end

function widget:Initialize() -- Runs as the widget starts.
	Spring.SendCommands({"echo hello world"})
end
Save this as init_helloWorld.lua in your Widgets folder.
Last edited by quantum on 27 Jan 2007, 13:22, edited 1 time in total.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Moved from Mods to Development.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

A couple of good sources for lua information:

http://www.lua.org/pil/

http://www.lua.org/manual/5.0/manual.html
simboy
Posts: 2
Joined: 25 Jan 2007, 08:29

Post by simboy »

Thanks a lot, guys ! I've been able to run quantum's "hello world" example and I'm starting to play with Spring scripting. Sounds like a lot of fun !
Post Reply

Return to “Lua Scripts”