• Using the lua_run entity in my map
    18 replies, posted
Basically what I'm trying to accomplish is that when I join a TTT server playing my map, I want my steam ID to be detected and a sound (packed into the map) to be played for everyone to hear when the map spawns. It looks as if a single line of code is needed in the lua_run entity and it has a 512 character limit. I would trigger the code by using a logic_auto to run the code when the map spawns. How would one do this using lua_run? I apologize that most of my experience is with [URL="http://steamcommunity.com/sharedfiles/filedetails/?id=133138700"]mapping[/URL] and not lua, so please forgive me for my lack of lua knowledge. I'm learning, and I've only created the most basic lua scripts so far.
Alternatively, I would like to press a button and have it play a sound file for everyone, although only usable by me through checking the Steam ID. How could this be done with a lua_run entity?
Tell the logic_auto to RunCode on the lua_run entity. hook.Add('PlayerInitialSpawn','a',function(p) if p:SteamID()=='STEAM_0:1:12345' then p:EmitSound('hi.wav', 0, 100) end)
[QUOTE=JetBoom;42739312]Tell the logic_auto to RunCode on the lua_run entity. hook.Add('PlayerInitialSpawn','a',function(p) if p:SteamID()=='STEAM_0:1:12345' then p:EmitSound('hi.wav', 0, 100) end)[/QUOTE] For some reason that didn't work... I got the error: [CODE] [ERROR] RunString:1: unexpected symbol near ')' 1. unknown - RunString:0 [/CODE]
[lua]hook.Add('PlayerInitialSpawn','a',function(p) if p:SteamID()=='STEAM_0:1:12345' then p:EmitSound('hi.wav', 0, 100) end end)[/lua]
[QUOTE=Banana Lord.;42739498][lua]hook.Add('PlayerInitialSpawn','a',function(p) if p:SteamID()=='STEAM_0:1:12345' then p:EmitSound('hi.wav', 0, 100) end end)[/lua][/QUOTE] Great, that seems to work! :smile: Thanks guys. The sound will only play the first time the player joins the server and the map resets, even if the map has been played for a few rounds, correct? If I wanted the sound to play every time, would I simply change 'PlayerInitialSpawn' to 'PlayerSpawn'? Now for the other method... How would I go about having a sound played [U]at the press of a button[/U], only if the steam ID checks out? I managed to get this working as a concept: [CODE]if ACTIVATOR:SteamID() == 'STEAM_0:1:30351687' then print('Hello World') end[/CODE] but this only prints "Hello World" to the console and I don't know if this is even the correct way to do things for what I want to accomplish.
[QUOTE=Matt2468rv;42739732]Great, that seems to work! :smile: Thanks guys. The sound will only play the first time the player joins the server and the map resets, even if the map has been played for a few rounds, correct? If I wanted the sound to play every time, would I simply change 'PlayerInitialSpawn' to 'PlayerSpawn'? Now for the other method... How would I go about having a sound played [U]at the press of a button[/U], only if the steam ID checks out? I managed to get this working as a concept: [CODE]if ACTIVATOR:SteamID() == 'STEAM_0:1:30351687' then print('Hello World') end[/CODE] but this only prints "Hello World" to the console and I don't know if this is even the correct way to do things for what I want to accomplish.[/QUOTE] When you use the lua_run entity, there are 3 globals setup to aid you. ACTIVATOR CALLER TRIGGER_PLAYER ACTIVATOR and CALLER are straight forward. TRIGGER_PLAYER is only set if ACTIVATOR is a player entity. Also, a tip: don't use double quotes at all or else you will make the map unparseable by hammer.
[QUOTE=vexx21322;42741430]When you use the lua_run entity, there are 3 globals setup to aid you. ACTIVATOR CALLER TRIGGER_PLAYER ACTIVATOR and CALLER are straight forward. TRIGGER_PLAYER is only set if ACTIVATOR is a player entity. Also, a tip: don't use double quotes at all or else you will make the map unparseable by hammer.[/QUOTE] Right, and I'm assuming that's why my above test code works using ACTIVATOR. I got this to work at the push of a button, triggering the code to be run: [CODE]if ACTIVATOR:SteamID() == 'STEAM_0:1:12345678' then ACTIVATOR:EmitSound('matt2468rv/sound.wav', 0, 100) end[/CODE] Is this the correct way to do things? The sound emits from the ACTIVATOR, which I assume is just fine given that the distance is set to 0, which I'm assuming tells it to play everywhere?
[QUOTE=Matt2468rv;42739732]Great, that seems to work! :smile: Thanks guys. The sound will only play the first time the player joins the server and the map resets, even if the map has been played for a few rounds, correct? If I wanted the sound to play every time, would I simply change 'PlayerInitialSpawn' to 'PlayerSpawn'?[/QUOTE] PlayerInitialSpawn is called when you connect to the server. If you disconnect and reconnect it will be played called again. PlayerSpawn is called every time you respawn. Also, just a tip: If you're going for compression you can use keywords straight after symbols: [lua]hook.Add('PlayerInitialSpawn','a',function(p)if p:SteamID()=='STEAM_0:1:12345'then p:EmitSound('hi.wav',0,100)end end)[/lua]
[QUOTE=Lexic;42742173]PlayerInitialSpawn is called when you connect to the server. If you disconnect and reconnect it will be played called again. PlayerSpawn is called every time you respawn. Also, just a tip: If you're going for compression you can use keywords straight after symbols: [lua]hook.Add('PlayerInitialSpawn','a',function(p)if p:SteamID()=='STEAM_0:1:12345'then p:EmitSound('hi.wav',0,100)end end)[/lua][/QUOTE] Gotcha! So if I wanted to do something like: PrintMessage( HUD_PRINTTALK, "Hi There!" ) instead of playing a sound, how would I accomplish that? I tried this: [CODE]hook.Add('PlayerSpawn','a',function(p) if p:SteamID()=='STEAM_0:1:12345678' then PrintMessage( HUD_PRINTTALK, "Hi There!" ) end end)[/CODE] but that did not work. Granted I have little experience with lua and I'm still trying to figure things out. I appreciate everyone's help greatly.
[QUOTE=Handsome Matt;42742501]p:PrintMessage( HUD_PRINTTALK, "hi" )[/QUOTE] That also does not work. :/
I know, I didn't.
[QUOTE=Handsome Matt;42742987]you're changing STEAM_0:1:12345678 to your own steam id... right?[/QUOTE] Definitely. And the other code is working with it. This is what I have right now: [CODE]hook.Add('PlayerSpawn','a',function(p) if p:SteamID()=='STEAM_0:1:12345678' then p:PrintMessage( HUD_PRINTTALK, 'Hi There!' ) end end)[/CODE] It does not work with: p:PrintMessage( HUD_PRINTTALK, 'Hi There!' ) PrintMessage( HUD_PRINTTALK, 'Hi There!' ) [B][U]Edit:[/U][/B] Figured out what I was doing wrong. I had two lua_run entities being triggered at the map spawn. One with the above code and one with the code earlier in the thread that worked. Since both of the hooks were named 'a', only one of them worked. When I changed the 'a' to 'b' in the above code, it worked successfully! Awesome. It also seems to work whether I use p: or not.
[QUOTE=Matt2468rv;42743021]It also seems to work whether I use p: or not.[/QUOTE] using p:PrintMessage will only send it to player p, using PrintMessage on it's own will print to everyone
Oh I see. So does that correspond to emitting a sound as well, like the first code that worked? Or does the sound simply emit from player p to everyone and can be heard everywhere? Can I also use a lua_run entity to restrict a button's output to a specific steam ID? Say I have a button that turns on a light in the map, but I only want it to work for someone with a specific steam ID. Can lua be used to interact with the map's entity I/O?
Sorry for double posting, but I've been searching around and trying out code for hours now and I'm stumped about how to get an input sent to a map entity through lua. This code seems to work fine when a button is pressed to run it: [CODE]if ACTIVATOR:SteamID() == 'STEAM_0:1:12345678' then print('Hello World') end[/CODE] But instead of having 'Hello World' printed to the console, I want to have an input sent to one of the map's named entities. For example, I have a logic_relay entity in my map named "relay_1" and I want the input "Trigger" to be sent to the logic_relay via the lua script. How would one accomplish this? I've been trying to get [URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index43b2.html"]Entity.Fire[/URL] to work, but have not had success. [B][U] Edit:[/U][/B] Marking this thread as solved, since my initial questions were answered. Thanks for the help with that guys. I believe a new thread regarding entity input would be more appropriate.
Sorry, you need to Log In to post a reply to this thread.