• Running Commands via NPC
    7 replies, posted
Hi, So I'm attempting to run commands from addons via a NPC. My current .lua's are pretty much cut and paste from others and I've just done trial and error trying to get this console command to run. Here are my three files: shared.lua ENT.Type = "ai" ENT.Base = "base_ai" ENT.PrintName = "gLevel NPC" ENT.Author = "N/A" ENT.Purpose = "To open gLevel" ENT.Spawnable = true ENT.AdminSpawnable = false init.lua AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:Initialize() self:SetModel("models/player/kleiner.mdl") self:SetHullType( HULL_HUMAN ) self:SetHullSizeNormal( ) self:SetNPCState( NPC_STATE_SCRIPT ) self:SetSolid( SOLID_BBOX ) self:CapabilitiesAdd( CAP_ANIMATEDFACE, CAP_TURN_HEAD ) self:SetUseType( SIMPLE_USE ) self:DropToFloor() self:SetMaxYawSpeed( 90 ) endfunction ENT:OnTakeDamage()returnfalseendfunction ENT:AcceptInput(name, activator, caller)if name == "Use"and caller:IsPlayer() then RunConsoleCommand("gLevel.openMenu") endend cl_init.lua include("shared.lua") function ENT:Draw() self:DrawModel() end Again, a lot is cut and paste. In the above I'm attempting to open the gLevel menu from the addon, gLevel. It says its an "unknown command" so I assume RunConsoleCommand might only work with traditional commands and none from addons? If this is so, how do I go about achieving: the user presses E (use) on a NPC and it opens up the menu from gLevel. I have seen this accomplished on other servers (even with gLevel) and assumed it was via NPC, but if its another addon I need that does this please do let me know. Thanks! **EDIT** Some of those code pasted on one line for whatever reason, example the ENT:OnTakeDamage and ENT:AcceptInput functions. When I try to add spaces/new lines it doesnt seem to work.
Net Library Usage You should network it instead of using a console command if you want to bring up any derma menu.
Thanks for the suggestion Leeroy . So I've read over the documentation you linked, as well as watched some videos on gmod networking. They talked about sending and receiving which I now have a brief understanding of. I'm not really all that sure on how to implement this however (I'm pretty much brand new to gmod lua). From what I understand you start, then do the writes, then do the send to the client. I might be wrong about that though. So in the write section what would be the method of writing the command and opening the menu from the addon? Sorry for that lack of understanding, again fairly new to this
Also, you should also use the "Use" function instead of the "AcceptInput" function. You don't need to write anything to the client if you want the client to open a derma menu. Example: if SERVER then util.AddNetworkString("mycoolmenu_netstring") -- add the network string function openmenuforplayer( ply ) -- make a function which you can call later at any time net.Start( "mycoolmenu_netstring" ) net.Send( ply ) end else net.Receive("mycoolmenu_netstring", function( len, pl ) -- when u receive it then make a derma menu. local Frame = vgui.Create( "DFrame" ) Frame:SetPos( 100, 100 ) Frame:SetSize( 200, 200 ) Frame:SetTitle( "This is a titel for the menu" ) Frame:SetVisible( true ) Frame:SetDraggable( false ) Frame:ShowCloseButton( true ) Frame:MakePopup() end) end The code above is just an example, but you could make something like it. You said you wanted to bring up the "gLevels" menu. Your best bet would be to search through its code and try to find its network string. ( Use CTRL+F, if you're using Atom, Sublime or Notepad++ ( or any other IDE where that shortcut works ) ) Then you could just make a function which sends the string like this: function openmenuforplayer( ply ) net.Start("gLevels_menu_networkstring_here") -- not the actual nw string net.Send( ply ) end And for the entity's use function you could do this: function self:Use( activator, caller ) if( IsValid(caller) && caller:IsPlayer() ) then openmenuforplayer(caller) end end
Must of done something wrong here, so I located the util.AddNetworkString's you talked about (there were three), and did the following: function openmenuforplayer( ply )             net.Start("NetWrapperVar") -- not the actual nw string net.Send( ply ) function ENT:Use( activator, caller ) if(IsValid(caller) && caller:IsPlayer()) then openmenuforplayer(caller) end end So as I said, there were three of the util.AddNetworkString (util.AddNetworkString( "NetWrapperVar" ), util.AddNetworkString( "NetWrapperRequest"), and util.AddNetworkString( "NetWrapperClear" )) Tested all three of them within the net.Start and none of them seemed to work. Looks like there isn't anymore strings either. The way its behaving at the moment: With *net.Start("NetWrapperVar")* I get an error (img below), not a syntax error or anything like that. https://files.facepunch.com/forum/upload/354173/34ef0cbb-43f8-4410-afee-236e4033e23c/Error.png Nothing happens upon "Use" With *net.Start("NetWrapperRequest")* Nothing happens upon "Use" With *net.Start("NetWrapperRequest")* Nothing happens upon "Use" I also figured that it might(?) be useful to show my file directory for this (img below), just in case I have done something wrong there. Pretty much all I have done is withing the **gLevel** addon > **LUA** > I created **entities** > **gLevel NPC** > and with in that are my .lua files. https://files.facepunch.com/forum/upload/354173/b29e5297-78a5-4d73-bcc5-6a11dc0ad624/FileDir.png
So, have you looked at the file with the error on it? In the error itself it states that in glevel/lua/gLevel/core/sh_net.lua on line 98 there is an error about reading a certain type. Pull up that line and look what could be causing an issue. It shouldn't be that bad to isolate it.
Yeah I did take a look at it, the line is: `local value  = net.ReadType( typeid )` It's apart of the original code from the addon creator, I didn't touch anything within addon itself, only copied the util.AddNetworkString's. Something in regards to me adding net.Start("NetWrapperVar") and using specifically "NetWrapperVar" has an effect on that line, but I'm pretty clueless on why that is.
Take a look in the original addon and see what value it's trying to send for net.WriteType. I assume in your new message that you're not writing the correct type, which is causing the net.ReadType to error.
Sorry, you need to Log In to post a reply to this thread.