lua entity not solid.

I am getting two errors, the first error being that the prop isn’t a solid object and you can just walk through it. The second error is that the vector locations of the prop / entity would just change to a random location.


function SpawnProp(position, ang , model, color)
    -- Create the entity.
	local ent1 = ents.Create("prop_physics") 
	-- Set angles for entity.
	ent1:SetAngles(ang)	
	-- Modelize the entity.
	ent1:SetModel(model)	
	-- Set the position of the entity.
	local pos = position
	pos.z = pos.z - ent1:OBBMaxs().z
	ent1:SetPos( pos )	
	-- Spawn the entity.
	ent1:Spawn()	
	-- Set the entity's color.
	ent1:SetColor(color)
	-- Make entity solid.
	ent1:PhysicsInit(SOLID_VPHYSICS)
	ent1:SetMoveType(MOVETYPE_VPHYSICS)
	ent1:SetSolid(SOLID_VPHYSICS)
end
   
  -- POSITION (VECTOR), ANGLE (VECTOR), MODEL(STRING), COLOR(COLOR VECTOR).
local function SpawnProps()
  SpawnProp(Vector( 5550, -4000, 155 ), Vector(-0, 120, 0), "models/tdmcars/murcielago.mdl", Color(225,205,37,255))
  SpawnProp(Vector( 5545, -4140, 155 ), Vector(-0, 120, 0), "models/tdmcars/sl65amg.mdl", Color(10,10,10,255))
  SpawnProp(Vector( 5540, -4270, 155 ), Vector(-0, 120, 0), "models/tdmcars/ferrari512tr.mdl", Color(208,30,30,255))
  SpawnProp(Vector( -7047, -6396, 152 ), Vector(-0, 270, 0), "models/tdmcars/cit_c4.mdl", Color(244,244,244,255))
  SpawnProp(Vector(-7569,13261,532),Vector(0,90,0),"models/LarasBoat/larasboat.mdl", Color(225,255,255,255))
end
hook.Add("InitPostEntity","SpawnTheProps",timer.Simple(1,SpawnProps))

I have this in cl_init for a gamemode. This is my first script, so please excuse my failness.

Can’t spawn phys props on the client using ent.Create.

This is somewhat off topic, but:

[lua]
hook.Add(“InitPostEntity”,“SpawnTheProps”,timer.Simple(1,SpawnProps))
[/lua]

This hook is written incorrectly.
It looks like you wanted it to run a 1-second timer after all entities have been spawned.

What you’re doing is running timer.Simple immediately (which creates a 1-second timer as soon as it’s called) and providing timer.Simple’s returned value to hook.Add.
Since timer.Simple always returns true, you’re giving true as the third argument of hook.Add. However, hook.Add requires the third argument to be a function.

There are a few ways you can resolve this.

Do this:
[lua]
hook.Add( “InitPostEntity”, “SpawnTheProps”, timer.Simple, 1, SpawnProps )
[/lua]

or this:
[lua]
hook.Add( “InitPostEntity”, “SpawnTheProps”, function() timer.Simple( 1, SpawnProps ) end )
[/lua]

or this:
[lua]
local function OnInitPostEntity()
timer.Simple( 1, SpawnProps )
end
hook.Add( “InitPostEntity”, “SpawnTheProps”, OnInitPostEntity )
[/lua]

Okay, now, back on topic:

Yes you can. I use this to create gibs for broken objects in Itemforge (in a clientside effect).

kragmars, are you sure you want to create the models clientside?
See if you can create the models serverside first. Clientside props work, but things don’t always work the same way on the client as they do on the server.
Besides, usually people want to create these things serverside, so everyone sees the same model.

Second, are you sure the original models all have physics?

Third, there are a couple of things wrong with this code…
[lua]
local pos = position
pos.z = pos.z - ent1:OBBMaxs().z
ent1:SetPos( pos )
[/lua]
The first thing to mention, Vectors behave the same way as tables in Lua do.
In Lua, tables are never copied unless you explicitly create a new table and manually copy it’s contents. The same goes for vectors.
On this line:
[lua]
local pos = position
[/lua]
I think you meant to create a copy of position called pos, and then change the copy’s z-coord below it.
But, this is not what happened. You just gave the same vector (position) a different name that it could be referred to by (pos).
Then, below that line, when you changed it’s z-coordinate, it wound up changing the ORIGINAL VECTOR’s z-coordinate.

To fix this, you can replace this…
[lua]
local pos = position
pos.z = pos.z - ent1:OBBMaxs().z
ent1:SetPos( pos )
[/lua]
with this…
[lua]
local pos = Vector( position.x, position.y, position.z - ent1:OBBMaxs().z )
ent1:SetPos( pos )
[/lua]

Now that brings me to the next thing I wanted to talk about here. Why are you changing the position?
The way the code has been designed, if you give a spawn position on the floor, for instance, the model will wind up stuck below the floor.
Did you mean to do position.z + ent1:OBBMaxs().z?

Also, keep in mind that ent1:OBBMaxs() and ent1:OBBMins() don’t necessarily have the same values, with one being positive and one being negative.
I suggest doing 0.5 * ( ent1:OBBMaxs().z - ent1:OBBMins().z ) instead of just ent1:OBBMaxs().z:

[lua]
local pos = Vector( position.x, position.y, position.z + 0.5 * ( ent1:OBBMaxs().z - ent1:OBBMins().z ) )
ent1:SetPos( pos )
[/lua]

Fourth, make sure ent1:Spawn() is the LAST thing that gets called in your function. Make sure it gets called AFTER PhysicsInit!

Fifth, you only need ent1:PhysicsInit(SOLID_VPHYSICS); you don’t need to set the movetype / solidtype. PhysicsInit will take care of this for you.