Prop spawning - Prop_physics not liking "Wake()"

Hello.

I’ve been working on a gamemode for the past few days, its supposed to mock STALKER.
I have the inventory laid out, and now I’m working on the functionality of it all.

I have my inventory panel, which is a frame with a “PanelList” inside it. I have it setup so that when i click a spawn icon, it will bring up a menu (if you’ve played stalker, when you right click it brings up a menu, and you can proceed to do whatever with the selected object, thats what I’m trying to recreate), with a few options.

The option I’m working on is dropping items out of your inventory. The only way i can see this happening is if I pass the model of the SpawnIcon to this function:

[lua]
function drop_item(a)

local ent = ents.Create("prop_physics")
local phys = ent:GetPhysicsObject()
local traceRes = LocalPlayer():GetEyeTraceNoCursor()

ent:SetModel(a)
ent:SetPos(traceRes["HitPos"]+Vector(0,0,5))
ent:SetMoveType(MOVETYPE_VPHYSICS)  
ent:Spawn()
phys:Wake()
phys:EnableGravity(true)  
phys:EnableMotion(true) 

end
[/lua]

(Eventually i will make the dropping of the item more pretty by making it seem like you toss it in front of you, but for now, using an EyeTrace will suffice.)

The problem arises when it hits “Wake().”
It creates the model at the specified vector(which i think is the entity, but it doesn’t move or collide), awesome. But it gives me this error:



cl_init.lua:196: Tried to use invalid object (type IPhysicsObject) (Object was NULL or not of the right type)


(Line 196 points to: phys:Wake() under the drop_item function)

I’m not really understanding what the problem is here, any and all help would be appreciated.

[editline]09:42AM[/editline]

I understand that if it were NULL, it doesn’t exist. But the mesh shows up, so wouldn’t that mean that the prop was created? And if its the wrong type, what other “types” are there?

If there’s something i don’t know and should, let me know. Really need this.
Or any alternatives.

And no, i will not use the sandbox gm_spawn.

If you want the user to be able to take that object to the ground, you should make a sENT with an OnUsed function that makes it disappear and adds it to your inventory. Then you spawn one copy of that sENT and change its model to fit the dropped object. Check the wiki how to make a sENT (maybe the jumping bathub tutorial, it should contain what you need) and the wake problem should be fixed.

Well to get the spawn icon, you need a model path, so i was just thinking i could identify the objects based on their model. Seeing as how all interactivity with the objects are done within the inventory ui, i wouldn’t really need anything other than SWEPs, since everything else would modify internal values and whatnot.

Just kind of weird i can’t get it to spawn a simple physics_prop… I could probably use SENTs to A. Get the physics props down and 2. be able to externally fire a “item picked up” function.

I’ll look into it, thanks.

[lua]

local function drop_item(a) – Either localize your function or add it into a table (i.e. MyGameModFuncs.FuncName)

local ent = ents.Create("prop_physics")  
local traceRes = LocalPlayer():GetEyeTraceNoCursor() --Remember GetEyeTraceNoCursor is a sandbox function.
  
ent:SetModel(a)  
ent:SetPos(traceRes.HitPos + traceRes.HitNormal*5) -- Simpler syntax. Also want to use hitnormal in case you aren't looking at the floor.
ent:SetMoveType(MOVETYPE_VPHYSICS)    
ent:Spawn()  
local phys = ent:GetPhysicsObject() --This line should be down here, AFTER it's spawned.
if phys:IsValid() then  --Always want to make sure the phys is valid, too. Redundant mostly, but helpful at times.
    phys:Wake()  
    phys:EnableGravity(true)    
    phys:EnableMotion(true)   
end

end

[/lua]

You want to get the physics object and associated functions AFTER it’s spawned.

Well, I don’t get any errors, using the fixed block. But, the props aren’t colliding, nor are they physically enabled.

I can’t localize the function, because the function that runs drop_item() says i can’t find it if i do.

What does adding it to a table do for me? And could i get some kind of example?

I’m still learning.

try calling ent:Activate() on line 16 of the example above.

I added a debug PrintMessage in the IsValid() conditional. All the props i try to spawn are not valid.
Any type of fix?

And added the Activate() command. Doesn’t look like that’ll matter, because the prop is not a valid physics prop?..

[editline]10:26PM[/editline]

Alright, well instead of d*cking around with this prop spawning crap, lets go with SENTs.

It’d still be the same code with a SENT, i’d imagine. So i just need it to spawn the SENT instead of a “prop_physics”.

Where would i find the bouncy ball scripts? I just want something to work from, and i’d imagine its already setup as a physical object.

Or better yet, where can i find a SENT tutorial?