Converting Python Code to LUA ... ?

They use different concepts, so you have to manually rewrite. But since it's different game, I doubt you can reuse any of Civ 4 mod code.

And I advice you to learn Lua - it's very easy and fun.
 
Nuh-uh, the languages are semantically very different. The best starting point for Lua is probably "Programming in Lua", which can be viewed for free on the net in a slightly outdated version. The Lua Wiki is also useful, but equally often slightly outdated. You can translate the logic but will probably have to re-write the code.

Some important quirks you have to get used to when learning Lua (don't try to understand them all right now, but they will come in handy as a reference when you see something weird):

  • Everything is basically a table (i.e. an associative array, or dict in Py slang). Any object can be used as a table key. Elements are accessed by a where a is the table and b is the key object. There is syntactic sugar in the form of a.b = a["b"] where " is the string literal

    [*]The colon notation. a:b() is different from a.b() and means a.b(a)

    [*]Inequality is checked with ~= not !=

    [*]Functions are objects, objects are theoretically callable but will throw an error if the call is undefined.

    [*]Numeric arrays are implemented as tables, too. Numbering starts at 1, in contrast to Python. They have a length function invoked by writing #a which returns the lowest empty index - 1. That is, if a = {[1] = 1, [2] = 2, [4] = 3} then #a = 2. Very weird but that's why I'm mentioning it.

    [*]Lua specifically differentiates between numeric and non-numeric keys. You can loop through numeric keys by using "for key, value in ipairs(table)" and through all keys, including numeric ones by using "for key, vlaue in pairs(table)". The reason for this is that the ipairs function preserves order while the pairs loops through items in arbitrary order

    [*]Tables can have so-called metatables. This can, among other things, make the table callable like a function or override the default behaviour for operators like "+" or "=". Metatables are also tables.

    [*]Non-existent table keys point to the global "nil" object. So if you check for non-existence of a key you don't "if a.haskey(b)" but you check "if a ~= nil". You can also use "if not a" but that will also return true if a == false

    [*]Lua doesn't have a built-in class mechanism. You can find nice implementations of one in the internet, but I'm not sure if the Firaxis devs add one of their own.

    [*]There are no different numeric types, just 64-bit float in the standard implementation and no custom object types. Everything you create will be a table so you have to write your own functions for testing types if you create classes.

    [*]Deleting keys means setting them to "nil", so a = nil will delete the b entry

    [*]Lua has a custom RegExp implementation that doesn't conform to POSIX

    [*]Lua can't read or write binary files very well out of the box. You have to manually convert to and from byte strings


Well that's about what I can think of off the top of my head. I'm not a LuA expert, mind, just using it for modding, too. I might extend this a bit if something else crosses my mind.
 
"Programming In Lua" is a good read. An older version exists for free on the site http://www.lua.org/pil/ and there are only minor changes between the first and second editions.

As for converting Python to Lua...
There are some similarities, and technically they have almost identical capabilities (baring things like coroutines versus threads) but in general Lua has fewer language concepts that do more functionality. "Do more with less" is the Lua motto.
Also, certain techniques are stressed more by the syntax and semantics in one language or another. For instance, Lua stresses closures and first-class functions, whereas Python stresses a more traditional Object-Oriented approach to data-hiding.
 
  • Inequality is checked with ~= not !=
  • Functions are objects, objects are theoretically callable but will throw an error if the call is undefined.
  • Numeric arrays are implemented as tables, too. Numbering starts at 1, in contrast to Python. They have a length function invoked by writing #a which returns the index of the lowest non-empty integer - 1. That is, if a = {[1] = 1, [2] = 2, [4] = 3} then #a = 2. Very weird but that's why I'm mentioning it.

    [...]
  • Lua doesn't have a built-in class mechanism. You can find nice implementations of one in the internet, but I'm not sure if the Firaxis devs add one of their own.
  • There are no different numeric types, just 64-bit float in the standard implementation and no custom object types. Everything you create will be a table so you have to write your own functions for testing types if you create classes.

That all sounds completly strange. Partially like inventing the wheel new (...which is sure not needed), and partially just like...nonsense.
 
Is learning Lua any good outside of Civ V (and apparently WoW)? While I understand python was slow at least it is a language in widespread use.

Thanks for the tutorial and wiki pointers.

It's apparently used as a scripting language (especially for UI) by a fair number of games. Outside of gaming, I lack information. The Total War games since Empire use it, WoW uses it, the newer LucasArts adventures,... You can find a list on Wikipedia: http://en.wikipedia.org/wiki/Category:Lua-scripted_video_games

That all sounds completly strange. Partially like inventing the wheel new (...which is sure not needed), and partially just like...nonsense.

I trust the creators had reasons for implementing things like this. Whether they are good reasons is up to personal opinion. Lua is completely written in ANSI C, which contributes to the quirks. I don't like many of the things, either.

The reason for the length thing is, for example, that the first n filled numeric indices are stored in a vector of length n, while anything that follows gaps (or large gaps, not sure) or uses non-numeric keys is stored in a hash table.

Another example for an, in my opinion, unnecessary thing is that LuA uses a custom implementation of regular expressions, that (by claim of the author, I didn't check it) is a lot shorter but misses some functionality and differs somewhat from the POSIX one.

The table thing is sometimes pretty practical once you got used to it, because you can expect everything basically works the same way no matter what "kind" of object it is. For example you can always count on being able to loop on all attributes and stuff like that.

Edit: Another thing, that's not very relevant for CiV, but that aggravated me some is that Lua is basically unable to read and write anything but text files with any user-friendliness. You can read and write byte strings but have to write custom functions to convert them to and from other objects. This is especially annoying if you want to store tables somewhere.
 
Is learning Lua any good outside of Civ V (and apparently WoW)? While I understand python was slow at least it is a language in widespread use.

And Heroes V.

Lua is specialized embedded script language. It's much better than Python here, since it has very clear access restrictions and very good performance for scripting language due to its simplicity.

If you grade scripting languages by popularity, consider JavaScript as well. With QtScript and Mozilla Rhino it's quite popular in non-game applications.
 
That all sounds completly strange. Partially like inventing the wheel new (...which is sure not needed), and partially just like...nonsense.
it is a reinventation of the wheel however if python is limited to 120KPH Lua goes to 2000KPH
 
That all sounds completly strange. Partially like inventing the wheel new (...which is sure not needed), and partially just like...nonsense.

Reinventing the wheel is justified if the existing wheel is square-shaped...
 
it is a reinventation of the wheel however if python is limited to 120KPH Lua goes to 2000KPH

Reinventing the wheel is justified if the existing wheel is square-shaped...

But both is not the case :dunno:.
The changes seem to be not very intuitive for me.
But alpaca has some good points.
 
Having used both for a few major projects I can safely say that Lua is much faster than Python.
 
Well, thank you for the heads-up, but based on that, I think I won't be learning Lua after all or at least not anytime soon. Sounds too ... strange.
 
Well, thank you for the heads-up, but based on that, I think I won't be learning Lua after all or at least not anytime soon. Sounds too ... strange.

I find it less strange than Python. Anyone who has done BASIC programming will find lua to be easy to get into. Python's meaningful whitespace was much trickier for me.
 
Well, thank you for the heads-up, but based on that, I think I won't be learning Lua after all or at least not anytime soon. Sounds too ... strange.

It's not stranger than any other programming language, it just has some different quirks you have to get used to. I didn't find it more difficult to learn than other programming languages.

I think most of the weirdness is a result of Lua not being implemented with an emphasis on programmer-friendliness but rather on speed and light weight.
 
Well I've started learning it so I'm up and running at game launch :)
Took the book below for a ride as part of my subscription (wouldn't have bought it), though it's focused on WoW there are several intro & advanced chapters on Lua alone:
http://my.safaribooksonline.com/9781430223719
And I'm buying the second current edition from the Lua developers from the ebookstore below (yes, I'm into ebooks!)
http://store.feistyduck.com/products/programming-in-lua

I think the latter is nicely priced and would definitely recommend it (out of reading amazon reviews, not out of reading it myself.. yet ;P)
 
Lua is by far the easiest language I have ever seen and also one of the most flexible. Most Lua code is fairly close to human readable and i don't even think you'd need a book as there are many good tutorials http://lua-users.org/wiki/TutorialDirectory

It also is great at avoiding mod conflicts.

For those of you who were asking if it's worth learning Lua here's a list of games that uses it:
http://lua-users.org/wiki/LuaUses

Just off the top of my head, Natural Selection 2, Supreme Commander series are two popular games that use Lua. There are also lots of frameworks like Love, AGen, or the T-Engine that easily allow you to make entire games using nothing, but Lua.
 
Back
Top Bottom