Results 1 to 9 of 9

Thread: Sky's Basic Lua Example Guide

  1. #1
    Join Date
    March 25th, 2010
    Posts
    1,442
    Level
    58

    Sky's Basic Lua Example Guide

    This barely scratches the surface. There are a ton more commands than just this, and there's a lot more you can do with Lua than is covered in this guide. All this does is just get you into a very tiny bit of what Lua can do.

    Also note that there are multiple ways to do Lua functions! My way isn't the only way. It's probably much more improper my way as I am starting out myself, so I recommend NOT using this as a de facto guide for Lua!


    Keep in mind that I'm also fairly new to Lua, so if you find an error in my coding, please point it out.

    NOTE: It's Lua, not LUA. Lua is not an acronym!

    Lua is a simplified programming language that was introduced to SRB2 in version 2.1. This is a guide for beginner Lua users.

    This guide is mostly just me having basic examples and explaining part by part.

    The newest version of SLADE now has and automatically selects the SRB2 Lua language by default.

    Reference Links
    Lua Scripting in SRB2 2.1 by JTE
    Lua basics guide (VERY helpful!)
    Monster Iestyn's Lua documentation thread by Monster Iestyn
    Monster Iestyn's Lua documentation (Skybase mirror)
    SRB2 wiki's article on Lua

    The Guide

    Here are important keywords you'll need to know:

    do
    if
    else
    for
    function
    or

    View Monster Iestyn's Lua documentation thread for more keywords and information.

    Comments are signified by these symbols:

    -- comment
    --[[ comment ]]
    // comment
    /* comment */

    Here is a simplified version of one of my Lua examples:

    Code:
    addHook("ThinkFrame", do
     for player in players.iterate
      if player.mo.state==S_PLAY_STND
      or (player.mo.state>=S_PLAY_TAP1 and player.mo.state<=S_PLAY_TAP2)
       hud.enable ("rings")
       hud.enable ("time")
       hud.enable ("score")
       hud.enable ("lives")
      else
       hud.disable ("rings")
       hud.disable ("time")
       hud.disable ("score")
       hud.disable ("lives")
      end
     end
    end)
    addHook: Adds a hook to the game. ThinkFrame is specified essentially for modifying objects mid-level. do tells the game to, well, do what comes after it.
    for: For the player (players.iterate)
    if: If the player state (player.mo.state) is equal to (==) S_PLAY_STND (player standing state), or if the player.mo.state is greater than or equal to S_PLAY_TAP1 and is less than S_PLAY_TAP2...
    Then the game will execute the following functions underneath, which are hud.enable ones.
    else: Otherwise...
    The game will execute the other commands underneath, which are the hud.disable ones.
    end: Closes the commands: addHook, for, if

    Lua Examples

    Example of how to make the hud disappear during certain states (with full comments):
    Code:
    addHook("ThinkFrame", do // ThinkFrame initation
     for player in players.iterate // For players only
      if (player.mo.state==S_PLAY_STND // If player is standing...
      or (player.mo.state>=S_PLAY_TAP1 and player.mo.state<=S_PLAY_TAP2) // Or if the player is idling...
      or (player.mo.state>=S_PLAY_TEETER1 and player.mo.state<=S_PLAY_TEETER2) // Or if the player is teetering on an edge (first state)...
      or (player.pflags & PF_NIGHTSMODE)) // Or if we're in NiGHTS / Special Stage mode (for stability), the hud will...
       hud.enable ("rings") // Have rings enabled.
       hud.enable ("time") // Have time enabled.
       hud.enable ("score") // Have score enabled.
       hud.enable ("lives") // Have lives enabled.
      else // Otherwise...
       hud.disable ("rings") // Have score disabled.
       hud.disable ("time") // Have score disabled.
       hud.disable ("score") // Have score disabled.
       hud.disable ("lives") // Have lives disabled.
      end // Closes if.
     end // Closes for.
    end) // Closes addHook.
    Example of how to make the hud toggleable with custom buttons:
    Code:
    addHook("ThinkFrame", do
     for player in players.iterate
      if (player.cmd.buttons & BT_CUSTOM1) // If Custom Button 1 is pressed, execute the following.
       hud.enable ("rings")
       hud.enable ("time")
       hud.enable ("score")
       hud.enable ("lives")
      end
     end
    end)
    
    addHook("ThinkFrame", do
     for player in players.iterate
      if (player.cmd.buttons & BT_CUSTOM2) // If Custom Button 2 is pressed, execute the following.
       hud.disable ("rings")
       hud.disable ("time")
       hud.disable ("score")
       hud.disable ("lives")
      end
     end
    end)
    More to come.
    Quote Originally Posted by SRB2
    <Mystyc Cheez> I ****ING QUIT
    Mystyc Cheez left the game
    <~Sky> Am I too awesome or something?
    <X\Wind> Why is everyone on red...
    <X\Wind> Is Sky that awesome?
    Quote Originally Posted by Steam
    20:48 - Sky: one does not simply ban him
    20:49 - Wind: we must gather the chaos emeralds and blast him

  2. #2
    Join Date
    March 31st, 2013
    Location
    In a closet.
    Posts
    206
    Level
    30
    This may help me. Thanks, Although I still don't get what you supposed to do. (Besides scripting/coding). Lol
    SRB2 Name: Professor-Hedgehog
    SRB2 Server Name: The Professor's Lab
    SRB2 Projects:
    S1RB (Sonic 1 Mod - Reboot)

  3. #3
    Join Date
    April 5th, 2014
    Location
    west easternland
    Posts
    33
    Level
    12
    Oh yes. This guide is much more useful than any other guide I've read thus far.
    <Rob> i'll fangle your dangle

  4. #4
    Join Date
    April 19th, 2014
    Location
    Poland
    Posts
    211
    Level
    36
    Great guide you should make more lua guide
    Amateur of wading,drawing and Match

  5. #5
    Join Date
    March 25th, 2010
    Posts
    1,442
    Level
    58
    RECOMMENDATION! Get this slade.pk3 file and replace your current one if you have SLADE so you can select the Lua language for references on what to fill in! http://www.mediafire.com/download/vd...l8u1/slade.pk3

    How to select the Lua language.


    EDIT: The newest version of SLADE now has the SRB2 Lua language and selects it by default. Ignore the above.
    Quote Originally Posted by SRB2
    <Mystyc Cheez> I ****ING QUIT
    Mystyc Cheez left the game
    <~Sky> Am I too awesome or something?
    <X\Wind> Why is everyone on red...
    <X\Wind> Is Sky that awesome?
    Quote Originally Posted by Steam
    20:48 - Sky: one does not simply ban him
    20:49 - Wind: we must gather the chaos emeralds and blast him

  6. #6
    Join Date
    December 30th, 2012
    Posts
    1,426
    Level
    34
    I might start learning Lua. It looks like a fun thing to mess around with at the very least, and a good project at most.

  7. #7
    Join Date
    April 5th, 2014
    Location
    west easternland
    Posts
    33
    Level
    12
    Quote Originally Posted by Sky The Destroyer View Post
    RECOMMENDATION! Get this slade.pk3 file and replace your current one if you have SLADE so you can select the Lua language for references on what to fill in!
    Oh, you can add your own text languages to SLADE? That's neat, I might make one for SOC.
    <Rob> i'll fangle your dangle

  8. #8
    Join Date
    December 22nd, 2010
    Posts
    1,192
    Level
    26
    Should help me with making Eggman more lively whenever I get some ideas.

    Husbando to a special kitty~♥

  9. #9
    Join Date
    February 14th, 2015
    Posts
    104
    Level
    18
    Great guide for lua's basic it help a lot thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •