return the phys object of any spawned entity

Morning,

I’m trying to run a function on every prop upon it being spawned. The code looks as follows…



function GM:OnEntityCreated( ent )
		--First we shrink the prop on the server
		--So to start off, initialize normal physics so we can get our mesh vertices
		ent:PhysicsInit( SOLID_VPHYSICS )
		ent:SetSolid( SOLID_VPHYSICS )
		ent:SetMoveType( MOVETYPE_VPHYSICS )

		--Next, we store a local variable filled with the initial vertices
		local phys	= ent:GetPhysicsObject()
		local convexes	= phys:GetMeshConvexes()	

		--Begin the clipping procedures, GetMeshConvexes returns vertices' coordinates as local vectors in relation
		--to the model origin, therefore we need to simply multiply each individual convex's local vector by a scalar

		local miniconvex = minitize( convexes )

		--Then we reinitialize our new clipped physics

		ent:PhysicsInit( SOLID_VPHYSICS )
		ent:SetMoveType( MOVETYPE_VPHYSICS )

		ent:PhysicsInitMultiConvex( miniconvex )

		--Then we enable custom collisions, unsure if it is actually as self explanatory as it seems
		self:EnableCustomCollisions( true )

end


There’s a bit more code but that’s what’s erroring, its returning an error on prop spawning saying that its a null entity and I can’t figure out what’s wrong. When I try and add a verification in an if statement, justa simple if IsValid( ent) and IsValid( ent:GetPhysicsObject() ) it skips the code entirely upon spawning a normal prop via the spawn menu.

Any help?

minitize is a function that just multiplies all values in a table by a scalar.

You need a capital G

Also, I would recommend checking whether your PhysicsObject is valid before using it. Not checking that may lead to errors, too.

[editline]23rd October 2013[/editline]

Also you haven’t defined a name for your variable.
[lua]
local physObj = ent:GetPhysicsObject()
[/lua]

[editline]23rd October 2013[/editline]

And returning something within OnEntityCreated wouldn’t really do much, unless you use hook.Call to call it and then use the return, but that is not something you should do at all.

Unless you mean that you want to use the return of GetPhysicsObject, in which case I misunderstood your question.

Thanks for the prompt reply, the lowercase g is a typo in the original post, I’ll edit it with a more info and fix my typos. Got to a computer so i can copy paste the code.

That doesn’t really seem like something you should do in that specific hook. All created entities go through that hook, you should do some checking first to make sure you only run it on the entities you want to run it on.

[editline]23rd October 2013[/editline]

By using GetClass and checking it against a table of valid entity classes, for example.

The overall goal is to shrink every prop spawned on the server, do you know an alternative method to find everything that was spawned or is being spawned?

Every entity would also include entities without models (e.g. controller entities). You don’t want to run this code on those. You would want to check whether it’s a prop_physiscs or prop_physics_multiplayer.

[editline]23rd October 2013[/editline]

Also if you want all props spawned by players you can use this. The gamemode has to be a sandbox deviate and the spawning system has to utilize sandbox’s spawning system, though.

I added a quick check to the function so that the physics modification only runs when the entity class is prop_physics. I’m still getting errors on the get physics object line stating that it tried to use an invalid object (type IPhysicsObject)

EDIT: gonna try with player spawned prop as you suggest, It’s a sandbox derivative so it’ll be fine.

EDIT TWO:I just tried with the GM:PlayerSpawnedProp() function and it worked without a hitch, thanks.

On a side note, I have another question…



function shrink( vectors )
	local result = {}
	
	for index, vector in pairs( vectors ) do
		local shrunk = vector * 0.5
		result[index] = shrunk 
	end
	
	return result
end

function minitize( table )
   local temptable = {}
   for index, vector in pairs( table ) do
     temptable[index] = shrink(vector)
   end
   return temptable
end



As far as I’m aware, GetMeshConvexes() returns a table of vectors (table of tables) containing local vectors of the coordinates of the mesh vertices in relation to the main prop origin. The two functions above are my method of shrinking it down, just a function that goes through the full table applying the actual shrink function to each individual vector. I’m still coming up with errors stating I’m trying to perform arithmetic on a table.

Do you have any experience with GetMeshConvexes()? I don’t know EXACTLY what it gives me back.

If you want to know what it gives back, simply use PrintTable on it.