How to properly spawn a prop in sandbox?

What are the necessary motions that you need to go through in order to simulate a player spawning a prop from the spawn menu?

Here is what I have so far;

[lua]

	local model = self.Owner:GetNWString( "spawn_selection" ) -- this is set at another point in the program
	if model ~= "" then

		local prop = ents.Create( "prop_physics" )
		if not IsValid( prop ) then
			return
		end

		prop:SetModel( model )
		prop:SetPos( self.Owner:EyePos() + (self.Owner:GetAimVector() * 48) )
		prop:SetAngles( self.Owner:GetAimVector():Angle() )
		prop:SetOwner( self.Owner )
		prop:Spawn()
             end

[/lua]

It sort of spawns the prop, but players can’t interact with it and you can’t undo it. Also, if you couldn’t tell, this code is from a SWEP.
What lines do I need to add to make it spawn properly?

[lua]if ( !gamemode.Call( “PlayerSpawnProp”, player, model ) ) then return end

local e = DoPlayerEntitySpawn( player, "prop_physics", model, iSkin, strBody )

if ( IsValid( player ) ) then
	gamemode.Call( "PlayerSpawnedProp", player, model, e )
end

-- This didn't work out - todo: Find a better way.
--timer.Simple( 0.01, CheckPropSolid, e, COLLISION_GROUP_NONE, COLLISION_GROUP_WORLD )

FixInvalidPhysicsObject( e )

DoPropSpawnedEffect( e )

undo.Create( "Prop" )
	undo.SetPlayer( player )
	undo.AddEntity( e )
undo.Finish( "Prop (" .. tostring( model ) .. ")" )

player:AddCleanup( "props", e )[/lua] Pulled from the GitHub repo. https://github.com/garrynewman/garrysmod/blob/e7111a4ab8318b69c3a29e2cbea6cc57ec0bcf5a/garrysmod/gamemodes/sandbox/gamemode/commands.lua#L157

-snip ninjas-

I’ve made this addition to my code;

[lua]
FixInvalidPhysicsObject( prop )
undo.Create( “Prop” )
undo.SetPlayer( self.Owner )
undo.AddEntity( prop )
undo.Finish( “Prop (” … model … “)” )
self.Owner:AddCleanup( “props”, prop )
[/lua]

But I’m still unable to interact with the prop.
I didn’t use DoPlayerEntitySpawn() because I don’t know how to get the iSkin and strBody variables.

Just call GMODSpawnProp. iSkin is the skin (usually 0), and strBody is something else, but you can set it to nil. Alternatively, call CCSpawn

I replaced my spawn line with this;
[lua]
local prop = DoPlayerEntitySpawn(self.Owner, “prop_physics”, model, 0, nil)
if not IsValid( prop ) then
self.Weapon:EmitSound( clickSound )
return
end
[/lua]

But I still can’t interact with the prop.



local model = string.lower(self.Owner:GetNWString("spawn_selection"))
	model = model:gsub("\\", "/")

local prop = ents.Create( "prop_physics" )
	if not IsValid( prop ) then
		return
	end

	prop:SetModel( model )
	prop:SetPos(self.Owner:GetShootPos() + self.Owner:GetAimVector() * 48)
	prop:SetAngles( self.Owner:GetAimVector():Angle() )
	prop:SetOwner( self.Owner )
	prop:Spawn()
	prop:Activate()

local phys = prop:GetPhysicsObject()
if IsValid(phys) then
	phys:Wake()
end

undo.Create("Launched Prop")
	undo.AddEntity(prop)
	undo.SetPlayer(self.Owner)
	undo.SetCustomUndoText("Undone Launched Prop")
undo.Finish()

cleanup.Register("Launched Prop")
cleanup.Add( self.Owner, "Launched Prop", prop )


I refactored my code to include the Activate and Awake statements:

[lua]

		prop:SetPos( self.Owner:EyePos() + (self.Owner:GetAimVector() * 48) )
		prop:SetAngles( self.Owner:GetAimVector():Angle() )
		prop:SetOwner( self.Owner )
		prop:Spawn()
		prop:Activate()

		local phys = prop:GetPhysicsObject()
		if not IsValid( phys ) then
			self.Owner:EmitSound( clickSound )
			return
		else
			phys:Wake()
		end

[/lua]
But I still can’t interact with the props I spawn.

And by the way, the spawn_selection NWString is set from the spawn menu, so it doesn’t need to be cleansed.

What do you mean you can’t interact with them? You’re nocollided with them, or they’re frozen?

They’re not frozen, and they can interact with eachother, but I can walk right through them, I can’t shoot them, and I can’t use the physgun on them.

What gamemode are you trying this in?
Are there any errors in console?
Do you have any tools installed that affect collision?

Sandbox, no, and no.

This only begun happening once I added the swep that spawns these props.

Is it with EVERY prop you spawn, or any props created with the gun?

Only the gun. Props spawned normally behave normally.

Would you mind posting more of the SWEP’s code or at the very least, PMing it to me? Perhaps you have something wrong in there.

Here’s the full code for the SWEP’s PrimaryAttack function. It should be helpful;
[lua]
local shootSound = Sound( “Weapon_Crossbow.Single” )
local clickSound = Sound( “Weapon_Pistol.Empty” )

function SWEP:PrimaryAttack()
if SERVER then
self.Weapon:SetNextPrimaryFire( CurTime() + 0.2 )
local model = self.Owner:GetNWString( “spawn_selection” )
if model ~= “” then
randx = ( math.random() - 0.5 ) * 0.3
randy = ( math.random() + 0.3 ) * -0.5
randz = ( math.random() - 0.5 ) * 0.6
self.Owner:ViewPunch( Angle( randy, randx, randz ) )

		local prop = DoPlayerEntitySpawn( self.Owner, "prop_physics", model, 0, nil )
		if not IsValid( prop ) then
			self.Owner:EmitSound( clickSound )
			return
		end

		prop:SetPos( self.Owner:EyePos() + (self.Owner:GetAimVector() * 48) )
		prop:SetAngles( self.Owner:GetAimVector():Angle() )
		prop:SetOwner( self.Owner )
		prop:Spawn()
		prop:Activate()

		local phys = prop:GetPhysicsObject()
		if not IsValid( phys ) then
			self.Owner:EmitSound( clickSound )
			return
		else
			phys:Wake()
		end

		phys:SetVelocity( self.Owner:GetAimVector() * 4000 )
		phys:AddAngleVelocity( Vector( math.random() * 20 - 10, math.random() * 20 - 10, math.random() * 20 - 10 ) * 200 )

		undo.Create( "Prop" )
			undo.SetPlayer( self.Owner )
			undo.AddEntity( prop )
		undo.Finish( "Prop (" .. model .. ")" )
		self.Owner:AddCleanup( "props", prop )

		self.Owner:EmitSound( shootSound )
	else
		self.Owner:EmitSound( clickSound )
	end
end

end
[/lua]

oh lawd, get rid of


prop:SetOwner(self.Owner)

Just realized that…

Thank you, that fixed it. Why was that causing the error?

[editline]6th April 2015[/editline]

I just looked up the gmod wiki entry for

Entity:SetOwner, and it seems to do something different than damage ownership, as I had assumed.