Welcome Back! Please login below. If you are not yet a member, why not register? Enjoy!

Join the forum, it's quick and easy

Welcome Back! Please login below. If you are not yet a member, why not register? Enjoy!

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Cubies Lua tutorial - Part 2 31We have now opened a new Build server! IP: 188.165.193.102:27016Cubies Lua tutorial - Part 2 31

    Cubies Lua tutorial - Part 2

    jakegadget
    jakegadget
    VIP Member
    VIP Member

    Number of posts : 68
    Age : 31
    Location : california
    In-Game Name : Companion Cube |STZ|

    Credits Credits :
    Cubies Lua tutorial - Part 2 Left_bar_bleue0 / 1000 / 100Cubies Lua tutorial - Part 2 Right_bar_bleue

    Warnings! Warnings! :
    Cubies Lua tutorial - Part 2 Left_bar_bleue0 / 100 / 10Cubies Lua tutorial - Part 2 Right_bar_bleue

    Registration date : 2009-07-04
    Points : 104
    Reputation : 6
    20091210

    * Cubies Lua tutorial - Part 2

    Post by jakegadget

    Hi all, again.

    Its time to learn how to setup our gamemode. Im assuming you downloaded skeleton gamemode. Extract it somewhere, desktop, and you should have a folder called skeleton. This needs to go into garrysmod/gamemodes. Im assuming you alread understand the gmod file structure, for gamemodes, addons, etc. Inside is info.txt. This is our first stop. Its fairly self explanitory. You can ignore info, icon, hide for now.
    Code:

    "Gamemode"
    {
       "name"      "TUTORIAL GM"
       "version"   "0.2"

       "author_name"   "Companion Cube"
       "author_email"   "N/A"
       "author_url"   "www.bams-server-forum.com"
       
       "icon"      ""
       "info"      ""
       "hide"      ""
    }

    This is very important to modify, as it will look stupid if you submit it un modified.
    Now we get to the code. Go into gamemode (Relative to your gamemode's directory.) Open up shared.lua. Modify the part with the author info:
    Code:

    GM.Name    = "Tutorial GM"
    GM.Author    = "Companion Cube"
    GM.Email    = "N/A"
    GM.Website    = "www.bams-server-forum.com"

    Go ahead and delete the GM:Initialize. We wont be needing this here.

    Teams
    The first thing we will do with our freshly created GM (Gamemode) is add teams. Two teams, plus a spectator, seems good. This is your first piece of code! Go to here and read about teams. You dont have to understand it 100%, but Its nice to see a bit of example code.
    Gmod lua wiki

    As you can see, the setup code for teams is part of the team library.
    Library
    A library is just that. It is a code followed by a "." that contains multiple functions. team.SetUp() is just one of many.

    Function, Arguments
    Functions are bits of code in lua that do things. They make our lives easier. Functions usually take arguments. Not like fighting over a bottle of Jack Daniels. These send info the the function so that it can work. For example, when Gmod calls PlayerConnected(), it passes the player that connected to the function, so that it can do things to him.

    team.SetUp() has 4 arguments, but we will only use 3 for this point. They are:
    Team number, Team name, Team color

    Lets go over one last thing before we jump in and create our teams. When ever we want to work with these teams, we will need to specify which team. Remembering that 1 is red, 2 is blue, etc can be confusing. Instead, we will create something similar to a constant (non changing) in Lua. To let our selves know that this is NOT to be modified, we will put it in CAPS. Then, we will assign these CAPITAL variables the number of their team. Then, whenever we want to access team 1, which is the red team, we can just use TEAM_RED. Add this to your code:
    Code:

    //Some "Constants" for our teams
    TEAM_RED = 1
    TEAM_BLUE = 2
    TEAM_SPEC = 3

    Now, we can setup our teams using our new "constants".
    Add this:
    Code:

    team.SetUp (TEAM_RED, "Red", Color (255, 0, 0, 255))
    team.SetUp (TEAM_BLUE, "Blu", Color (0, 0, 255, 255))
    team.SetUp (TEAM_SPEC, "Spectator", Color (255, 255, 255, 255))

    Notice how I used Color() to define my color?
    Teh wiki of win

    When gmod asks for a color, it usually wants the "color" data type. The function Color(r,g,b,a) creates a color data and returns it to us. So, we could use things like:
    Code:

    mycolor = Color(1,1,1,255)
    or
    Code:

    ply:SetColor(Color(123,123,123,123))

    Data types
    There are many Data Types in garrysmod. Some common ones are:
    String,
    Integer,
    Float,
    Color

    Now, I think the correct term for these is object, but for the purposes of this tutorial, we will call them data types.

    So, whenever you want to use a Color, you use Color(red,green,blue,alpha). Alpha is the transperency.

    To recap:
    Our code should look like this so far:
    Code:

    GM.Name    = "Tutorial GM"
    GM.Author    = "Companion Cube"
    GM.Email    = "N/A"
    GM.Website    = "www.bams-server-forum.com"

    TEAM_RED = 1
    TEAM_BLUE = 2
    TEAM_SPEC = 3

    team.SetUp (TEAM_RED, "Red", Color (255, 0, 0, 255))
    team.SetUp (TEAM_BLUE, "Blu", Color (0, 0, 255, 255))
    team.SetUp (TEAM_SPEC, "Spectator", Color (255, 255, 255, 255))

    We now have three teams, a red, a blue, and a white spectator.

    Next we will begin to work on classes.
    Share this post on: reddit

    Bam

    Post Thu Dec 10, 2009 4:49 am by Bam

    "Not like fighting over a bottle of Jack Daniels." - Because I'd win, hands down. Wink

    Seem easy enough to understand so far =] I'm going to try it when its not 1:48am Very Happy

    Keep it up Smile
    The Erik

    Post Wed Jan 20, 2010 10:26 am by The Erik

    Stumbled upon this again today, since i forgot to tell you how awesome it is that you care to make such an easy guide for this =]
    chargers5583

    Post Thu Jan 21, 2010 12:22 am by chargers5583

    The Erik wrote:Stumbled upon this again today, since i forgot to tell you how awesome it is that you care to make such an easy guide for this =]

    I bet that makes him very happy!

    Post  by Sponsored content


      Current date/time is Fri Apr 26, 2024 11:15 pm