Spawn entity

Hi!

I’m trying to create a simple Derma panel, which can spawn any props that I’ve entered ( In the code ofc. ).

However, I know that Derma is client-sided, and spawning an entity is server-sided, so as I am new to this, "How would I get around linking the client-side derma up to a server file so I can spawn an entity using


DermaButton.DoClick = function ()

Yeah, I can take the blame for not knowing this, but I’ve searched for this, and I didn’t really find what I was looking for.

Thanks!

( Oh yeah, I do not wish making this as an addon! )

Use the net library to request the spawn from the server.
[lua]util.AddNetwowrkString( “spawn_entity” )
net.Receive( “spawn_entity”, function( len, ply )
local trace = ply:GetEyeTrace() // grab the player’s eyetrace so you can set a spawn point
local ent = ents.Create( net.ReadString() ) // create the entity from the player’s choice
ent:SetPos( trace.HitPos ) // set the position of the prop as the location that the player is looking at
ent:SetOwner( ply ) // set the owner
ent:Spawn() // finally spawn it
end )
[/lua]
This is how I’d handle the server-side. So you’d have to send the class of the entity in the clientside:
[lua]DermaButton.DoClick = function()
net.Start( “spawn_entity” )
net.WriteString( entityclass ) // send the entity class here from however you’re setting it
net.SendToServer()
end
[/lua]

Thanks!

  • But where would those files be located?
    And what do I need to modify to set the model of the spawning entity?

And I really appreciate your work to help me, it’s awesome.

I would recommend making a single file and just using the basic
[lua]if SERVER then
//serverside stuff goes here
else
//clientside stuff goes here
end
[/lua]
So you could just place a single file in GarrysMod/garrysmod/lua/autorun.
As to your second question it depends, what have you made so far for your menu?

Awesome.

What I’ve made is simply a nice looking DFrame, with a single button, where I want to make it spawn an entity.

Lua king IKR.:zoid:

Edited:
Hmm, I’m having this error:[LUA][ERROR] lua/autorun/spawnmenu.lua:5: attempt to call field ‘AddNetwowrkString’ (a nil value)

  1. unknown - lua/autorun/spawnmenu.lua:5

[/LUA]
[LUA]/////////////////
if SERVER then //
/////////////////

util.AddNetwowrkString( “spawn_entity” )
net.Receive( “spawn_entity”, function( len, ply )
local trace = ply:GetEyeTrace() // grab the player’s eyetrace so you can set a spawn point
local ent = ents.Create( net.ReadString() ) // create the entity from the player’s choice
ent:SetPos( trace.HitPos ) // set the position of the prop as the location that the player is looking at
ent:SetOwner( ply ) // set the owner
ent:Spawn() // finally spawn it
end )

///////
else //
///////

function showcrappymenu()

local crappymenu = vgui.Create( “DFrame” ) – Creates the frame itself
crappymenu:SetPos( ScrW() / 2 - 1000 / 2, ScrH() / 2 - 520 / 2 ) – Position on the players screen
crappymenu:SetSize( 1000, 520 ) – Size of the frame
crappymenu:SetTitle( “Spawn Menu” ) – Title of the frame
crappymenu:SetVisible( true )
crappymenu:SetDraggable( true ) – Draggable by mouse?
crappymenu:ShowCloseButton( true ) – Show the close button?
crappymenu:MakePopup() – Show the frame
crappymenu.Paint = function()
draw.RoundedBox(4,0,0, crappymenu:GetWide(), crappymenu:GetTall()-5, Color(50,50,50,255)) – (1,2,3) 1 = How rounded it should be 2 = Bredden 3 = Højden ### Main box
draw.RoundedBox(4,0,0, crappymenu:GetWide(), 25, Color(164,0,0,255)) – (1,2,3) 1 = How rounded it should be 2 = Bredden 3 = Højden ### Header grey

end

local DermaButton = vgui.Create( “DButton” )
DermaButton:SetParent( crappymenu ) – Set parent to our “DermaPanel”
DermaButton:SetText( “Spawn an entity” )
DermaButton:SetPos( 25, 50 )
DermaButton:SetSize( 150, 50 )
DermaButton.DoClick = function()
net.Start( “spawn_entity” )
net.WriteString( entityclass ) // send the entity class here from however you’re setting it
net.SendToServer()
end
end

end

concommand.Add( “Spawnmenu”, showcrappymenu )[/LUA]

I think you should look at this thread: http://forum.facepunch.com/showthread.php?t=1226172
It’ll show you how to use DIconLayout. I think it would be useful in your situation.
I would do something along the lines of
[lua]spawnable_entities = { [“class”] = “model”, [“class2”] = “model2” } // the list of spawnable entities and their models

local frame = vgui.Create(“DFrame”)
frame:SetSize(256, 128)
frame:MakePopup()

list = frame:Add(“DIconLayout”)
list:Dock(FILL)
list:DockMargin(3, 3, 3, 3)
list:SetSpaceX(3)
list:SetSpaceY(3)

for k, v in pairs( spawnable_entities ) do // I think it’ll return both the class and the model because it returns the key
local icon = list:Add(“DSpawnIcon”)
icon:SetModel( v )
icon.DoClick = function()
net.Start( “spawn_entity” )
net.WriteString( k )
net.SendToServer()
end
end
[/lua]

[editline]29th January 2014[/editline]

You spelt “Network” wrongly.

Worked, but how would I set the model to spawn?:smile:

Each I icon should spawn it’s own prop when clicked I think, if DoClick is supported.

The code you sent me, made me really confused. This is how it looks currently:
[LUA]/////////////////
if SERVER then //
/////////////////

util.AddNetworkString( “spawn_entity” )
net.Receive( “spawn_entity”, function( len, ply )
local trace = ply:GetEyeTrace() // grab the player’s eyetrace so you can set a spawn point
local ent = ents.Create( net.ReadString() ) // create the entity from the player’s choice
ent:SetPos( trace.HitPos ) // set the position of the prop as the location that the player is looking at
ent:SetOwner( ply ) // set the owner
ent:Spawn() // finally spawn it
end )

///////
else //
///////
spawnable_entities = { [“class”] = “model”, [“class2”] = “model2” } // the list of spawnable entities and their models

function showcrappymenu()

local frame = vgui.Create(“DFrame”)
frame:SetSize(256, 128)
frame:MakePopup()

list = frame:Add(“DIconLayout”)
list:Dock(FILL)
list:DockMargin(3, 3, 3, 3)
list:SetSpaceX(3)
list:SetSpaceY(3)

for k, v in pairs( spawnable_entities ) do // I think it’ll return both the class and the model because it returns the key
local icon = list:Add(“DSpawnIcon”)
icon:SetModel( v )
icon.DoClick = function()
net.Start( “spawn_entity” )
net.WriteString( k )
net.SendToServer()
end
end
end

concommand.Add( “Spawnmenu”, showcrappymenu )[/LUA]
[LUA][ERROR] lua/autorun/spawnmenu.lua:9: Tried to use a NULL entity!

  1. SetPos - [C]:-1
  2. func - lua/autorun/spawnmenu.lua:9
    3. unknown - lua/includes/modules/net.lua:31
    [/LUA]:words:

///////
spawnable_entities = { [“class”] = “model”, [“class2”] = “model2” } // the list of spawnable entities and their models

Edit tihs.

You have to add your own models and classes in the table spawnable_entities, I put class = model as a placeholder (make sure you keep the brackets around the class and surround all the paths in speech marks).

[LUA]spawnable_entities = { [“prop_physics”] = “models/props_c17/oildrum001.mdl”, [“prop_physics”] = “models/props_c17/oildrum001.mdl” } [/LUA] I’m not quiet sure if I made the class right though.
And while I’m on track, it gives me this error[LUA]
[ERROR] lua/autorun/spawnmenu.lua:34: attempt to index local ‘icon’ (a nil value)

  1. unknown - lua/autorun/spawnmenu.lua:34
  2. unknown - lua/includes/modules/concommand.lua:69
    [/lua]
    ( I know it starts to get frustrating for you, but thanks.

Sorry, it is list:Add( “SpawnIcon” ) not “DSpawnIcon”

Worked, now showing icon.
However, it’s not spawning it, nor is it outputting an error.

Edited: Outputting this actually:[LUA]prop at 560 -40 -150 missing modelname
p[/LUA]

It is, prop_physics requires a model, change the DoClick to tell the server what the model you want is instead, and create a prop_physics and set it’s model.
[lua]
/////////////////
if SERVER then //
/////////////////

util.AddNetworkString( “spawn_entity” )
net.Receive( “spawn_entity”, function( len, ply )
local trace = ply:GetEyeTrace() // grab the player’s eyetrace so you can set a spawn point
local ent = ents.Create( “prop_physics” ) // create the prop_physics
ent:SetPos( trace.HitPos ) // set the position of the prop as the location that the player is looking at
ent:SetOwner( ply ) // set the owner
ent:SetModel( net.ReadString() ) // set the model
ent:Spawn() // finally spawn it
end )

///////
else //
///////
spawnable_entities = { “models/props_c17/oildrum001.mdl”, “models/props_c17/oildrum001.mdl” } // you don’t need the class if you’re just setting prop_physics.

function showcrappymenu()

local frame = vgui.Create(“DFrame”)
frame:SetSize(256, 128)
frame:MakePopup()

list = frame:Add(“DIconLayout”)
list:Dock(FILL)
list:DockMargin(3, 3, 3, 3)
list:SetSpaceX(3)
list:SetSpaceY(3)

for k, v in pairs( spawnable_entities ) do // I think it’ll return both the class and the model because it returns the key
local icon = list:Add(“SpawnIcon”)
icon:SetModel( v ) // the the model for the icon.
icon.DoClick = function()
net.Start( “spawn_entity” )
net.WriteString( v ) // write the model not the class
net.SendToServer()
end
end
end
concommand.Add( “Spawnmenu”, showcrappymenu )
[/lua]
Also you might wanna make a scrollbar as shown in the DIconLayout tutorial

The code above is outputting this error:[LUA][ERROR] lua/autorun/spawnmenu.lua:35: attempt to index local ‘icon’ (a nil value)

  1. unknown - lua/autorun/spawnmenu.lua:35
  2. unknown - lua/includes/modules/concommand.lua:69
    [/LUA]

Edited: And yes, I did change list:Add(“DSpawnIcon”) to list:Add( “SpawnIcon” ) to

Oh jeez I forgot the SpawnIcon again. Edited.

Fixed it, a missing ‘end’ was the cause, thanks a BUNCH for your help!