Author Topic: Tricks and tips for Lua scripting  (Read 13900 times)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Tricks and tips for Lua scripting
« on: June 12, 2011, 12:33:31 AM »
I've been poking around the net recently, and I found some interesting stuff about Lua. A fair bit of it seemed fairly obscure, at least to me, so I thought I'd share it with you guys.


  • Lua is case sensitive. This isn't particulary obscure, and I'm sure you all knew it anyway, but some things that are posted my require this knowledge. FOO =/= foo =/= Foo
  • For the purposes of logic false = nil and nil = false. Also, if I use either nil or false below, assume the other could be used.
  • a, b = b, a will swap the values of the variable a and b. Presumably, though I haven't tested it, a, b, c = b, c, a will transpose all three one space to the left.
  • a = a or b will make a = b, but only if a is nil or false (see above). c = a or b will do the same, but c will be given the value. If a is NOT nil, c =a.
  • There is a very short method of writing simple if...else statements: condition and do if true or do if false  . This is very similar to the previous line, no?
  • conditional1 and a or b  will return a if conditional is true, else it will return b.  --Orion63

Post any other things you know below. You will be credited.
« Last Edit: August 17, 2011, 11:34:27 PM by Pilchard123 »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #1 on: August 17, 2011, 09:07:08 PM »
Block comments are written...

--[[
commented stuff here
]]

If you add an extra - at the start of the block...

---[[
uncommented stuff here
]]

Thanks to Aino and Orion 63 for clarification.
« Last Edit: August 17, 2011, 11:56:39 PM by Pilchard123 »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #2 on: August 17, 2011, 11:06:49 PM »
Heh, though you don't need the '--' at the end of the '--]]--' :P

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #3 on: August 17, 2011, 11:18:57 PM »
At the end? I though you didn't need it at the start...

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Tricks and tips for Lua scripting
« Reply #4 on: August 17, 2011, 11:28:22 PM »
conditional1 and a or b
It will return a if conditional is true, else it will return b.

i.e.
5 == 5 and 1 or 2 -> returns 1
5 == 12 and 1 or 2 -> returns 2

Also I recommend not using the or trick that often, since Eufloria itself(if not restarted) doesn't reset values, and if you're using the trick just to do something if the variable is not initialized, it will create some weird bugs(since the values will actually be initialized from the previous game).

Also, you don't need both '--' at the end, nor at the front.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #5 on: August 17, 2011, 11:31:47 PM »
You need the double at the start of both of them, but not at the end.

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Tricks and tips for Lua scripting
« Reply #6 on: August 17, 2011, 11:42:46 PM »


Hum...not in my editor...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #7 on: August 17, 2011, 11:52:09 PM »
Lol, thats right xD

I face that problem when doing array inside of an arraybox, and I say wrong about it! I mean you need the double ONLY at the first!

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #8 on: August 18, 2011, 12:05:25 AM »
Just OT a sec, is your avatar a glider from Game of Life, Orion?

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Tricks and tips for Lua scripting
« Reply #9 on: August 18, 2011, 12:16:41 AM »
Yap ^^. Good days when I spent entire days, shooting gliders against each other to see if I could make an awesome pattern :-P(Actually, it was last year, but still ^^)

Bonobo

  • Achiever
  • Old Oak
  • ****
  • Thank You
  • -Given: 139
  • -Receive: 12
  • Posts: 670
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #10 on: August 18, 2011, 09:51:09 AM »
Amazing, the story of Conways Game of Life. I am 54 now, and I remember being 11 or 12 years old (1968 or ’69), and I got a “boys’ book” with an article about this “new” game and how scientists “wasted” thousands of $$ “playing” Life on computers the size of houses, and how I played this with checkers stones  … and how exciting this was.

BTW am I the oldest here? Could it be? *1957

Greetz, Tom

Orion63

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 73
Re: Tricks and tips for Lua scripting
« Reply #11 on: August 18, 2011, 10:06:05 AM »
I am way younger(16), so... Game of Life it's pretty much exclusive to the computer ^^, but it's still pretty damn fascinating. Good to know, that there are still enthusiastics out there and that I am not the only one being fascinated by a zero-player game ^^
« Last Edit: August 19, 2011, 12:14:17 AM by Orion63 »

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #12 on: August 18, 2011, 11:57:28 PM »
16? Nice, I'm 15, but you might have recognized from the signature I had :P

Bonobo

  • Achiever
  • Old Oak
  • ****
  • Thank You
  • -Given: 139
  • -Receive: 12
  • Posts: 670
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #13 on: August 19, 2011, 04:23:08 AM »
hahaha, and sorry for the OT, but I must let you know that I remember the time very well when 15 was SOOOO much younger than 16 :D

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #14 on: August 19, 2011, 04:33:11 AM »
"sorry for the ot"? We were way off topic anyways :P

Bonobo

  • Achiever
  • Old Oak
  • ****
  • Thank You
  • -Given: 139
  • -Receive: 12
  • Posts: 670
  • Eufloria: Yes
Re: Tricks and tips for Lua scripting
« Reply #15 on: August 19, 2011, 08:34:09 AM »
Yeah, meant “sorry for carrying the OT even further” ;)