Jump to content
  • entries
    12
  • comments
    82
  • views
    45,599

If you are used to C++ these are a few strange things that you will find in Lua.


Chris Vossen

3,838 views

 Share

For those of you who are more familiar with C style syntax here are a few oddities that initially threw me off:

 

The not equals in lua has a tilde

self.entity ~= nil

 

if you want to "not" a boolean you have to use the keyword "not"

if not self.target then

 

All "if" statements you have to do include 3 keywords if, then, end

if self.stunned==true then return end

 

While loops have 3 keywords while, do, end

While x < 10 do x = x+1 end

 

This is what a for loop looks like:

for i = 0, 10, 2 do
   print ( i )
end

 

in C that same for loop would be:

for (i = 0; i <= 10, i += 2)
{
   printf("%d \n",i);
}

 

Notice in the Lua for loop that exit condition is always less than or equal to <= so be careful!

 

There are no +=, -=, /=, or *= in lua

 

String concatenation is ".." rather than +

 

There is no need to end each line with a semicolon although it can be used to break up lines of code. This way you don't have to dedicate a whole line to a small declaration.

x = 10 ; y = 10;

 

Lua uses tables for everything, instead of arrays and maps. (A table is basically a map though..)

 

tables are declared with {}

Script.state={}

 

you can add specific keys and values to this table by:

Script.state.idle=0
Script.state.walk=1
Script.state.chase=2
Script.state.attack=3
Script.state.hurt=4
Script.state.dying=5
Script.state.dead=6

 

Then if you want to know the size of the table use the symbol #

#self.state

will return 7

 

In Darkness awaits I always use the short hand function call.

Time:GetCurrent()

This is expanded to the long hand: (I think this is the correct syntax)

Time.GetCurrent(Time)

The short hand is cleaner but when you get an error in one of the parameters the debugger will be wrong in the argument number. For example say you called gotopoint with too many arguments (there should only be 5 not 6

self.entity:GoToPoint(20,2,0,0,1.4,1)

The debugger would complain and say "argument #7 is 'number';" and you'll think to yourself, there is no argument 7 you crazy debugger.... but this is because the function is expanded to the long hand form so it is actually calling

self.entity.GoToPoint(self.entity,20,2,0,0,1.4,1)

and you can see the extra argument is actually in the 7th position.

 

Well off the top of my head these are all I can think of. I know there are more. For more in depth lua info check out: http://www.lua.org/pil/contents.html#P1

  • Upvote 1
 Share

2 Comments


Recommended Comments

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...