Garry's Mod

Garry's Mod

Not enough ratings
How to use lua_run in hammer.
By snipe 2.0
This guide will cover the basic and advanced ways lua_run can be used.

Spanish version by "HM EXTREAME GAMER"
https://gtm.steamproxy.vip/sharedfiles/filedetails/?id=2986310468
   
Award
Favorite
Favorited
Unfavorite
Getting Started
Alright if you're searching this then I will assume you know how to open your game files and use hammer, you will also need some text editor like Notepad++[notepad-plus-plus.org] but I will recommend Visual Studio[visualstudio.microsoft.com] with the Garry's mod Lua[marketplace.visualstudio.com] plugin.

To start in hammer spawn a lua_run entity.




If you want lua_run to run when starting the map give it a name and use a logic_auto and send a command to lua_run to run on map spawn.
Running console commands
If you have been mapping you will know that point_servercommand doesn't work in Garry's mod and logic_script isn't a thing, but using lua_run you can get around this.

The lua_run entity can be treated like the console where you can run commands, while I have not looked into it all that much commands that affect players don't seem to work.

In the lua_run entity in the field box "code" put RunConsoleCommand('command', number)[wiki.facepunch.com] where "command" is string[en.wikipedia.org] of the command you want and "number" is a float[en.wikipedia.org] of the number you want
Running scripts
Now running commands are great and all but as you probably noticed making scripts in that tinny box is hard. So i'm going to show you how to run script files next.

To start in the run_lua entity in code put include()[wiki.facepunch.com]


Now the difference with this command is you need to setup a file structure so it knows where to find the script file.

1) Open you're Garry's mod game files.
2) Go to (Game location)\GarrysMod\garrysmod\addons
3) Make a new folder and call it "mapcontent"


4) In "mapcontent" make a file called "lua"


5) In lua make a text file and rename it "MyMapScript.lua" and REMEMBER. It always needs to have ".lua" at the end of the file name for the game to be able to use it.


Now copy the file name then go back into hammer and add the file name into the
include('MyMapScript.lua')



Side Note: If you're experienced with programming addons then you will do fine as map scripts are very similar.
Learning resources and examples
Programming map scripts can be difficult to get started as there is very little information about lua_run, but lots of youtube videos and other resources exist.

The Garry's mod wiki is a good resource as it provides information about commands you can use.
Garry's mod wiki[wiki.facepunch.com]

Video tutorials also exist.
Omni Games Garry's mod: Lua Tutorial
<CODE BLUE> Garry's mod Lua


Side Note - 1: I have not tried running map scripts on a server but I will assume that the file structure is the same or would be in the lua folder somewhere.

Side Note - 2: It should be possible to use lua files from addons but in my limited testing I have not gotten it working.

Basic example of in code comments, variables, printing to console, and chat.
-- Vars local NumA = 42 local NumB = 7 -- Main print('Hello World') print(NumA - NumB) PrintMessage(HUD_PRINTTALK, 'Hello Chat')

Example of multi-line comments, for loops, finding entity's by class, and name
--[[ -- Vars NumA = 42 NumB = 7 -- Main print('Hello Console') print(NumA - NumB) PrintMessage(HUD_PRINTTALK, 'Hello Chat') --]] for k, v in ipairs( ents.FindByClass('Player') ) do PrintMessage(HUD_PRINTTALK, v:Name()) end
The class 'Player' can be found on the Garry's mod wiki under Classes

1 Comments
Plain Bagel 31 Dec, 2024 @ 2:13am 
super useful