Choose a random item from a table

Lua indexes tables from 1, unlike C, Java etc. which indexes arrays from 0. That means, that in your table, the valid indexes are: 1, 2, 3, 4. What you are looking for is the following: print( myTable[ math.random( #myTable ) ] ) When called with one argument, math.random(n) returns a random integer from 1 … Read more

Difference between ‘ and ” within Lua

Nope. No difference, except that you can enclose the other inside the ones that you are using. — No difference between these myStr = “Hi!!” myStr=”Hi!!” myStr = [[Hi!!]] — The ‘weird’ way to make a string literal IMO… — Double quotes enclosed in single quotes myStr=”My friend said: “Hi!!”” — Single quotes enclosed in … Read more

How to compile Lua scripts into a single executable, while still gaining the fast LuaJIT compiler?

Translate all of the Lua source code files to object files and put them in a static library: for f in *.lua; do luajit -b $f `basename $f .lua`.o done ar rcus libmyluafiles.a *.o Then link the libmyluafiles.a library into your main program using -Wl,–whole-archive -lmyluafiles -Wl,–no-whole-archive -Wl,-E. This line forces the linker to include … Read more