• Gmod lua programming - spawning props through models
    13 replies, posted
Hey, i got this function online: [CODE] function spawnprop( ply, props ) local tr = ply:GetEyeTrace() -- Create a trace to where the player is looking if tr.HitNonWorld then return end -- If the player is not looking at the ground then return local ent=ents.Create("prop_physics") ent:SetModel( props ) ent:SetPos( tr.HitPos ) ent:Spawn() ent:EmitSound("npc/scanner/combat_scan5.wav") end [/CODE] I got it to work with some other code that i did and .mdl file that i somehow found: "models/props_c17/oildrum001.mdl" The thing is, that i want, lets say, spawn the chair prop, oven and pot. But i have no idea where to find the mdl or whats the path to them Anyone knows? 2nd question: How can i do that some props after a few bullet shots/taking damage will break/blow up? Cheers!
1) Go to spawn menu in Sandbox, find the prop you want and right click on it > Copy to Clipboard. 2) Some props do that on their own. For the rest you will have to give them health ( Not sure if Entity:SetHealth() will work ) and then check their health in "On damaged" hook and blowup/remove the prop if health < 0.
Thanks a lot mate, appreciate it.
[QUOTE=Robotboy655;48149722]1) Go to spawn menu in Sandbox, find the prop you want and right click on it > Copy to Clipboard. 2) Some props do that on their own. For the rest you will have to give them health ( Not sure if Entity:SetHealth() will work ) and then check their health in "On damaged" hook and blowup/remove the prop if health < 0.[/QUOTE] Hi again. So first of all i tried both of your solutions. The first one worked perfectly, but i didn't quiet understood how to use the hook (im very new to gmod lua) if this: [url]http://wiki.garrysmod.com/page/GM/EntityTakeDamage[/url] is the hook, i would love it if you put some code of how to use it.
[QUOTE=KosKoss;48155724]Hi again. So first of all i tried both of your solutions. The first one worked perfectly, but i didn't quiet understood how to use the hook (im very new to gmod lua) if this: [url]http://wiki.garrysmod.com/page/GM/EntityTakeDamage[/url] is the hook, i would love it if you put some code of how to use it.[/QUOTE] [B]Not a code that will work, just some guidance[/B] [CODE]local EntityHP = Entity:Health() --We set EntityHP to get the Entity's health function RemoveEntity() if EntitiyHP < 0 then --If Entity health is below 0 continue Entity:Remove() -- Remove the Entity end hook.Add( "OnTakeDamage", "RemoveEntityBelow0HP", RemoveEntity ) --When an entity takes damage, run the command 'RemoveEntity' [/CODE]
[QUOTE=JasonMan34;48155868][B]Not a code that will work, just some guidance[/B] [CODE]local EntityHP = Entity:Health() --We set EntityHP to get the Entity's health function RemoveEntity() if EntitiyHP < 0 then --If Entity health is below 0 continue Entity:Remove() -- Remove the Entity end hook.Add( "OnTakeDamage", "RemoveEntityBelow0HP", RemoveEntity ) --When an entity takes damage, run the command 'RemoveEntity' [/CODE][/QUOTE] Aha, thanks a lot mate! [editline]9th July 2015[/editline] [QUOTE=JasonMan34;48155868][B]Not a code that will work, just some guidance[/B] [CODE]local EntityHP = Entity:Health() --We set EntityHP to get the Entity's health function RemoveEntity() if EntitiyHP < 0 then --If Entity health is below 0 continue Entity:Remove() -- Remove the Entity end hook.Add( "OnTakeDamage", "RemoveEntityBelow0HP", RemoveEntity ) --When an entity takes damage, run the command 'RemoveEntity' [/CODE][/QUOTE] Okay i just realized something: The hook`s function does not take as an argument the target that took damage. If so, how can i know whats the target and remove that one? All of this sits on the init.lua?
Bump
Its been 3 hours, chill out
[QUOTE=Exho;48158184]Its been 3 hours, chill out[/QUOTE] hey friend :))))) [QUOTE=KosKoss;48156502]Okay i just realized something: The hook`s function does not take as an argument the target that took damage. If so, how can i know whats the target and remove that one? All of this sits on the init.lua?[/QUOTE] I'm going to assume if this hook did not work then you are not formally creating an entity. The OnTakeDamage hook works when you create and register an entity, but needs to exist within the entity's context. Your suspicion of [URL="http://wiki.garrysmod.com/page/GM/EntityTakeDamage"]EntityTakeDamage[/URL] was correct in this scenario. In your case, you would adjust to something like: [CODE] function RemoveEntity( ent ) if ent:Health() < 0 then ent:Remove() end end hook.Add( "EntityTakeDamage", "RemoveEntityBelow0HP", RemoveEntity ) [/CODE] As I'm sure you have guessed, EntityTakeDamage passes in both the affected entity and respective damage information (should you need it for anything) as arguments.
[QUOTE=iSchmal;48159049]hey friend :))))) I'm going to assume if this hook did not work then you are not formally creating an entity. The OnTakeDamage hook works when you create and register an entity, but needs to exist within the entity's context. Your suspicion of [URL="http://wiki.garrysmod.com/page/GM/EntityTakeDamage"]EntityTakeDamage[/URL] was correct in this scenario. In your case, you would adjust to something like: [CODE] function RemoveEntity( ent ) if ent:Health() < 0 then ent:Remove() end end hook.Add( "EntityTakeDamage", "RemoveEntityBelow0HP", RemoveEntity ) [/CODE] As I'm sure you have guessed, EntityTakeDamage passes in both the affected entity and respective damage information (should you need it for anything) as arguments.[/QUOTE] Thanks a lot, and sorry for bumping again, totally my bad. Since then, i have 1 more question. Lets say i hold on the server the player`s money. How do i pass the variable to the cl_init? Is just globalizing the variable will do the charm? Edit: the solution didnt work eventually [CODE] hook.Add("EntityTakeDamage","RemoveEntityBelow0Hp",function(ent) if(ent:Health() < 0) then ent:Remove() end end) [/CODE] spawning entity: [CODE] function spawnprop( ply, props ) local tr = ply:GetEyeTrace() -- Create a trace to where the player is looking if tr.HitNonWorld then return end -- If the player is not looking at the ground then return local ent=ents.Create("prop_physics") ent:SetModel( props ) ent:SetHealth(25) ent:SetPos( tr.HitPos ) ent:Spawn() ent:EmitSound("npc/scanner/combat_scan5.wav") end [/CODE]
bumperino
Man, quit bumping. People are throwing code at you and you are not putting effort into debugging the clusterfuck that insues when you copy that into your project.
[QUOTE=Exho;48177454]Man, quit bumping. People are throwing code at you and you are not putting effort into debugging the clusterfuck that insues when you copy that into your project.[/QUOTE] So maybe if you guys stop "throwing code at me" and explained me the concept or guide me through it i wouldn't need any help by now.
[QUOTE=KosKoss;48188878]So maybe if you guys stop "throwing code at me" and explained me the concept or guide me through it i wouldn't need any help by now.[/QUOTE] The only real way you're going to better yourself is putting in the work and effort yourself. You have to really figure out some of these things on your own. Someone can help you on to a bike but eventually they're going to have to let go and it's up for you to decide on if you fall off or keep your balance. First you can start by explaining what you want in better context from your other previous "not a bump" post. I have no idea what you are trying to say.
Sorry, you need to Log In to post a reply to this thread.