NS2: School of LUA

2

Comments

  • FocusedWolfFocusedWolf Join Date: 2005-01-09 Member: 34258Members
    edited February 2010
    <!--quoteo(post=1752581:date=Feb 12 2010, 01:09 AM:name=cmc5788)--><div class='quotetop'>QUOTE (cmc5788 @ Feb 12 2010, 01:09 AM) <a href="index.php?act=findpost&pid=1752581"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->C# can't be as fast as C/C++. It's not designed to be and it doesn't try to be.

    "Embedded C#" in the sense that Lua or Python are embedded in a game doesn't make a lot of sense; it's not designed to be a scripting language.<!--QuoteEnd--></div><!--QuoteEEnd-->

    After playing my fair share of garrysmod i can competently say that even Lua sucks if you have that much scripted code... I'd say the game sucks up gigabytes of ram just to get to the main menu. Server stability isn't much better as they crash practically every 15 to 20 minutes. Naturally garrysmod without any addons runs as good as hl2 and starts up instantly.

    And the devs say that ns2 is entirely written in LUA with just the engine being in C++.... they're also using flash for all the interfaces (think Crysis does this also... there goes about 200 mb of ram... when you consider all the little animated flashy things for ammo readout, hud, and commander view :P). That should amount to even more scripted code then what garrysmod manages... and i recall early on the developers were actually in communication with the developer of garrysmod so... almost makes me wonder if we're gonna see similar performance.

    Just going to be interesting to see if the game runs that good in the end. Could explain why the devs don't release little snippits of the game to us... for example that shooting range mockup would be interesting to play right?
  • Dalin SeivewrightDalin Seivewright 0x0000221E Join Date: 2007-10-20 Member: 62685Members, Constellation
    Its too bad you're judging Lua in its entirety from a bad experience you had with GMod. There could be a dozen reasons why GMod could run slow. Perhaps the implementation of Lua is horrible/hacky? Maybe the Lua scripts are horribly written?

    <a href="http://vendetta-online.com/" target="_blank">Vendetta Online</a> uses Lua for pretty much -everything- on the client and loads up/runs faster than HL2 will EVER run. HL2 adds a lot of crap on the background as well, which would also slow down the Lua interpreter. Again not sure how GMod is implemented but I'm pretty sure it goes "Source Engine Crap->GMod Crap->Lua" and by the time you actually get to the Lua script files you're already looking at two layers of mods on top of the original engine.

    Lua isn't as fast as C/C++, but it isn't slow by any means, if implemented correctly.
  • cmc5788cmc5788 Join Date: 2009-10-06 Member: 68959Members
    edited February 2010
    <!--quoteo(post=1752660:date=Feb 12 2010, 10:26 AM:name=FocusedWolf)--><div class='quotetop'>QUOTE (FocusedWolf @ Feb 12 2010, 10:26 AM) <a href="index.php?act=findpost&pid=1752660"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->After playing my fair share of garrysmod i can competently say that even Lua sucks if you have that much scripted code... I'd say the game sucks up gigabytes of ram just to get to the main menu. Server stability isn't much better as they crash practically every 15 to 20 minutes. Naturally garrysmod without any addons runs as good as hl2 and starts up instantly.

    And the devs say that ns2 is entirely written in LUA with just the engine being in C++.... they're also using flash for all the interfaces (think Crysis does this also... there goes about 200 mb of ram... when you consider all the little animated flashy things for ammo readout, hud, and commander view :P). That should amount to even more scripted code then what garrysmod manages... and i recall early on the developers were actually in communication with the developer of garrysmod so... almost makes me wonder if we're gonna see similar performance.

    Just going to be interesting to see if the game runs that good in the end. Could explain why the devs don't release little snippits of the game to us... for example that shooting range mockup would be interesting to play right?<!--QuoteEnd--></div><!--QuoteEEnd-->

    You're generalizing about Lua in a way that proves you don't understand it. Lua is a completely modifiable language -- individuals who choose to implement it on a large scale project can and should build from source with customized options for memory management, etc. Lua isn't like many other languages in that you can actually edit the source files that control how the language is interpreted.

    If GMOD's poor performance is owed to its Lua implementation (which I don't think is entirely true -- probably more to do with ineptly-written scripts), then that doesn't carry an ounce of meaning for something else like NS2 that implements Lua.

    Speaking of which, all of World of Warcraft's interface is written in Lua. You can say a lot of things about World of Warcraft, but one of them is NOT that it's inefficient or unstable.
  • AezayAezay Join Date: 2003-04-19 Member: 15660Members
    I've never coded in Python, could someone explain what the "significant whitespace" is all about?
  • cmc5788cmc5788 Join Date: 2009-10-06 Member: 68959Members
    edited February 2010
    <!--quoteo(post=1752722:date=Feb 12 2010, 01:46 PM:name=Aezay)--><div class='quotetop'>QUOTE (Aezay @ Feb 12 2010, 01:46 PM) <a href="index.php?act=findpost&pid=1752722"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I've never coded in Python, could someone explain what the "significant whitespace" is all about?<!--QuoteEnd--></div><!--QuoteEEnd-->

    Yeah. It means indentation is an actual part of the code, it won't compile unless (for instance) everything within an if statement is indented exactly once more than the statement line. It's not like C where you could, theoretically, just make the entire program a huge block of text without ever hitting enter and using the spacebar rarely.
  • AezayAezay Join Date: 2003-04-19 Member: 15660Members
    <!--quoteo(post=1752724:date=Feb 12 2010, 08:49 PM:name=cmc5788)--><div class='quotetop'>QUOTE (cmc5788 @ Feb 12 2010, 08:49 PM) <a href="index.php?act=findpost&pid=1752724"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Yeah. It means indentation is an actual part of the code, it won't compile unless (for instance) everything within an if statement is indented exactly once more than the statement line. It's not like C where you could, theoretically, just make the entire program a huge block of text without ever hitting the spacebar.<!--QuoteEnd--></div><!--QuoteEEnd-->
    That's not too bad then, when I started coding ~10 years ago, I was very bad at creating a "pretty" syntax, but I've since then, through bad experience returning to old code, learned that a proper syntax means a lot. For example I used to use space indentation, instead of tabs.
    But as Lua is very relaxed with the indentation/syntax in that regard, there is no stopping someone from using the same kinda of indentation/syntax in Lua as they are in Python, I guess the problem comes when trying to read other's code.
  • Draco_2kDraco_2k Evil Genius Join Date: 2009-12-09 Member: 69546Members
    edited February 2010
    <!--quoteo(post=1752722:date=Feb 12 2010, 10:46 PM:name=Aezay)--><div class='quotetop'>QUOTE (Aezay @ Feb 12 2010, 10:46 PM) <a href="index.php?act=findpost&pid=1752722"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I've never coded in Python, could someone explain what the "significant whitespace" is all about?<!--QuoteEnd--></div><!--QuoteEEnd-->
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->It means this.
      
      As                 opposed to      
                               this.<!--c2--></div><!--ec2-->

    Basically it means you can't organise your code visually.

    EDIT: Slowpoke is slow.
  • CrispyCrispy Jaded GD Join Date: 2004-08-22 Member: 30793Members, Constellation
    edited February 2010
    Significant whitespace is kind of like how bullet point groups work in MS Word, except without the bullet points and the relationship is more dependant. If you press tab in a bullet-point list you can make a sub-group (e.g. you can change <b>2</b> to <b>i</b>, with <b>i</b> being a sub-group of <b>1</b> - i.e. <b>1.i</b>). The significant whitespace thing here is basically saying that everything at the same indentation level refers back to and is part of everything on the previous indentation level, unless there is a double-linebreak.

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->def LearnLanguage(user,language):
        if (user.Alive() == False):
            print("No Zombies!")
        if (language == "Lua"):
            print("Get out!")
        if (language == "Python"):
            site = "http://www.python.org"
            user.visit(site)<!--c2--></div><!--ec2-->
    So in this code, as I've understood it, the 3 'if' statements are all part of the 'definition' for LearnLanguage because they are one line below and at the same indentation, and the consequence of each if statement is indented in once and on the line below. To me it seems like a nice and clean way to work. It seems like it's enforcing people to make their code clearly legible, or there will be consequences. I guess some people don't like the idea of there being consequences for not writing to a specific standardised format.

    ---
    I'm all up for Lua. As a languages graduate I'm interested in learning a more abstract language (and as an aspiring designer I think it will be a useful skill to have). I'm hoping the fact I won't need to learn several thousand words of vocabulary or worry about register or pronunciation will make it easier. As far as I can see it's mostly vocabulary (learning functions and logic functions), punctuation (modifiers and exactitude), syntax (word order) and morphology (lexemes and lemmas).
  • MidoMido Join Date: 2004-04-05 Member: 27742Members
    <!--quoteo(post=1752576:date=Feb 12 2010, 12:52 AM:name=FocusedWolf)--><div class='quotetop'>QUOTE (FocusedWolf @ Feb 12 2010, 12:52 AM) <a href="index.php?act=findpost&pid=1752576"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->If Ns2 had embedded C#... well it'd probably be a memory hog (compared to LUA) but the syntax would be so much more powerful IMHO... And it can even stay multi-platform (although it'd be better if it used straight up .net framework imho lol) <a href="http://www.mono-project.com/Embedding_Mono" target="_blank">http://www.mono-project.com/Embedding_Mono</a><!--QuoteEnd--></div><!--QuoteEEnd-->

    C#, as others have said, is simply not fit to be a scripting language for this kind of game model. An important thing to consider when choosing a scripting language for NS2 is to really think about the scripts that the serverside will be sending to the clientside.

    Since any server can send clientside scripts, what's to stop someone from writing a script that scrambles your windows folder, or tries to find sensitive information, or straight up take control of your computer? The developer has to be able to restrict the language's abilities to import and execute system level code. A common issue with extending/embedding python, much to my frustration, is that you need to clip it's wings, but it takes a lot of effort.

    Lua on the other hand, is perfect for embedding because the language is limited by default, and very easy to "add" to. Python you have to "take away" and it's simply less secure unless you're really thorough. Your C# fantasy suffers from the fact that there is no support or real benefit to implementing/embedding it in this context over something more fit like Lua.

    <!--quoteo(post=1752734:date=Feb 12 2010, 03:06 PM:name=Draco_2k)--><div class='quotetop'>QUOTE (Draco_2k @ Feb 12 2010, 03:06 PM) <a href="index.php?act=findpost&pid=1752734"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Basically it means you can't organise your code visually.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Nobody really does though. Every C++ program you'll ever look at has a tab/3-4 space for each nest, and the only real "creativity" I see in how the code looks is whether or not functions are written like
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->void myFunc() {
       // stuff
    }<!--c2--></div><!--ec2-->
    or like
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->void myFunc()
    {
        // stuff
    }<!--c2--></div><!--ec2-->
    and function naming.
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->my_function()
    MyFunction()
    myFunction()<!--c2--></div><!--ec2-->

    Find me a decently written C++ program that actually "organizes it's code visually" and doesn't just follow the absolute standard convention that everyone uses for readability's sake anyway.

    <!--quoteo(post=1752443:date=Feb 11 2010, 10:06 AM:name=cmc5788)--><div class='quotetop'>QUOTE (cmc5788 @ Feb 11 2010, 10:06 AM) <a href="index.php?act=findpost&pid=1752443"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I've also done quite a few things with Python, spent a few years getting into the intricacies of it<!--QuoteEnd--></div><!--QuoteEEnd-->
    This makes me nervous. Because a huge part of Python is that it's NOT intricate, you can pick it up and be absolutely productive within a day, and understand it almost fully after skimming a few docs. So it makes me nervous to hear that you 'spent a few years getting into the intricacies of it' unless you have no prior programming knowledge. The language is SO simple at it's core. Everything is an Object implicitly, functions, variables, classes, all Objects, so they can all exist in arrays/tuples/attributes/descriptors of other Objects. It's the nicest language to work with in that sense. You rarely have to be type sensitive (and can be, in the line of code necessary to check types when rarely necessary).
    It's beauty is simply in that it trims away all the excess fat with type sensitive/whitespace insensitive languages. You don't need to open/close nests with opening/closing symbols (easily cuts down on the vertical fat by 20-50%), you don't need to be type sensitive (declaring types with variable declarations), easily making the code more straight forward (duck typing allowing you to not worry about typing issues)

    A definite reason to pick Lua over Python for NS2 is that Python 2.6.4 and below all use a global-scope interpreter, which isn't a problem by itself, but when you want to do multi-threading, you have to hold a lock to the global interpreter (GI), meaning that only one thread can think with python at a time. Lua is designed such that you can run multiple Lua interpreters inside of multiple threads, which may or may not be a killer feature for the UWE team (they'll probably never read this crap anyway :P)
  • Draco_2kDraco_2k Evil Genius Join Date: 2009-12-09 Member: 69546Members
    <!--quoteo(post=1752888:date=Feb 13 2010, 12:12 PM:name=Mido)--><div class='quotetop'>QUOTE (Mido @ Feb 13 2010, 12:12 PM) <a href="index.php?act=findpost&pid=1752888"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Find me a decently written C++ program that actually "organizes it's code visually" and doesn't just follow the absolute standard convention that everyone uses for readability's sake anyway.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Christ.

    Well, you're the boss, my only real experience with coding is that of doing some silly things in GMod. I was just explaining what "significant whitespace" is.
  • cmc5788cmc5788 Join Date: 2009-10-06 Member: 68959Members
    edited February 2010
    <!--quoteo(post=1752888:date=Feb 13 2010, 03:12 AM:name=Mido)--><div class='quotetop'>QUOTE (Mido @ Feb 13 2010, 03:12 AM) <a href="index.php?act=findpost&pid=1752888"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->This makes me nervous. Because a huge part of Python is that it's NOT intricate, you can pick it up and be absolutely productive within a day, and understand it almost fully after skimming a few docs.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Probably a poor choice of words. It would be more accurate to say "I spent a few years doing things with it." I started coding out of my childhood habit of playing text-based RPGS (MUDs, I'm sure a few of you know what I'm talking about). So when I decided to learn to program, I started out with C, and my first challenge was to code platform-dependent netcode in C so that my pet game projects would work.

    Anyhow, I'm not just tooting my own horn... point is, it took me months to get something functional going like that in C with no prior experience. I picked up Python and coded a fully functional server and database model for a MUD in less than a day. Python doesn't have a lot of "intricacies" in terms of syntax or usage. It might take a little longer to gain complete mastery of, say, the standard library -- but even that isn't too bad if you're accustomed to a monster of a standard library from something like Java.

    I have nothing against Python as a functional language. I just think that significant whitespace is stupid and unnecessary. It's not a hazard to the efficacy of the language, and I can see why some people would appreciate a standardized look and feel to reading community code -- but I'm not one of those people. I like the freedom to change my indentation patterns to make small inline functions one-liners, embed nested for loops for iterating over multi-dimension arrays, etc. It feels right, and it viscerally enrages me that a language would tell me "no" to my style of coding.

    <!--quoteo(post=1752888:date=Feb 13 2010, 03:12 AM:name=Mido)--><div class='quotetop'>QUOTE (Mido @ Feb 13 2010, 03:12 AM) <a href="index.php?act=findpost&pid=1752888"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->It's beauty is simply in that it trims away all the excess fat with type sensitive/whitespace insensitive languages. You don't need to open/close nests with opening/closing symbols (easily cuts down on the vertical fat by 20-50%), you don't need to be type sensitive (declaring types with variable declarations), easily making the code more straight forward (duck typing allowing you to not worry about typing issues)<!--QuoteEnd--></div><!--QuoteEEnd-->

    I can't speak for everyone, but I wouldn't call any of that "unnecessary fat." After years of coding in C/C++, when browsing over a new piece of code for the first time, my focus is initially driven to the typenames. Seeing what type things are declared as (especially if the programmer followed good convention and typedef'd or #DEFINE'd his own custom types) is one of the easiest ways to begin understanding what a section of code does.

    As for the braces, I feel that Python's "condensed" style becomes even more unreadable in the long-term. Your scroll button might be a bit more worn down, but braces give the feel of something organized and readable, developed into manageable chunks rather than just... huge blocks. Most peoples' Python code looks to me like what I would expect out of a novice C programmer who doesn't understand how unreadable his code will be later, all compressed into unbroken blocks without any distinguishing characteristics for the eyes to adjust to important segments within function blocks, etc.

    <!--quoteo(post=1752888:date=Feb 13 2010, 03:12 AM:name=Mido)--><div class='quotetop'>QUOTE (Mido @ Feb 13 2010, 03:12 AM) <a href="index.php?act=findpost&pid=1752888"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->A definite reason to pick Lua over Python for NS2 is that Python 2.6.4 and below all use a global-scope interpreter, which isn't a problem by itself, but when you want to do multi-threading, you have to hold a lock to the global interpreter (GI), meaning that only one thread can think with python at a time. Lua is designed such that you can run multiple Lua interpreters inside of multiple threads, which may or may not be a killer feature for the UWE team (they'll probably never read this crap anyway :P)<!--QuoteEnd--></div><!--QuoteEEnd-->

    That's interesting, I didn't know that. Still, asynchronous programming is probably mostly handled in lower-level engine code rather than the scripting layer.
  • MidoMido Join Date: 2004-04-05 Member: 27742Members
    <!--quoteo(post=1752935:date=Feb 13 2010, 10:15 AM:name=cmc5788)--><div class='quotetop'>QUOTE (cmc5788 @ Feb 13 2010, 10:15 AM) <a href="index.php?act=findpost&pid=1752935"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->That's interesting, I didn't know that. Still, asynchronous programming is probably mostly handled in lower-level engine code rather than the scripting layer.<!--QuoteEnd--></div><!--QuoteEEnd-->

    I could see an engine taking classes defined from script and finding ways to thread them.

    I appreciate the calm discussion and it's safe to say we both have pretty valid reasons for liking/disliking the syntax of python.

    <3
  • AezayAezay Join Date: 2003-04-19 Member: 15660Members
    <!--quoteo(post=1752979:date=Feb 13 2010, 09:08 PM:name=Mido)--><div class='quotetop'>QUOTE (Mido @ Feb 13 2010, 09:08 PM) <a href="index.php?act=findpost&pid=1752979"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I could see an engine taking classes defined from script and finding ways to thread them.<!--QuoteEnd--></div><!--QuoteEEnd-->
    Just for clarification, there are no classes in Lua, you can however "emulate" them using metatables.
  • Dalin SeivewrightDalin Seivewright 0x0000221E Join Date: 2007-10-20 Member: 62685Members, Constellation
    Isn't that all a Class really is anyways? Plain old Lua has no real implementation for Object-oriented programming (AFAIK) and you're right that you have to use some horrible (or awesome, depending on what side of the table you're on) manipulation of metatables to allow the creation of objects (which of course, isn't really creation, more... copying, right? Thats how I understand it anyways).

    The thing is though, Sparks implementation of Lua, at least from the very little I've seen of it, looks like it has more in common with a language that has a native concept of Object-oriented-ness, which IMO, Lua severely lacks by default. The metatable manipulation will likely be a hidden Feature of the language that one could take advantage of (then again, it might not, although I don't see any reason why they would disable access to metatables), and I don't think they've stated what version/distribution of Lua they're using? I think LuaJIT is pretty popular and I don't know how all that different it is when compared to the normal Lua interpreter.

    So no, object-oriented-ness doesn't exist in Lua, but for those who are going to be using Lua to mod Natural Selection 2, they (hopefully) won't notice the difference. Chances are good that we'll be able to define classes just like one would in Java or C++.
  • AezayAezay Join Date: 2003-04-19 Member: 15660Members
    edited February 2010
    <!--quoteo(post=1753007:date=Feb 13 2010, 11:09 PM:name=Dalin Seivewright)--><div class='quotetop'>QUOTE (Dalin Seivewright @ Feb 13 2010, 11:09 PM) <a href="index.php?act=findpost&pid=1753007"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Isn't that all a Class really is anyways? Plain old Lua has no real implementation for Object-oriented programming (AFAIK) and you're right that you have to use some horrible (or awesome, depending on what side of the table you're on) manipulation of metatables to allow the creation of objects (which of course, isn't really creation, more... copying, right? Thats how I understand it anyways).<!--QuoteEnd--></div><!--QuoteEEnd-->
    I probably cannot explain it as well as the <a href="http://www.lua.org/pil/" target="_blank">PIL manual</a>, but one of the uses that I find really neat about meta tables is that you can use them for meta functions.
    As an example, lets say I create a meta table which I intend to use for the weapons in NS2, I could then in the metatable add some functions like <!--coloro:yellow--><span style="color:yellow"><!--/coloro-->GetAmmoCount()<!--colorc--></span><!--/colorc-->, <!--coloro:yellow--><span style="color:yellow"><!--/coloro-->GetAmmoCountMax()<!--colorc--></span><!--/colorc-->, <!--coloro:yellow--><span style="color:yellow"><!--/coloro-->Reload()<!--colorc--></span><!--/colorc--> etc etc.
    Then for each weapon table, I will be able to call the functions in the meta table, so you could write <!--coloro:yellow--><span style="color:yellow"><!--/coloro-->WeaponHMG:Reload()<!--colorc--></span><!--/colorc--> or <!--coloro:yellow--><span style="color:yellow"><!--/coloro-->WeaponPistol:GetAmmoCount()<!--colorc--></span><!--/colorc-->, but each weapon table will use the same functions you defined in the meta table.

    Personally, I don't think Lua truly starts to shine, until you learn about metatables, so this is not something that will be cut or become a "hidden feature".

    After having coded in Lua for a few years, I have not once missed any kind of object oriented features of the language, although, my projects might have been relatively small. Tables in Lua just have such a wide use, that they can easily fill out the role for object oriented support.
  • cmc5788cmc5788 Join Date: 2009-10-06 Member: 68959Members
    edited February 2010
    As a general response to the last few posts:

    Object-orientation is a concept. Lua allows its implementation. The method of implementation is as robust as the comprehension of the script writer.

    That's simply all there is to it. The "OOP language" paradigm has been blown way out of proportion. If you really want to (and many companies do), you can use basic C in an "object-oriented" way with structures, etc. "Enforcing" object-oriented code is not nearly the same as supporting it.

    Almost anything "supports" object-oriented code if you really know what it is and you feel that is the best way to model your program. If you're been trained exclusively as an OOP programmer, you should read Bjarne Stroustrup's "The C++ Programming Language" to get a good, intuitive feel for using OOP in a logical way. Some projects simply don't have a scope that allows OOP to be useful or necessary.
  • MidoMido Join Date: 2004-04-05 Member: 27742Members
    <!--quoteo(post=1753054:date=Feb 14 2010, 01:53 AM:name=cmc5788)--><div class='quotetop'>QUOTE (cmc5788 @ Feb 14 2010, 01:53 AM) <a href="index.php?act=findpost&pid=1753054"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->you should read Bjarne Stroustrup's "The C++ Programming Language" to get a good, intuitive feel for using OOP in a logical way.<!--QuoteEnd--></div><!--QuoteEEnd-->

    I can definitely approve this recommendation. It's sitting on my shelf :)
  • Thraka2Thraka2 Join Date: 2002-12-03 Member: 10426Members
    <!--quoteo(post=1753007:date=Feb 13 2010, 07:09 PM:name=Dalin Seivewright)--><div class='quotetop'>QUOTE (Dalin Seivewright @ Feb 13 2010, 07:09 PM) <a href="index.php?act=findpost&pid=1753007"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Isn't that all a Class really is anyways? .... .... ....

    So no, object-oriented-ness doesn't exist in Lua, but for those who are going to be using Lua to mod Natural Selection 2, they (hopefully) won't notice the difference. <!--coloro:#FF8C00--><span style="color:#FF8C00"><!--/coloro-->Chances are good that we'll be able to define classes just like one would in Java or C++<!--colorc--></span><!--/colorc-->.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Can you elaborate on <!--coloro:#FF8C00--><span style="color:#FF8C00"><!--/coloro-->this<!--colorc--></span><!--/colorc-->?
  • JAmazonJAmazon Join Date: 2009-02-21 Member: 66503Members
    Just wrote a 3D renderer in C++ and now I'm raring to try my hands at NS2 modding :D
  • AezayAezay Join Date: 2003-04-19 Member: 15660Members
    <!--quoteo(post=1757415:date=Mar 6 2010, 12:05 AM:name=Thraka2)--><div class='quotetop'>QUOTE (Thraka2 @ Mar 6 2010, 12:05 AM) <a href="index.php?act=findpost&pid=1757415"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Can you elaborate on <!--coloro:#FF8C00--><span style="color:#FF8C00"><!--/coloro-->this<!--colorc--></span><!--/colorc-->?<!--QuoteEnd--></div><!--QuoteEEnd-->
    As it has been said, you cannot define classes in Lua, but I'm guessing he is referring to a table with a metatable.__index containing functions, which in practice works like a class.
  • Dalin SeivewrightDalin Seivewright 0x0000221E Join Date: 2007-10-20 Member: 62685Members, Constellation
    edited March 2010
    <!--QuoteBegin-"Thraka2"+--><div class='quotetop'>QUOTE ("Thraka2")</div><div class='quotemain'><!--QuoteEBegin-->Can you elaborate on this?<!--QuoteEnd--></div><!--QuoteEEnd-->

    I was referring to the possiblity that the Lua source code might have been modified to allow for easier, less messy syntax for creating Classes, similar to Java where all methods of the class you're definining exist within that definition instead of outside of it. So instead of messing around with metatables (as Aezay has said, __index and __methods, although I don't think either of those are required to implement classes nicely) and having to decide between "function myClass::myFunction(a,b,c)" and "function myClass.myFunction(self, a,b,c)" it may be simplified to something you might see in UnrealScript or Java:
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->class myClass extends myOtherClass
        AVariable = 10
        function myFunction(a,b,c)
             self.AVariable = 1337
             AVariable = 1337 //Does the same as above    
        end
    end<!--c2--></div><!--ec2-->

    The above code shows the definition of a class called "myClass" which extends "myOtherClass". AVariable is a variable that is a class-member and myFunction is a a class method. This would require some decent modification to the Lua Lexer/Parser etc. It essentially would do all the work of creating metatable entries and will automatically associate everything inside the 'class' block with the newly created metatable entry.

    This of course might be out of the scope of what they wanted for the Lua scripting language, and modifying the Syntax of Lua might make it difficult for Decoda to play nice with it (in fact, I decided to work on my own simple scripting language rather than go forward with modifying Lua to support this). This is sort of a "Gosh, I hope they did something like this" but <b>in reality, it probably won't be like this at all.</b>

    In reality, if they've taken steps to simplify the OOP process at all, it might be as simple as having a built in function called "class" that does all the magic for us. This method is far more common. So something like this:

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->myClass = class("myClass")
    {
        AVariable = 10
    }

    myClass::myFunction(a,b,c)
    {
       AVariable = 1337
    }<!--c2--></div><!--ec2-->

    Might be closer to what we end up getting.
  • Thraka2Thraka2 Join Date: 2002-12-03 Member: 10426Members
    Wouldn't it be nicer to use a language that supported OOPs in some way instead of hacking it in?

    Though they say that they are making the entire game logic that way so they must have something that makes it easy to code weapons and stuff... I've never been that impressed with scripting languages that don't have those productive capabilities. You end up writing more code to get complex things done. VBScript seems to have more capabilities (granted i've not looked too into LUA, but I play with PawnScript for SourceMod) even Unreal Script has classes..

    Meh.
  • puzlpuzl The Old Firm Join Date: 2003-02-26 Member: 14029Retired Developer, NS1 Playtester, Forum Moderators, Constellation
    LUA supports OO concepts fine enough. Don't get hung up on the limitations of the tools you use to solve problems, instead you need to think of how you can use LUA constructs to help you organise your code and data sensibly.

    If you look at perl5, it has the most bizarre method of doing OO, but it works and as long as you know what you are doing you have some serious power at your disposal. Now, I'm not suggesting that NS2 LUA is going to be anything like perl5, but it will be possible to do sensible things like abstracting interfaces, inheritence etc.

    You will, for example, be able to take the stock NS2 lua code and derive your own flavour of a class very easily by defining your custom behaviour and then binding it to the base class.

    I wouldn't get too hung up on language syntax. And do not get into the mindset of blaming LUA itself for shortcomings in your projects.

    I mean, I write OO code in C at work. I have 15+ years of C/C++/Java and I have zero problems with organising code cleany such that interfaces are clean, data is hidden and the code is very extensible. I've written Python, Ruby, C#, Fortran, Pascal, C++, Objective C.... and the one thing I know for sure is that language zealots can go sit in a corner and argue about whitespace vs curly brackets all night long while I'll just get on with solving problems with the tool at hand.
  • MidoMido Join Date: 2004-04-05 Member: 27742Members
    <!--quoteo(post=1757718:date=Mar 6 2010, 06:20 PM:name=puzl)--><div class='quotetop'>QUOTE (puzl @ Mar 6 2010, 06:20 PM) <a href="index.php?act=findpost&pid=1757718"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I mean, I write OO code in C at work. I have 15+ years of C/C++/Java and I have zero problems with organising code cleany such that interfaces are clean, data is hidden and the code is very extensible. I've written Python, Ruby, C#, Fortran, Pascal, C++, Objective C.... and the one thing I know for sure is that language zealots can go sit in a corner and argue about whitespace vs curly brackets all night long while I'll just get on with solving problems with the tool at hand.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Rock on. Though at some level you have to have a general preference, which was the spark of some debate earlier (but not a debate I'd consider zealous).
  • Thraka2Thraka2 Join Date: 2002-12-03 Member: 10426Members
    With pawnscript I get pretty frustrated because of the lack of classes. they have this sort of way of mixing enum's with arrays, but it feels hokey. They have all these strange data structure supplements that add tons of code just to work around. I guess we'll just have to wait and see what we get to work with. :) I hope it will be great :)

    I just got the alpha editor, which is ok, but I really just want to code! :)
  • AdvertAdvert Join Date: 2010-01-10 Member: 70040Members
    I believe the developers' time would be better spent fine-tuning the game instead of making a secondary way of creating objects in Lua.
  • Dalin SeivewrightDalin Seivewright 0x0000221E Join Date: 2007-10-20 Member: 62685Members, Constellation
    <!--quoteo(post=1757931:date=Mar 7 2010, 08:14 PM:name=Advert)--><div class='quotetop'>QUOTE (Advert @ Mar 7 2010, 08:14 PM) <a href="index.php?act=findpost&pid=1757931"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I believe the developers' time would be better spent fine-tuning the game instead of making a secondary way of creating objects in Lua.<!--QuoteEnd--></div><!--QuoteEEnd-->

    Something like this would make fine-tuning the game and creating new content for the game theoretically easier.

    But its not something that they would really be working on right now (adding syntactical sugur for OOP support) as it would probably had to have been done before this much work was done on the game, else they would have to modify all of their existing game code. (so that it looks the same, the functionality between the syntactical sugar and the "usual" way of doing OOP in Lua would already be identical in the backend of things) This discussion about OOP and syntax wasn't really a "they need to do this NOW before they release the game" but more of a "I really hope they've done it this way, as it would benefit everyone." at least from my point of view.
  • AdvertAdvert Join Date: 2010-01-10 Member: 70040Members
    edited March 2010
    <!--quoteo(post=1757935:date=Mar 8 2010, 02:37 AM:name=Dalin Seivewright)--><div class='quotetop'>QUOTE (Dalin Seivewright @ Mar 8 2010, 02:37 AM) <a href="index.php?act=findpost&pid=1757935"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Something like this would make fine-tuning the game and creating new content for the game theoretically easier.

    But its not something that they would really be working on right now (adding syntactical sugur for OOP support) as it would probably had to have been done before this much work was done on the game, else they would have to modify all of their existing game code. (so that it looks the same, the functionality between the syntactical sugar and the "usual" way of doing OOP in Lua would already be identical in the backend of things) This discussion about OOP and syntax wasn't really a "they need to do this NOW before they release the game" but more of a "I really hope they've done it this way, as it would benefit everyone." at least from my point of view.<!--QuoteEnd--></div><!--QuoteEEnd-->
    There is syntax sugar for OOP in Lua:

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->A = {}

    function A:stuff()

    end
    -- translates into:

    function A.stuff(self)

    end

    -- using it:

    A:stuff()

    -- translates into

    A.stuff(A)<!--c2--></div><!--ec2-->

    It's not a very good idea to modify Lua's source for this, as we then have some mangled dialect of Lua that we'll have to modify old libraries for use. In addition, when Lua 5.2 is finished, it'll be much more time consuming to update the version the game is using.

    You can emulate support for this, however:
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function new(tbl, ...) -- note that tbl is the metatable
        local obj = {}
        setmetatable(obj, tbl)
        if obj.__init then
            obj:__init(unpack(arg)) -- call an init function if it exists, this can be used
                         -- to do standard variables, or one-time setup stuff.
        end
        return obj
    end



    do -- class X, example vector
        X = {}
        X.__index = X
        -- these will serve as defaults
        X.x = 0
        X.y = 0
        X.z = 0

        function X:__add(b)
            if getmetatable(b) == getmetatable(self) then -- using getmetatable(self)
                                                           -- instead of X, so you can
                                                           -- extend the class without problems
                local x = self.x + b.x
                local y = self.y + b.y
                local z = self.z + b.z
                return new(getmetatable(self), x, y, z)
            else
                error("Unable to add vector with "..type(b))
            end
        end

        function X:__sub(b)
            if getmetatable(b) == getmetatable(self) then -- same as above applies here
                local x = self.x - b.x
                local y = self.y - b.y
                local z = self.z - b.z
                return new(getmetatable(self), x, y, z)
            end
        end


        function X:__init(x, y, z)
            self.x = x
            self.y = y
            self.z = z
        end

        function X:__tostring()
            return string.format("Vector[%d, %d, %d]", self.x, self.y, self.z)
        end
    end


    A = new(X, 20, 40, 80)
    B = new(X, -40, 30, 123)

    C = A+B
    D = A-B
    E = C+D

    print(A, B, C, D, E)<!--c2--></div><!--ec2-->

    Output:

    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->>lua -e "io.stdout:setvbuf 'no'" "oop.lua"
    Vector[20, 40, 80]    Vector[-40, 30, 123]    Vector[-20, 70, 203]    Vector[60, 10, -43]    Vector[40, 80, 160]
    >Exit code: 0<!--c2--></div><!--ec2-->

    I suggest you download Lua for windows and play around with it a bit.

    If you want, I could possibly make a sample vector object with some more comments, and show you how you would extend it.

    <a href="http://www.lua.org/pil/16.html" target="_blank">Programming In Lua</a> shows you how to do this pretty clearly, if you haven't already read it.

    PS, I know some things are missing in the example, too lazy to fix them now :V
  • AezayAezay Join Date: 2003-04-19 Member: 15660Members
    I'm really confused as to why there seem to be this huge obsession about having strict OOP in NS2, what do you need it for exactly?
    Unless you fully understand how Lua works with its tables, metatables, and how scripting in NS2 is going to be implemented, there is no reason for calling a need on OOP.

    Lua is a really powerful language, once you learn it.
  • CowThingCowThing Join Date: 2010-03-03 Member: 70818Members
    I've been using Lua with Garry's Mod for about half a year now, I can't wait to try it out with NS2.
Sign In or Register to comment.