Having my entity run a console command

I would like to run a console command when my entity is used.
Here is the code I have tried:


function ENT:Use( ply )

	if ( ply:IsPlayer() ) then
	
		RunConsoleCommand( 'opencp' )
		
	end

end

What am I doing wrong?

  • Thanks!

Should the command be ran on the server or by the player?
I guess it’s by the player in this case use this…



ply:SendLua("RunConsoleCommand('opencp')")


Oh it worked! Thank you!

Actually one more thing. Why does it open more than one? It takes like 10 clicks to close it now?

Since use of SendLua is frowned upon, I suggest you use Player:ConCommand instead.
It’s a bad idea to get into the habit of using SendLua when there are better alternatives.

Ok i’ll use that. Any idea why it takes so many clicks to close?

Ah I think it’s because ENT:Use is called more than once; once for every frame the user has their key down. Try setting the use type to SIMPLE_USE in ENT:Initialize

How would I code that out?

Like this?:


function ENT:Initialize( SIMPLE_USE )

ENT:Initialize is an entity method run when the entity is created. It’s used just like ENT:Use. You have to give SetUseType the SIMPLE_USE enumeration as the argument, running the function on the entity, passed as “self” in any ENT: method.
[lua]function ENT:Initialize()

self:SetUseType( SIMPLE_USE )

end[/lua]

Everything is working great but how come i’m getting this error?


[ERROR] gamemodes/sandbox/entities/entities/npc.lua:20: attempt to call method 'SetUseType' (a nil value)
  1. unknown - gamemodes/sandbox/entities/entities/npc.lua:20


Ah sorry, it’s a server side method only, so do this:
[lua]function ENT:Initialize()

if SERVER then self:SetUseType( SIMPLE_USE ) end

end[/lua]

Ok thank you. Also I want to put images on my derma panel. How do I get the actual images on to the server?

You can use http.Fetch to grab the data of images and load it into a txt file, then material. Willox has an example for that: https://dl.dropboxusercontent.com/u/26074909/code/material_data.lua

Or, you can just put png files in your fastdl and load them as you would any other Material.

That sounds much harder than I thought. But thank you.

There are other ways… HTML panel with url to image. To cache you can download them as txt and show them as shown above. You can add them to fastdl / workshop download ( probably best way ) etc…

How would I add them to a workshop download?

You should be able to package them in a .gma file and upload it.

Ok thanks so much :D. Also one more question: I’m trying to make my entity model a hl2 character ragdoll. I can do it but no matter what it goes into T pose. Any ideas on how to make the arms go down?

Create a prop_dynamic and use the idle animation with SetSequence

I dont really understand? Could I have an example?