Passing Args to Functions

[lua]function fbcTest(entType)
local entType = entType || “logic_case” // logic_case, info_player_start, npc_, etc…
local spawn = ents.FindByClass(entType)
for i=1,100 do
if spawn
==nil then
if i==1 then
player.GetByID(1):ChatPrint(“Entity Count: 0”)
print(spawn*:GetClass(), " ["…i…"] located at: ", spawn*:GetPos())

concommand.Add(“fbctest”, fbcTest)[/lua]

The previous code is in the shared.lua file of a gamemode. I want to be able to call this function via console command (hence line 16), and be able to give it arguments to change the value of entType to whatever string I give it. However, for some reason, entType is ALWAYS auto-initialized to my player. Not only that, but it ignores any arguments I try to give it from the console (by typing “fbctest “npc_*””), and continues to force entType to my player.

I need entType to be a string so I can plug it in to FindByClass. However, whenever I try to run the function, it gives me the error: **gamemodes
eospacebuild\gamemode\shared.lua:12: bad argument #1 to ‘FindByClass’ (string expected, got Player)

Can anybody tell me why 1) entType is initialized to my player, and 2) how to get this function to accept arguments from the console?

Thanks in advance.

Concommands take three arguments, the first is the player that sent the command, the second is the command that was sent, and the third is a table of arguments.
function fbcTest(Pl, Cmd, Args)
local entType = Args[1] || “logic_case”

Thanks a lot, that really helps me out. Now I can call this command from my Derma in cl_init.lua.

Would there be a way to get a button to call this function directly, giving the function the arg entType?

[lua]button.DoClick = function() callFunction( entType ) end[/lua]

You mean like that?

That works part of time, such as when I pass it the string “npc_*”, but for some reason, when I pass it “logic_case” it replies that there are none. I know for sure that there are logic_cases, because I verified it using a test version of this script (not to mention I have the VMF). I have a feeling it doesn’t have to do with syntax, but with the fact that I am passing arguments from a clientside vgui to a custom shared function running a serverside function (FindByClass).

Does anyone know the actual reason?

EDIT: I read in one of these posts that the only way to send data from client to server is via console command. I suppose I can live with that.

logic_case is a map trigger entity, so it’s only accessible serverside.

[lua]button.DoClick = function() RunConsoleCommand(“fbctest”, “logic_case”) end[/lua]


You should run the console command, not the function itself, else it will run clientside, and that’s not what you want. Running the console command actually runs the serverside function bound to this command, and that’s what you need.

I did almost exactly what _Kilburn said and it works perfectly. I really appreciate all the help! I wish I’d picked up on this a little sooner in learning GLua, but better late than never.