Extremely basic LUA help is needed

Gold1Gold1 Join Date: 2012-12-03 Member: 174022Members
Hi, this is the first time in my life that I try to mod. I have basically no idea what I am doing and unfortunately there are no tutorials for modding Natural Selection 2. (I also have very little experience in programming)

I am still trying to understand the Lua syntax, and would like an answer to this question:


When I open the Client.lua in ns2, I see these lines -

// Precache the common surface shaders.
Shared.PrecacheSurfaceShader("shaders/Model.surface_shader")
Shared.PrecacheSurfaceShader("shaders/Emissive.surface_shader")
Shared.PrecacheSurfaceShader("shaders/Model_emissive.surface_shader")
Shared.PrecacheSurfaceShader("shaders/Model_alpha.surface_shader")


If I understand correctly PrecacheSurfaceShader() is a function, right? Well I go to Shared.lua and I can't see where it is created... I am probably way off in my understanding of something and since it is super basic I want to be corrected.


And another thing... what does ":" mean ? Like here for example -
GetGUIManager():CreateGUIScript("GUIFeedback")


If someone can help me, please be very clear and very basic. Do not assume I know anything at all.


Thanks again for any help

Comments

  • remiremi remedy [blu.knight] Join Date: 2003-11-18 Member: 23112Members, Super Administrators, Forum Admins, NS2 Developer, NS2 Playtester
    The : is "syntactical sugar". What it really means is...
    <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->local self = GetGUIManager();
    self.CreateGUIScript(self,"GUIFeedback");<!--c2--></div><!--ec2-->

    You have the right idea about PrecacheSurfaceShader (in terms of where to look), but it is actually a function defined on the C side (it's an engine API call). So you won't be able to find that one in the open-source portion of the game.
  • JimWestJimWest Join Date: 2010-01-03 Member: 69865Members, Reinforced - Silver
    edited December 2012
    This function is written in C++ (directly in the engine), you can't see its code, only call it.
    It Precaches the files so it can refer to them in the memory in game and don't have to load it (better performance).

    The : calls the function from a object, which are declared in a class.

    for example,
    function Player:GetCrouching()
    return self.crouching
    end

    If "self" is a object from the class Player, you can call self:GetCrouching().
  • BrainsBrains Join Date: 2012-12-03 Member: 174081Members
    I haven't tried modding NS2 yet, but I work with Lua at my current job.

    The difference between ":" and "." is an annoying hard to understand quirk of Lua. The best way to think about it is probably "immediacy".

    The "." operator is usually used for a field directly on the thing to the left. It's the "close" one. Usually variables: E.G. "MyThingy.name"

    The ":" operator is usually used for things not directly on the thing to the left. It's the "far" one. This is usually methods and is the most common one. If you start chaining calls then you'll probably be using ":" as well. E.G. "MyThing:CollectAllNames()"

    Your best bet is to use ":" by default and run + check for errors. If it's a variable, or something complex coming from another file/code, consider "." and look through the rest of the file to see how it's being used.

    Hope that helps.
  • JimWestJimWest Join Date: 2010-01-03 Member: 69865Members, Reinforced - Silver
    if you use the ., you need to do it like this:
    Player.GetCrouching(self)
  • BrainsBrains Join Date: 2012-12-03 Member: 174081Members
    Yeah, in Lua:

    MyThing:CollectAllNames()

    is the exact same as

    MyThing.CollectAllNames(MyThing)

    Which is why I'm recommending ":" whenever possible, unless it's a variable or from a library/other complex piece of code.

    Usually it's:
    self.ImmediateVariable
    self:MyMethod()
  • Gold1Gold1 Join Date: 2012-12-03 Member: 174022Members
    Wow, I didn't expect answers so fast! Thanks!

    Is there a way to search the content of the Lua files?
    Say, I want a mod that changes the Team Balance ratio (for example - 2 marines for every 1 alien), how do I find the correct file and location? Do I make an educated guess?

    And are those kind of things are as simple as changing a variable's value?
  • SoundFXSoundFX Join Date: 2003-08-21 Member: 20048Members
    edited December 2012
    <!--quoteo(post=2039435:date=Dec 3 2012, 04:35 PM:name=Gold1)--><div class='quotetop'>QUOTE (Gold1 @ Dec 3 2012, 04:35 PM) <a href="index.php?act=findpost&pid=2039435"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->Wow, I didn't expect answers so fast! Thanks!

    Is there a way to search the content of the Lua files?
    Say, I want a mod that changes the Team Balance ratio (for example - 2 marines for every 1 alien), how do I find the correct file and location? Do I make an educated guess?

    And are those kind of things are as simple as changing a variable's value?<!--QuoteEnd--></div><!--QuoteEEnd-->

    As a fellow noob I will suggest the educated guess path, it leads to exploration and hopefully understanding. In case you are stuck though, I glanced into TeamJoin, Team, and TeamInfo and they might pertain to what you are looking for. More specifically TeamJoin, It was a quick glance, so explore a bit.
Sign In or Register to comment.