• Lua triggers
    5 replies, posted
I'm trying to make an entity that I can control with lua. I have reached a road block though because I've never done this before :( My concept is to have one entity that I can spawn under different names (variables) and then modify them based on my map and players. What I am trying to achieve is to have an entity where a player walks into it and music plays, only to stop playing when the player leaves. I'm trying to incorporate my entity with [url]http://wiki.garrysmod.com/page/ENTITY/StartTouch[/url], [url]http://wiki.garrysmod.com/page/ENTITY/EndTouch[/url], [url]http://wiki.garrysmod.com/page/Category:IGModAudioChannel[/url] and various other entity specific functions to set it's location in the world. My code as follows so far. [B]The Entity[/B] [code] AddCSLuaFile() -- Why? Because nothing was explained, tutorials just said do it... ENT.Type = 'anim' -- I still dont understand what these do :/ ENT.Base = 'base_gmodentity' -- Had to move base_gmodentity.lua from sandbox for compatibility. ENT.PrintName = 'Zoning Music Base' ENT.Author = 'Renamon Toast Crunch' ENT.Purpose = 'Base entity for dynamic zoned music' ENT.Instructions = 'Spawn and modify with lua' ENT.Spawnable = false -- Nope ENT.AdminSpawnable = false -- We want to spawn aand modify within lua only and not be controlled outside of the files. function ENT:Initialize() self:SetSolid( 0 ) -- Not Solid self:SetCollisionBoundsWS(Vector(0,0,0),Vector(0,0,0)) -- This is in the void. self:SetTrigger(true) -- So self is editable with lua. end function ENT:StartTouch() end function ENT:StopTouch() end [/code] [B]Serverside File[/B] [code] -- This is a serverside file! /* 3D H/W Reference ( Vector( -1122.997925, 2017.903564, -2951.961670 ), Vector( -864.203491, 1775.536865, -3198.830566 )) -- Zone: Secret Room ( Vector( -804.779541, 698.597168, -2244.161865 ), Vector( -1250.074219, 1865.446411, -2555.334717 )) -- Zone: Spawn Room ( Vector( -2319.035645, 4598.020020, -5.790947 ), Vector( -5994.073242, -485.654572, -3550.934326 )) -- Zone: Tutorial Island */ timer.Simple( 3, function() --Create the map entitys 3 seconds after gamemode loads to prevent errors. MsgC( Color( 255, 0, 255 ), "Varsgroth: ",Color( 0, 255, 0 ),"Zoning Check!\n") local zcc = ents.Create('dyn_music') zcc:SetPos(Vector( 413.939758, 1272.408325, -1966.252075 )) -- Vector position in the center of the CCreation room. zcc:SetCollisionBoundsWS(Vector( -111.968750, 1647.968750, -1967.854248 ), Vector( 621.744141, 912.075867, -1562.446289 )) -- 3D W/H of ZCC. zcc:Spawn() zcc:EmitSound( 'HL1/fvox/activated.wav',100,100 ) -- Emitting sound just to make sure it's actually where it's supposed to be. print('Success!') end) [/code] The above code works... kinda but what I cant figure out is how to do StartTouch and EndTouch from the serverside file. I have only begun testing via one spawned entity (in this case named zcc) but will end up spawning 3 others under the names zsr, zti, and zspr.
You need to move StartTouch and EndTouch in to your init.lua, they are serverside, and you can then send a net message on StartTouch to the client telling them to play the music, then store the music channel in a variable clientside, then when EndTouch is called send a net message telling the music to stop, then call stop on the channel variable you made earlier.
use a brush ent, not an anim ent
[QUOTE=PortalGod;45961106]use a brush ent, not an anim ent[/QUOTE] Thanks, I was wondering what to put for ENT.Type, I can't seem to find anything about the types though like a list of the types and what they are each for :/ [editline]12th September 2014[/editline] [QUOTE=meharryp;45961101]You need to move StartTouch and EndTouch in to your init.lua, they are serverside, and you can then send a net message on StartTouch to the client telling them to play the music, then store the music channel in a variable clientside, then when EndTouch is called send a net message telling the music to stop, then call stop on the channel variable you made earlier.[/QUOTE] Thanks for the help, I'll try that soon.
[QUOTE=meharryp;45961101]You need to move StartTouch and EndTouch in to your init.lua, they are serverside, and you can then send a net message on StartTouch to the client telling them to play the music, then store the music channel in a variable clientside, then when EndTouch is called send a net message telling the music to stop, then call stop on the channel variable you made earlier.[/QUOTE] [code] timer.Simple( 3, function() --Create the map entitys 3 seconds after gamemode loads to prevent errors. MsgC( Color( 255, 0, 255 ), "Varsgroth: ",Color( 0, 255, 0 ),"Zoning Check!\n") zcc = ents.Create('dyn_music') if ( !IsValid( dyn_music ) ) then return end zcc:SetPos(Vector( 256,1280,-1824 )) -- Vector position in the center of the CCreation room. zcc:SetCollisionBoundsWS(Vector( -128,1664,-2144 ), Vector( 640,896,-1536 )) -- 3D W/H of ZCC. zcc:Spawn() zcc:EmitSound( 'HL1/fvox/boop.wav',100,100 ) -- Emitting sound just to make sure it's actually where it's supposed to be. print('Success!') function zcc:StartTouch(hitent) if ( hitEnt:IsValid() and hitEnt:IsPlayer() ) then zcc:EmitSound( 'HL1/fvox/activated.wav',100,100 ) end end function zcc:StopTouch(hitent) if ( hitEnt:IsValid() and hitEnt:IsPlayer() ) then zcc:EmitSound( 'HL1/fvox/deactivated.wav',100,100 ) end end end) [/code] My SetPos function doesn't seem to work, the entity is just spawned to 0,0,0 :(
1. Use this to spawn them: (reference: [url]http://facepunch.com/showthread.php?t=1408703[/url] ) [LUA] hook.Add("InitPostEntity", "Spawn <entity name> after everything initialized", function() -- Don't forget to add the entity's name! local Spawns = { { pos = Vector( 256, 1280, -1824 ), ang = Angle( 0, 0, 0 ) } -- I think you know how to add more items :P } for _, v in pairs(Spawns) do local the_ent = ents.Create( 'dyn_music' ) the_ent:SetPos(v.pos) the_ent:SetAngles(v.ang) the_ent:SetModel( "<model>" ) -- Don't forget to change this! the_ent:Spawn() the_ent = nil end end) [/LUA] 2. For the StartTouch/EndTouch hooks, you'd need to indentify whether the sound had stopped playing so you could re-start playing it(if you need some reference, you can use the WireMod's Sound Player entity) 3. So the StartTouch/EndTouch hooks work, you have to use them on the Entity's files, and define them as an Entiy's function, like:[LUA]ENT:StartTouch()[/LUA] or [LUA]ENT:EndTouch()[/LUA]
Sorry, you need to Log In to post a reply to this thread.