Lua run: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(update template, update name to conform with other entity names)
No edit summary
 
(29 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{lang|{{Code|selected=1|lua_run}}}}
{{LanguageBar}}
{{entity|lua_run|type=e0|game=Garry's Mod|sprite=1}}
{{this is a|logical entity|name=lua_run|notext=1|game=Garry's Mod|sprite=1}} It runs raw [http://en.wikipedia.org/wiki/Lua_%28programming_language%29 Lua] code given to it in its keyvalues.
It runs raw [http://en.wikipedia.org/wiki/Lua_%28programming_language%29 Lua] code given to it in its keyvalues.
{{clr}}
{{clr}}
== Keyvalues ==
== Keyvalues ==
{{KV|Code|intn=Code|string|Lua code to run when triggered.}}
{{ModernBugfix|In Hammer, using string arguments will corrupt the [[VMF]] file's structure, making the file unviewable for the next Hammer session.|Remove the string argument manually with a text editor.}}
{{KV Targetname}}
{{KV Targetname}}
{{KV|Code|intn=Code|string|Lua code to run when triggered.</br>The lua variables {{Code|ACTIVATOR}} and {{Code|CALLER}} may be used in the code, as well as {{Code|TRIGGER_PLAYER}} if '''!activator''' resolves to a player.}}
:{{Hammer quote warning}}
:{{Workaround|You should instead use either an apostrophe (single quote) {{Code|'}} or brackets {{Code|[[]]}} to contain strings in your lua code.}}
:{{tip|Using {{code|include()}} you can run external lua files. The function looks for files in {{path|\GarrysMod\garrysmod\addons\mapcontent\lua}}.<br>For example you could create a file named {{code|MyMapScript.lua}} <!-- I wonder wheres this file name came from? The answer is a steam guide, I didn't figure this shit out myself. -Wisdurm --> at {{path|\GarrysMod\garrysmod\addons\mapcontent\lua\MyMapScript.lua}}, which could then be run in Hammer with {{code|include('MyMapScript.lua')}}.
}}
== Flags ==
== Flags ==
*1: Run code on spawn
{{Fl|1|Run code on spawn}}
 
== Inputs ==
== Inputs ==
{{IO|RunCode|Run Code that was defined in the entity.}}
{{I|RunCode|Run Code that was defined in the entity.}}
{{IO|RunPassedCode|param=string|Run code that was passed as a variable.}}
{{I|RunPassedCode|param=string|Run code that was passed as a variable.}}
{{I Targetname}}
 
== Outputs ==
== Example Code ==
{{O Targetname}}
'''Lua_run Chat Message'''
[[Category:English]]
*<code>RunConsoleCommand('say', 'Hello World')</code>
 
'''lua_run parents a spark to the player, the spark then starts, stops after 10 seconds and removes itself for clean up'''
*<code>for k,v in pairs(ents.FindInSphere(Entity(1):GetPos(), 50)) do if v:IsPlayer() then local spark = ents.Create('env_spark'); spark:SetKeyValue('MaxDelay', '0.1'); spark:SetKeyValue('TrailLength', '3'); spark:SetKeyValue('Magnitude', '8'); spark:SetPos(v:GetPos() + Vector(0,0,45)); spark:SetParent(v); spark:Spawn(); spark:Fire('StartSpark'); spark:Fire('Kill', '', 10); end end</code>
 
== External links ==
'''Map example .VMF and .BSP (x3 lua_run functioning) - By gtamike_TSGK'''
* http://gtamike.tsgk.com/gtamike_TSGK/test_area_lua_run_v2.zip
 
'''Advanced Map example .VMF and .BSP (x3 lua_run parenting a spark to the player) - By gtamike_TSGK'''
* http://gtamike.tsgk.com/gtamike_TSGK/lua_run_spark_on_player_v2.zip

Latest revision as of 16:33, 11 September 2025

English (en)Русский (ru)Translate (Translate)
Lua run.png

lua_run is a logical entity available in Garry's Mod Garry's Mod. It runs raw Lua code given to it in its keyvalues.

Keyvalues

Name (targetname) <string>[ Edit ]
The name that other entities refer to this entity by, via Inputs/Outputs or other keyvalues (e.g. parentname or target).
Also displayed in Hammer's 2D views and Entity Report.
See also:  Generic Keyvalues, Inputs and Outputs available to all entities

Code (Code) <string>
Lua code to run when triggered.
The lua variables ACTIVATOR and CALLER may be used in the code, as well as TRIGGER_PLAYER if !activator resolves to a player.
Warning.pngWarning:Using the quote symbol " in any text field of a Hammer Object Properties Dialog will lead to VMF corruption, fixable only by editing the VMF directly with a text editor and removing the added quote symbol.
PlacementTip.pngWorkaround:You should instead use either an apostrophe (single quote) ' or brackets [[]] to contain strings in your lua code.
Tip.pngTip:Using include() you can run external lua files. The function looks for files in 🖿\GarrysMod\garrysmod\addons\mapcontent\lua.
For example you could create a file named MyMapScript.lua at 🖿\GarrysMod\garrysmod\addons\mapcontent\lua\MyMapScript.lua, which could then be run in Hammer with include('MyMapScript.lua').

Flags

Run code on spawn : [1]

Inputs

RunCode
Run Code that was defined in the entity.
RunPassedCode <stringRedirectInput/string>
Run code that was passed as a variable.

Example Code

Lua_run Chat Message

  • RunConsoleCommand('say', 'Hello World')

lua_run parents a spark to the player, the spark then starts, stops after 10 seconds and removes itself for clean up

  • for k,v in pairs(ents.FindInSphere(Entity(1):GetPos(), 50)) do if v:IsPlayer() then local spark = ents.Create('env_spark'); spark:SetKeyValue('MaxDelay', '0.1'); spark:SetKeyValue('TrailLength', '3'); spark:SetKeyValue('Magnitude', '8'); spark:SetPos(v:GetPos() + Vector(0,0,45)); spark:SetParent(v); spark:Spawn(); spark:Fire('StartSpark'); spark:Fire('Kill', , 10); end end

External links

Map example .VMF and .BSP (x3 lua_run functioning) - By gtamike_TSGK

Advanced Map example .VMF and .BSP (x3 lua_run parenting a spark to the player) - By gtamike_TSGK