Practice Lua Scripts

Practice Lua Scripts

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

Moderator: Moderators

Post Reply
User avatar
imboss77
Posts: 11
Joined: 08 Apr 2014, 03:40

Practice Lua Scripts

Post by imboss77 »

I have perused scripts trying to understand what they do and they are all very complex. Are there any scripts in particular that are easier to understand for beginners?
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Practice Lua Scripts

Post by smoth »

Code: Select all

function widget:GetInfo()
	return {
		name			= "Name as shown in widget list",
		desc			= "",
		author			= "john doe",
		date			= "date",
		license		 	= "PD",
		layer		 	= 0,
		experimental	= false,
		enabled	 		= true
	}
end

local stringOfWords = "BLAH BLAH BLAH"
local toggle

function widget:Initialize()
	Spring.Echo(stringOfWords .. " I STARTED LOOK AT ME JIM!")
end


function widget:Update(frame)
	if(toggle == true) then
		Spring.Echo("TOCK")
		toggle = false
	else
		Spring.Echo("TICK")
		toggle = true
	end
end
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Practice Lua Scripts

Post by Jools »

Yes, except that bif this were actually written for beginners in mind it would have more comments in the code, of what it does.
8611
XTA Developer
Posts: 242
Joined: 29 Dec 2014, 08:22

Re: Practice Lua Scripts

Post by 8611 »

Not sure what "difficulty level" you are looking for, so just general answer:
1) Lua itself:
For learning Lua (loops, variables, functions,..) there are better places/tutorials in web than spring offers.
Imo NOT recommend to learn Lua (or even scripting) via spring, it will take longer than reading normal tutorials first.

2) Spring-Lua:
imo problematic to learn from examples because of one or several points:
...long & complicated :arrow: Too hard for beginner
...easy logic but deal with something very specific to spring engine :arrow: "What does this even try to solve?"
...would be 1/5 the size if not for all the "extra situations" that need to be handled :arrow: Tricky to see which parts are really important
...have mistakes that go unnoticed because for this script the mess-up is irrelevant. :arrow: But maybe not for you.
...require other files from game (like units, weapons, other scripts,...) :arrow: Can be annoying.
...written by someone who was/is beginner himself :arrow: Pick up bad habits
Neither is something you would want to learn from.

In general learnining any spring modding by example is not so good. There are some okay examples but just as many bad or outdates ones.
Similiar with forum or chat. In conclusion nothing/noone, including this post, can be trusted and you should remain skeptical against everything.
If you want an example, no need to look very far.
function widget:Update(frame)
The parameter of Update() is not frame.
See wiki for correct..Ops. On wiki it says "none", but description is ok
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Practice Lua Scripts

Post by smoth »

Jools wrote:Yes, except that bif this were actually written for beginners in mind it would have more comments in the code, of what it does.
His next step would be to learn to use the wiki. You are welcome to post a better example documented and all.
8611 wrote: If you want an example, no need to look very far.
function widget:Update(frame)
The parameter of Update() is not frame.
See wiki for correct..Ops. On wiki it says "none", but description is ok
Same as above...
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Practice Lua Scripts

Post by zwzsg »

For the Lua language itself, all you need to know is on this page: http://www.lua.org/manual/5.1/

For the Spring Lua API, use all the pages linked on this page: https://springrts.com/wiki/Lua_Scripting
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Practice Lua Scripts

Post by smoth »

1 week later, still only my example. yet, the people who are quick to raise issue with it the very day I posted it, still have not given the new guy an example.


and I am the only one who sees the issue with this? Poster comes asks for help. I take the time to post a LESS THAN PERFECT EXAMPLE and instead of posting a better one, 2 posters immediately drop everything to chastise it. Why? why is this the case here? Should I post another anything ever for anyone?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Practice Lua Scripts

Post by gajop »

Imo there's no need to post anything else until the user shows more interest. This is OK for now.
User avatar
imboss77
Posts: 11
Joined: 08 Apr 2014, 03:40

Re: Practice Lua Scripts

Post by imboss77 »

Imo NOT recommend to learn Lua (or even scripting) via spring, it will take longer than reading normal tutorials first.
I am using other resources to learn Lua. I am not trying to use Spring to learn Lua. I just like seeing examples of implementation.

I should have clarified what I was asking for. Not necessarily a simple code (your code is much appreciated smoth), but rather moderate code that is neat, to the point and commented. If there is no decent example that any of you know of then I am sorry for wasting your time.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Practice Lua Scripts

Post by gajop »

I can't offer any, and I don't think it's wise searching for "moderate" examples at first (code usually does one specific thing). You can always look at one of the many game repos and try to figure out what's going out from there, but as said, I don't recommend it.
Also, I don't really comment my code usually if there are no special quirks.

The basics that you need to know are:
for functions widgets and gadgets can have to handle events that are invoked from the engine: https://springrts.com/wiki/Lua:Callins
for reading game objects: https://springrts.com/wiki/Lua_SyncedRead https://springrts.com/wiki/Lua_UnsyncedRead
for modifying game objects: https://springrts.com/wiki/Lua_SyncedCtrl https://springrts.com/wiki/Lua_UnsyncedCtrl
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Practice Lua Scripts

Post by smoth »

You are more than welcome to grab my repo and post stuff from any Lua file I can try and direct you towards answer on why some things are there

viewtopic.php?f=9&t=28265
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Practice Lua Scripts

Post by Silentwings »

You've not given enough information to determine what you regard as a "moderate" example. If you linked to the examples which you had found too difficult, or gave some idea of your current knowledge, that would help. Without that all I can offer you is too look inside game repos for yourself, e.g. http://imolarpg.dyndns.org/trac/balates ... ui/widgets, https://github.com/ZeroK-RTS/Zero-K/tre ... UI/Widgets, https://springrts.com/wiki/Gamedev:PublicRepos.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: Practice Lua Scripts

Post by PicassoCT »

Alot of spring people started by making widgets..

On the other hand i started - making maps.. so maybee my advice is kind of weird ..
Help is available, but not always in an instant.. join moddev.. the temple of the temptrees lua, where the mad monks sing strange songs of wright and Rongs


PS: Also dont feel ashamed or stupid to ask. I started more then clueless. Still standing. Understanding. Every day. A little more.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Practice Lua Scripts

Post by Jools »

imboss77 wrote:I have perused scripts trying to understand what they do and they are all very complex. Are there any scripts in particular that are easier to understand for beginners?
ldots! He's asking for a hello world widget or similar. There is some stuff here that's useful: https://springrts.com/wiki/Lua_Scripting

But that links to a forum post which in turn links back to the wiki. Funny. Maybe this is how spring people think.

Moderator (gajop): User was issued for this post. https://springrts.com/wiki/Felony#1.
+ you're wrong (a basic hello world was provided and this is the answer): I should have clarified what I was asking for. Not necessarily a simple code (your code is much appreciated smoth), but rather moderate code that is neat, to the point and commented.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Practice Lua Scripts

Post by Jools »

Are you insane? Why warn me for that?
No flaming or insulting of other users, respect for all other users is mandatory and assumed regardless of post count, amount contributed, or time active.
Who did I insult? And with what?

Moderator (gajop): https://springrts.com/wiki/Felony#1. + https://springrts.com/wiki/Felony#4. User was warned for this post and subsequently banned for the 7day minimum.
User avatar
imboss77
Posts: 11
Joined: 08 Apr 2014, 03:40

Re: Practice Lua Scripts

Post by imboss77 »

Thank you all for your responses and help. I wish I could go full steam ahead, but sadly school isn't kind. I have a huge project I need to present this weekend, and I have to work on it this week. Thanks again for all the responses. After this weekend I'll post progress I've made.
Post Reply

Return to “Lua Scripts”