Lua Beginners FAQ: Difference between revisions

From Spring
Jump to navigationJump to search
Knorke (talk | contribs)
dead link, tutorial links more clear
cleaned up lua beginners faq
Line 7: Line 7:


==Q: I'm interested in learning to use Lua with Spring. Where should I start?==
==Q: I'm interested in learning to use Lua with Spring. Where should I start?==
A: You can start by reading general Lua tutorials  [http://lua-users.org/wiki/LuaTutorial here].
After that, you can try following try using Lua in Spring, for example [[Lua:Tutorial_GettingStarted|by following this tutorial.]]


You can start by reading general Lua tutorials  [http://lua-users.org/wiki/LuaTutorial here].


==Q: Where can I find documentation?==
After that, you could look at the example widget [[Lua:Tutorial_GettingStarted|here]], and try to add some functionality of your own!
A: On the [[Lua_Scripting|Lua_Scripting page.]]


==Q: What can't Lua do in the current version of Spring?==


A: You can't ...
==Q: Where can I find help?==
*control the ground LOS (but you can set the los status of units!).
 
You can find documentation on the [[Lua_Scripting|Lua_Scripting]] page. Discussion of lua coding takes place on the lobby in #moddev and #lua.
 


==Q: How do I open ".lua" files?==
==Q: How do I open ".lua" files?==


A: Notepad or any text editor might be enough. If you are using windows and the text is all garbled, try a programmer's editor. [http://notepad-plus.sourceforge.net/uk/site.htm Notepad++] can read Lua files with syntax highlighting.
Any text editor is enough. [http://notepad-plus.sourceforge.net/uk/site.htm Notepad++] is free, multi-platform and highly recommended. There are also IDEs able to work with Lua, such as Eclipse, if you look for them.




Line 30: Line 29:




==Q: What is the difference between a widget and a gadget?==
==Q: What is the difference between a unsynced and synced lua code?==
 
Synced code is code that runs, identically and at the same time, on every connected client. In practice, this means code which controls the game state. Unsynced code is code that will run differently (or maybe not at all) on each connected client. Typically, this means the user interface.
 
For example, lua code to make units explode if they fell down a cliff would need to be synced. But, code that warned a players when one of their own units was getting near a cliff edge, would need to be unsynced.
 
 
==Q: What is the difference between LuaUI, LuaRules, widgets and gadgets?==
 
There are two so-called [[Lua_System|Lua States]] that are primarily used by game developers. Inside each of these states, the developer can write addons, which are self-contained pieces of code. They are:
* <b>LuaUI</b>: Used for unsynced GUI related code. Addons in here are often called widgets. Clients can (if the game permits it) control which widgets are running. Widgets contain only unsynced code. They can be bundled inside the game, or loaded from the users "Spring/LuaUI/Wigets" directory.
* <b>LuaRules</b>: Addons in here are often called gadgets. Clients cannot choose which gadgets run. Gadgets can include both synced and unsynced code, and must be bundled inside the game.
 
Intended for mappers, there is also LuaGaia, which behaves like LuaRules but is seperated from it. To see which of the lua states has access to which data/functionality, see [[Lua_System|Lua States]] and [[Lua:Callins]].


A: A widget is a Lua script that works with LuaUI. A gadget works with LuaRules, LuaCob or LuaGaia. Widgets allow you to customize your interface, and must be unsynced. They can be bundled with the mod or you can save them in your "Spring/LuaUI/Wigets" directory. Gadgets can be synced or unsynced. LuaRules and LuaCob gadgets are usually part of a mod. LuaGaia gadgets should be part of a map. Gadgets can contain both synced and unsynced code.


==Q: What can't Lua do in the current version of Spring?==


==Q: Where can I find examples?==
Springs Lua interface is very powerful. To work out if some idea is possible you need to ask
* When do I need the engine to tell me something has happened? Check against [[Lua:Callins|Callins]].
* What information will I need to know about the game state? Check against [[Lua_SyncedRead]] (synced) and [[Lua_UnsyncedRead]] (unsynced).
* What do I then need to do about it? Check against [[Lua_SyncedCtrl]] (synced) and[[Lua_UnsyncedCtrl]] (unsynced).
Sometimes you need special functionality, like disk access or OpenGL calls, in which case check against [[Lua_Scripting#Interfaces|Interfaces]].


A: The "Spring\examples\Widgets" directory contains many examples. More can be found in almost every game, especially in the [http://zero-k.info/ Zero-K] game.
For completeness, some examples of things you (currently) can't do:
*control the ground LOS - but you can set the los status of units!
*add custom parameters to CEG definitions.


==Q: Can I reload a script without restarting Spring?==


Yes, you can type "/luaui reload" with "/cheats" enabled.
==Q: Where can I find/edit examples?==
[http://answers.springlobby.info/questions/393/how-can-i-easily-test-my-mod More usefull commands.]


==Q: Why does the print function seem to do nothing?==
The best source of exmaples is inside games themselves, in the LuaUI/widgets and Luarules/gadgets directories. See [[Gamedev:PublicRepos]] for a list of repositories.


A: Its output is displayed in stdout. If you have no easy way to see it, you can is Spring.Echo which prints to the ingame chat and infolog.txt.
To make your own widgets, simply individual .lua files into your Spring/LuaUI/widgets dir (create the dir if it is not present). Game repositories can be checked out into a folder named "SomeAwesomeGame.sdd", placed in your "/Spring/games" dir; this shoudl give a local copy of the game, recognized by Spring, where you can edit files. Make sure to check the licenses before doing this!




==Q: How do I print the "UnitDefs[unitDefID]" or "WeaponDefs[weaponDefID]" tables? "for k, v in ipairs(t) do print(k, v) end" does not behave as expected.==
==Q: Can I reload a script without restarting Spring?==


A: Use "for k,v in UnitDefs[unitDefID]:pairs() do print(k, v) end" instead.
Yes, you can type "/luaui reload" to reload widgets, and if you have "cheats" enabled you can also "/luarules reload".




==Q: Where can I find help?==
==Q: Why does the print function seem to do nothing?==


A: There is a [http://springrts.com/phpbb/viewforum.php?f=23 Lua section] in the Spring forums. Probably the most effective way, though, is to join #lua on the lobby.
Its output is displayed in stdout. If you have no easy way to see it, you can Spring.Echo("some text") which prints to the ingame chat and infolog.txt.


==Q: Who implemented LuaUI, LuaRules and LuaGaia?==


A: They were written by trepan.
[[Category: Lua]]
[[Category: Lua]]

Revision as of 13:58, 13 August 2015

Development < Lua Scripting < Lua Beginners FAQ


This page only tries to answer general questions, for more specific Questions please ask in the forum.


Q: I'm interested in learning to use Lua with Spring. Where should I start?

You can start by reading general Lua tutorials here.

After that, you could look at the example widget here, and try to add some functionality of your own!


Q: Where can I find help?

You can find documentation on the Lua_Scripting page. Discussion of lua coding takes place on the lobby in #moddev and #lua.


Q: How do I open ".lua" files?

Any text editor is enough. Notepad++ is free, multi-platform and highly recommended. There are also IDEs able to work with Lua, such as Eclipse, if you look for them.


Q: What is a call-in? And a call-out?

Call-ins are functions defined in your script that Spring calls when a determined event takes place. For example, the gadget:Initialize() call-in is run by Spring when the widget is loaded. Call-outs are functions defined in Spring you can run at whichever moment you desire. For example, when you call Spring.GetUnitTeam(unitID), the engine returns the teamID of the unit identified by "unitID".


Q: What is the difference between a unsynced and synced lua code?

Synced code is code that runs, identically and at the same time, on every connected client. In practice, this means code which controls the game state. Unsynced code is code that will run differently (or maybe not at all) on each connected client. Typically, this means the user interface.

For example, lua code to make units explode if they fell down a cliff would need to be synced. But, code that warned a players when one of their own units was getting near a cliff edge, would need to be unsynced.


Q: What is the difference between LuaUI, LuaRules, widgets and gadgets?

There are two so-called Lua States that are primarily used by game developers. Inside each of these states, the developer can write addons, which are self-contained pieces of code. They are:

  • LuaUI: Used for unsynced GUI related code. Addons in here are often called widgets. Clients can (if the game permits it) control which widgets are running. Widgets contain only unsynced code. They can be bundled inside the game, or loaded from the users "Spring/LuaUI/Wigets" directory.
  • LuaRules: Addons in here are often called gadgets. Clients cannot choose which gadgets run. Gadgets can include both synced and unsynced code, and must be bundled inside the game.

Intended for mappers, there is also LuaGaia, which behaves like LuaRules but is seperated from it. To see which of the lua states has access to which data/functionality, see Lua States and Lua:Callins.


Q: What can't Lua do in the current version of Spring?

Springs Lua interface is very powerful. To work out if some idea is possible you need to ask

Sometimes you need special functionality, like disk access or OpenGL calls, in which case check against Interfaces.

For completeness, some examples of things you (currently) can't do:

  • control the ground LOS - but you can set the los status of units!
  • add custom parameters to CEG definitions.


Q: Where can I find/edit examples?

The best source of exmaples is inside games themselves, in the LuaUI/widgets and Luarules/gadgets directories. See Gamedev:PublicRepos for a list of repositories.

To make your own widgets, simply individual .lua files into your Spring/LuaUI/widgets dir (create the dir if it is not present). Game repositories can be checked out into a folder named "SomeAwesomeGame.sdd", placed in your "/Spring/games" dir; this shoudl give a local copy of the game, recognized by Spring, where you can edit files. Make sure to check the licenses before doing this!


Q: Can I reload a script without restarting Spring?

Yes, you can type "/luaui reload" to reload widgets, and if you have "cheats" enabled you can also "/luarules reload".


Q: Why does the print function seem to do nothing?

Its output is displayed in stdout. If you have no easy way to see it, you can Spring.Echo("some text") which prints to the ingame chat and infolog.txt.