Converting Python Code to LUA ... ?

Thanks for the tips, alpaca, quite to the point! Before getting into Lua I read them and they made absolutely no sense whatsoever. Now after a week and some hours into it they make perfect sense. But this one has me confused:

[*]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.

I don't know if I understand what you're saying here.
#a would indeed be 2, because we only have two sequential elements, [1] and [2]. Sequential elements are stored in the array part, and non-sequential and non-number keys are stored in the hashmap. The length operator applies only to the array part.

Where does that index of the lowest non-empty integer - 1 come from?
 
I don't know if I understand what you're saying here.
#a would indeed be 2, because we only have two sequential elements, [1] and [2]. Sequential elements are stored in the array part, and non-sequential and non-number keys are stored in the hashmap. The length operator applies only to the array part.

Where does that index of the lowest non-empty integer - 1 come from?
It's a Lua convention that numeric arrays start at 1 rather than 0 as in most other programming languages. That's actually something I like because it's more natural ;)

In other words, if you construct an array like x = {"a", "b", "c"}, then x[0] == nil, x[1] == "a" and so on.
 
Definitely, I'm currently doing heavy C# programming daily but zero-based index will always feel unnatural, no matter how deep it manages to make it into my brain's autopilot thinking. The n - 1 rule is by far the most unnatural thing. At least for me, I'm not very good with math visualization (e.g. timezones pop my braincells).

But what still confuses me, is that by your rule we'd have 3 as the lowest non-empty integer for the table t = {"a", "b", "c"}, minus 1 we'd have #t = 2. But #t is 3.
 
Thanks for the tips, alpaca, quite to the point! Before getting into Lua I read them and they made absolutely no sense whatsoever. Now after a week and some hours into it they make perfect sense. But this one has me confused:



I don't know if I understand what you're saying here.
#a would indeed be 2, because we only have two sequential elements, [1] and [2]. Sequential elements are stored in the array part, and non-sequential and non-number keys are stored in the hashmap. The length operator applies only to the array part.

Where does that index of the lowest non-empty integer - 1 come from?

I was wondering how using a hash table for everything could not negative affect performance dramatically. But it sounds like they use a hybrid array or table when necessary data structure for everything. That makes more sense.

Sounds like a language I could spit something out in real fast like javascript. It also sounds like something that would be insane to try to maintain a large application in. I know mods can get pretty big sometimes, what kind of features does it have for code reuse (since it doesn't have classes and inheritance) and things such as importing/namespaces/object scope
 
And I guess this thing with the length appling only to the array part is great for metatables, because a table can be it's own metatable, and metatables basically mean storing methods for custom-defined table operations, such as defining yourself what "+" means between two tables.

mattcrwl, it has various implementations of classes & inheritance, all using tables. The bit about metatables above is one of many powerful features that allow the flexibility to implement OOP. And everything is a first class citizen, too.

Includes, namespaces (don't remember the name they give now) and scope seem to be also covered.

It will actually depend more on what Firaxis will have for us in terms of custom libraries, I guess.
 
Definitely, I'm currently doing heavy C# programming daily but zero-based index will always feel unnatural, no matter how deep it manages to make it into my brain's autopilot thinking. The n - 1 rule is by far the most unnatural thing. At least for me, I'm not very good with math visualization (e.g. timezones pop my braincells).

But what still confuses me, is that by your rule we'd have 3 as the lowest non-empty integer for the table t = {"a", "b", "c"}, minus 1 we'd have #t = 2. But #t is 3.

Sorry, my mistake. It's the lowest empty integer minus one (or the highest non-empty integer that doesn't have a hole somewhere in front of it). I'll fix that in my list.

I was wondering how using a hash table for everything could not negative affect performance dramatically. But it sounds like they use a hybrid array or table when necessary data structure for everything. That makes more sense.

Sounds like a language I could spit something out in real fast like javascript. It also sounds like something that would be insane to try to maintain a large application in. I know mods can get pretty big sometimes, what kind of features does it have for code reuse (since it doesn't have classes and inheritance) and things such as importing/namespaces/object scope

You can implement a kind of class mechanism. To explain that you need to know what metatables are: Each table can be assigned a metatable. If a function is not found in the children of the table, it's looked up in the metatable.

Then you can create an "instance" by creating a table and hooking the "class" as a metatable to it. What Lua will then do is to look in the metatable if something is not found in the table. You can create inheritance by setting up a metatable for your "class" table. I don't think you can have multi-inheritance but take my opinion with a grain of salt there because I'm no real Lua expert.

For namespaces you can use tables (as I said, everything's a table). Generally, each Lua file has its own namespace but you can manipulate them by the setfenv command to create shared namespaces between files. There's also a function called module that is useful for imported things and automatically provides a namespace of its own (I mostly use it for singleton objects and prefer to otherwise execute the files manually so I can setfenv on them but I'm not sure that's good practice)
 
Making a python to lua translation would be quite a pain.
I've written a proprietary-C++-like-language to lua converter at work and it took quite some time, particularly because of a few 'features' which exist only in one of the language (stuff like binary and/or, continue in loops, switch/cases which don't require 'break'...). Python has enough quirks of its own, some of them pretty useful, that don't exist in lua in order to make a translator quite painful to write. The worst part would be the set of python libraries which have no equivalent in lua, and the object/inheritance which doesn't map directly into something lua-esque.

Lua is very much used in games all around, we use it for simulation but it's much like games in terms of technology. I know it's been used in banks.

Its main strengths with regards to python are:
1) It's so much faster.
2) It's way easier to integrate with a C (or C++...) application.
 
Sorry, my mistake. It's the lowest empty integer minus one (or the highest non-empty integer that doesn't have a hole somewhere in front of it). I'll fix that in my list.
That makes sense :) Also, I think 'index' is better wording than integer, since the latter might be taken to mean the value instead of the key.

You can implement a kind of class mechanism. To explain that you need to know what metatables are: Each table can be assigned a metatable. If a function is not found in the children of the table, it's looked up in the metatable.

Then you can create an "instance" by creating a table and hooking the "class" as a metatable to it. What Lua will then do is to look in the metatable if something is not found in the table. You can create inheritance by setting up a metatable for your "class" table. I don't think you can have multi-inheritance but take my opinion with a grain of salt there because I'm no real Lua expert.

You mean multiple inheritance as in interfaces or as in cascading derivation?
Because a metatable is a table itself, so it can have a metatable that's another table, and so own. You can cascade indefinitely.
I think interfacing is just as possible, remember reading that the 'inheritance' in Lua is more like interfaces are used for everything. Still have to dig more into it though.
 
I'm assuming a "table" can only have one meta-table? That would mean multiple inheritance is out. But you could put a meta-table as an element of the original meta-table no? You could have a suedo interface that way.
 
The multiple inheritance where you inherit from multiple parent classes: http://en.wikipedia.org/wiki/Multiple_inheritance

Aren't interfaces better? Sure you have to implement it, but then you can just add a reference to another class that would have the methods already implemented.

Multiple inheritance sounds messy. Should be some wild fun when I get into C++.
 
Aren't interfaces better? Sure you have to implement it, but then you can just add a reference to another class that would have the methods already implemented.

Multiple inheritance sounds messy. Should be some wild fun when I get into C++.

Most modern programming languages don't allow multiple inheritance because not only is it messy, it can become ambiguous. http://en.wikipedia.org/wiki/Diamond_problem
 
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.
It's quite often used by game developers, you just don't always see it exposed obviously since not all games are moddable.
 
How do interfaces work with inheritance? I do very little real programming so all I know about interfaces is that they define a certain view on an object, f.ex. requiring that the object implements a certain function.
 
I would personally recommend Notepad++ or Eclipse depending on whether you want something more lightweight or an IDE. I haven't tried the whole list, though ;)

They probably don't have what I wish they had. If I could dream up an editor then it would have a preview window to see what you're creating while you're creating.
 
How do interfaces work with inheritance? I do very little real programming so all I know about interfaces is that they define a certain view on an object, f.ex. requiring that the object implements a certain function.

Interfaces need some very good planning and design.
Basically they let you define instance members and methods, but not implement them. So as an example, the interface IRanged could be:

interface IRanged {
int range;
void Shoot();
}

So if you have the class Archer that inherits from IRanged, you NEED to implement the Shoot() method inside the Archer class, and it would be wise to initialize the range variable with some value. You need because a interface doesn't say how to Shoot, only that it is possible to Shoot. You can't implement anything inside an interface.

So the definition for the class Archer could well be "Archer : Unit, IRanged, IAncient"
That is, it can inherit from one superclass (given single inheritance and not multiple inheritance) and from as many interfaces as you want.

The advantage is, if you want to have a variable that takes any ranged unit, you can create it from IRanged:

IRanged rangedUnit;

And it will accept an Archer or any other class inheriting from IRanged. It will of course, only be able to access those methods and properties of Archer that come from IRanged, not ones defined inside Archer or inherited from other interfaces.
 
Back
Top Bottom