So, I'm trying to create like a basic radio system. Two entities, one a broadcaster, one a radio. The broadcaster:
[lua]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')
local x = 0
function ENT:Initialize()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS)
self.Entity:SetSolid( SOLID_VPHYSICS)
self:SetModel( "models/props_lab/citizenradio.mdl" )
local phys = self.Entity:GetPhysicsObject()
if(phys:IsValid()) then
phys:Wake()
end
end
function ENT:Use( activator, caller )
v = ents.FindByName("radio")
v:PlaySound()
end
[/lua]
Essentially just searches for the radio item and trys to play a sound.
The radio just recieves that.
[lua]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')
local x = 0
function ENT:Initialize()
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS)
self.Entity:SetSolid( SOLID_VPHYSICS)
self:SetModel( "models/props_lab/citizenradio.mdl" )
local phys = self.Entity:GetPhysicsObject()
if(phys:IsValid()) then
phys:Wake()
end
end
function ENT:PlaySound()
self.Entity:EmitSound("lavieenrose.mp3", 65, 100)
end
function ENT:Use( activator, caller )
if x == 1 then
umsg.Start("RadioWindowOpen", activator)
umsg.End()
x = 0
end
timer.Create("timer1", 2, 1, function() x = 1 end)
end
function ENT:OnRemove()
self:StopSound("lavieenrose.mp3")
end
[/lua]
Just recieves it.
And it returns this error:
[quote][gamemodes\tiramisu\entities\entities\broadcaster\init.lua:19] attempt to call method 'PlaySoundz' (a nil value)
[/quote]
Here's my cl_init for the radio SENT. For some reason on spawn it pops up the derma window, but it doesn't put any errors in console.
[lua]
include('shared.lua')
function ENT:Draw()
self:DrawEntityOutline(0.0)
self.Entity:DrawModel()
end
local function DrawRadioControl()
local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself
DermaPanel:SetPos( 50,50 ) -- Position on the players screen
DermaPanel:SetSize( 1000, 900 ) -- Size of the frame
DermaPanel:SetTitle( "Radio" ) -- Title of the frame
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true ) -- Draggable by mouse?
DermaPanel:ShowCloseButton( true ) -- Show the close button?
DermaPanel:MakePopup() -- Show the frame
local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel"
DermaButton:SetText( "Play Song" )
DermaButton:SetPos( 25, 50 )
DermaButton:SetSize( 150, 50 )
DermaButton.DoClick = function() print("Fucktheworld")end
local DermaButton1 = vgui.Create( "DButton" )
DermaButton1:SetParent( DermaPanel ) -- Set parent to our "DermaPanel"
DermaButton1:SetText( "Add to inventory." )
DermaButton1:SetPos( 25, 50 )
DermaButton1:SetSize( 150, 50 )
DermaButton1.DoClick = function() print("Fucktheworld")end
end
usermessage.Hook( "RadioWindowOpen", DrawRadioControl() );
[/lua]
instead of v:PlaySound()
Just do v:EmitSound
[QUOTE=Llamalord;30889254]instead of v:PlaySound()
Just do v:EmitSound[/QUOTE]
I already tried that, and it returns
[quote]
[gamemodes\tiramisu\entities\entities\broadcaster\init.lua:19] attempt to call method 'EmitSound' (a nil value)
[/quote]
[QUOTE=Llamalord;30889254]instead of v:PlaySound()
Just do v:EmitSound[/QUOTE]
You guys are so oblivious, stop helping people if you don't even know what you're talking about!
ents.FindByName, and most of the functions from the ents library don't return an entity, but a list of entities. That makes sense since several entities can have the same name, and there can be several entities of the same class.
Actually I believe FindByName isn't what you need there, it is used for finding entities which have been given a name via SetName, or map entities which are often named. Entities spawned by the player never have a name, unless a script overrides that behaviour, so you will rarely need FindByName.
Use FindByClass instead. ents.FindByClass("radio") will give you a list of all the "radio" entities present on the map.
And finally, if you don't know about it, the "for k,v in pairs(t) do ... end" loop is quite handy for iterating through lists. That loop will execute for every element in the list t, where k is the index of the current element and v is the element itself. The only problem is that the order is completely arbitrary, you can't predict in which order the elements will be processed. Most of the times, it doesn't matter at all, so you don't have to worry about it.
Just in case you didn't understand or can't fix it by yourself, here's your fixed ENT:Use function.
[lua]
function ENT:Use( activator, caller )
for k,v in pairs(ents.FindByName("radio")) do
v:PlaySound()
end
end
[/lua]
[editline]5th July 2011[/editline]
Also self.Entity is deprecated in scripted entities, and using it is a bad habit because new people who look at your code will have a hard time guessing when they should put self.Entity and when they should put just self.
self and self.Entity are the same, so just replace everything with self and it will make your code more readable.
Sorry, you need to Log In to post a reply to this thread.