Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google Log In with Steam Sign In
  • Create Account
Photo

Lua API Questions

- - - - - lua api scripting

  • Please log in to reply
65 replies to this topic

#1
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction
I am working on teaching myself Lua for modifying ET. So far, it has been going very well, but I ran into a problem that I am having a hard time figuring out. I'm trying to use the following function...
clientnum = et.G_ClientNumberFromString( string )

Here is the code that I am using...
function et_ClientCommand(clientNum, command)
   if et.trap_Argv(0) == "listclientnumber" then
      if et.trap_Argc() == 2 then
         local targetClientNum = et.G_ClientNumberFromString(et.trap_Argv(1))
         et.trap_SendServerCommand(clientNum, string.format("cpm \"%d\"", targetClientNum))
      else
         et.trap_SendServerCommand(clientNum, "cpm \"Error: Need two arguments!\"")
      end
      return 1
   end
   return 0
end

When I execute the command to run the above code (e.g. "/listclientnumber JvI" in my game console), ET gives an unknown command error due to the G_ClientNumberFromString line. Even if I manually put a string instead of using the argument for the parameter, I still have this problem. If I remove that line and modify the line below it to just print text, it works exactly as intended. The client number function was directly from the lua.html that's located in the silent zip archive. Am I using it incorrectly, or is the documentation possibly out of date?

My second question is what is the best way to print text to a clients console? I noticed cpm acts just like echo and also flashes the text on the left side of the main screen. I'd prefer if there was a way of only printing to the console instead so my custom command behaves closer to the built in admin commands.

For my last question, is there any way to define standard admin commands that start with '!' instead of the console commands that start with '/' through Lua. I know you can map the admin commands to the console commands, but then I need to restrict the level in the script as well as the shrubbot.cfg file to prevent everyone from using it. Or perhaps there is another way to do this?

Thanks,
JvIasterMind

#2
OFFLINE   Sol

Sol

    Regular Member

  • Coders
  • PipPipPip
  • 113 posts
28
On a distinguished road
  • LocationPoland

When I execute the command to run the above code (e.g. "/listclientnumber JvI" in my game console), ET gives an unknown command error due to the G_ClientNumberFromString line. Even if I manually put a string instead of using the argument for the parameter, I still have this problem. If I remove that line and modify the line below it to just print text, it works exactly as intended. The client number function was directly from the lua.html that's located in the silent zip archive. Am I using it incorrectly, or is the documentation possibly out of date?

G_ClientNumberFromString is bugged, you can do it by yourself or just search algorithm for it.

My second question is what is the best way to print text to a clients console? I noticed cpm acts just like echo and also flashes the text on the left side of the main screen. I'd prefer if there was a way of only printing to the console instead so my custom command behaves closer to the built in admin commands.

this is my small API ( http://mygamingtalk....-api/#entry8157 ) a lot of easier with it but you can also use
et.trap_SendServerCommand( clientnum, 'print "text"' )
it's exactly what do you want "print" printing text into client's console.

For my last question, is there any way to define standard admin commands that start with '!' instead of the console commands that start with '/' through Lua. I know you can map the admin commands to the console commands, but then I need to restrict the level in the script as well as the shrubbot.cfg file to prevent everyone from using it. Or perhaps there is another way to do this?

It's possible to use shrubbot into LUA too, but its some work to do then you have to prase it and use it.

#3
OFFLINE   gaoesa

gaoesa

    Advanced Member

  • Management
  • PipPipPipPip
  • 4391 posts
341
Will become famous
  • LocationFinland
Would it be possible to use Lua calls in shrubbot custom commands? Maybe trapping server console commands in Lua script?

EDIT:
It looks like the et.G_ClientNumberFromString would be actually just et.ClientNumberFromString. Does that work?

#4
OFFLINE   Sol

Sol

    Regular Member

  • Coders
  • PipPipPip
  • 113 posts
28
On a distinguished road
  • LocationPoland

It looks like the et.G_ClientNumberFromString would be actually just et.ClientNumberFromString. Does that work?

et.ClientNumberFromString works indeed so, there is mistake in doscs.

Would it be possible to use Lua calls in shrubbot custom commands? Maybe trapping server console commands in Lua script?

something like shrubbot file but for lua, because shrubbot would be have errors probably yes?

#5
OFFLINE   gaoesa

gaoesa

    Advanced Member

  • Management
  • PipPipPipPip
  • 4391 posts
341
Will become famous
  • LocationFinland
I was thinking something like this:

[command]
command = example
exec = mycommand param1 param2
desc = Invoking Lua from custom commands
levels = 0 1 2 3 4 5

And in the Lua script somthing like
function et_ConsoleCommand()
   -- check if it is a command to intercept
   -- if so, read parameters and execute the associated Lua functions
end

About the ClientNumberFromString, it seems to be the same thing with the ETPub. I'm wondering if it should be fixed to match the documentation or if documentation should be fixed to match the current function name. It depends about which syntax is more used and changing it would cause bigger problems.

#6
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction

this is my small API ( http://mygamingtalk....-api/#entry8157 ) a lot of easier with it but you can also use

et.trap_SendServerCommand( clientnum, 'print "text"' )
it's exactly what do you want "print" printing text into client's console.


Your API looks very nice! I just skimmed the file and already learned a lot! For now, though, I am going to start by manually coding so I learn it better. I'll look into it again after I feel more comfortable with the scripting.


et.ClientNumberFromString works indeed so, there is mistake in doscs.


That is great to know! I know I could have coded something similar myself, but I figure it is better not to reinvent the wheel. I'll play with that command later today.


I was thinking something like this:

[command]
command = example
exec = mycommand param1 param2
desc = Invoking Lua from custom commands
levels = 0 1 2 3 4 5

And in the Lua script somthing like
function et_ConsoleCommand()
   -- check if it is a command to intercept
   -- if so, read parameters and execute the associated Lua functions
end

About the ClientNumberFromString, it seems to be the same thing with the ETPub. I'm wondering if it should be fixed to match the documentation or if documentation should be fixed to match the current function name. It depends about which syntax is more used and changing it would cause bigger problems.


Is that functionality already available, or are you thinking of possibly adding it? Either way, I think that would be great functionality to have because the '/' commands feel kind of awkward in silEnT and seem to be a lot less flexible.

Just out of curiosity, I am guessing that et_ConsoleCommand() intercepts rcon commands and server console commands? Would this mean you would be able to also run the Lua script through rcon?

#7
OFFLINE   gaoesa

gaoesa

    Advanced Member

  • Management
  • PipPipPipPip
  • 4391 posts
341
Will become famous
  • LocationFinland

Is that functionality already available, or are you thinking of possibly adding it? Either way, I think that would be great functionality to have because the '/' commands feel kind of awkward in silEnT and seem to be a lot less flexible.


The custom commands are an existing feature. http://mygamingtalk....ot#shrubbot.cfg

Just out of curiosity, I am guessing that et_ConsoleCommand() intercepts rcon commands and server console commands? Would this mean you would be able to also run the Lua script through rcon?


That is precisely what I was thinking. I'm not sure what you mean by runing Lua scripts through rcon, but there is no rcon command to execute scripts. However, the script that is loaded on map start can be triggered to do things.

#8
OFFLINE   Sol

Sol

    Regular Member

  • Coders
  • PipPipPip
  • 113 posts
28
On a distinguished road
  • LocationPoland

Just out of curiosity, I am guessing that et_ConsoleCommand() intercepts rcon commands and server console commands? Would this mean you would be able to also run the Lua script through rcon?

What do you mean exactly, et_ConsoleCommand() callback is looking for cmds via rcon only.

I was thinking something like this:

[command]
command = example
exec = mycommand param1 param2
desc = Invoking Lua from custom commands
levels = 0 1 2 3 4 5

And in the Lua script somthing like
function et_ConsoleCommand()
-- check if it is a command to intercept
-- if so, read parameters and execute the associated Lua functions
end

Yea, this is nice idea too.

#9
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction
Is there any way you could create a wiki section for the lua api? The documentation for the api is pretty poor, especially when trying to find out what the available fields are in the gentity struct. The best resource I found was looking at etpub's source code directly (and that isn't commented very well). I think it would be a huge benefit to the ET community to have better documentation for this and all in one place.

If you do create the section in the wiki, I'd be willing to help populate it with information and add examples as I learn this stuff if you would be willing to give me editing rights.

#10
OFFLINE   gaoesa

gaoesa

    Advanced Member

  • Management
  • PipPipPipPip
  • 4391 posts
341
Will become famous
  • LocationFinland
That is a good suggestion. The major problem with the current documentation is that it is describing the differences to the ETPro's documentation. http://wolfwiki.anim...php/Lua_Mod_API

#11
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction

The custom commands are an existing feature. http://mygamingtalk....ot#shrubbot.cfg


I know about the custom commands already... I was actually asking about the calling a lua function through the exec line like in your example. Is that functionality available currently?

That is precisely what I was thinking. I'm not sure what you mean by runing Lua scripts through rcon, but there is no rcon command to execute scripts. However, the script that is loaded on map start can be triggered to do things.


I meant if you run the same command as the exec line through rcon, it would have the exact same effect of running the script, correct? I was just wondering how et_ConsoleCommand() worked, since I haven't looked into it yet.

Edited by JvIasterMind, 22 October 2012 - 04:51 PM.


#12
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction

That is a good suggestion. The major problem with the current documentation is that it is describing the differences to the ETPro's documentation. http://wolfwiki.anim...php/Lua_Mod_API


Couldn't the documentation cover the etpro documentation as well as the changes? It would be a lot better if all of it is in one place instead of needing to check both sources independently.

#13
OFFLINE   Sol

Sol

    Regular Member

  • Coders
  • PipPipPip
  • 113 posts
28
On a distinguished road
  • LocationPoland

I know about the custom commands already... I was actually asking about the calling a lua function through the exec line like in your example. Is that functionality available currently?

something like this
[command]
command = ammo
exec = lua ammo
desc = bla bla bla
levels = 0 1 2 3 4 5
function et_ConsoleCommand( cmd ) -- <-- there is a cmd

if cmd == "lua" then -- commands here
  if et.trap_Argv( 1 ) == "ammo" then
   et.gentity_set( tonumber(et.trap_Argv( 2 )), "ps.ammo", tonumber(et.trap_Argv( 3 )) )
    -- it needs 2 extra parametrs or just use et_ClientCommand callback
  end
end
end


#14
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction

something like this

[command]
command = ammo
exec = lua ammo
desc = bla bla bla
levels = 0 1 2 3 4 5
function et_ConsoleCommand( cmd ) -- <-- there is a cmd

if cmd == "lua" then -- commands here
   if et.trap_Argv( 1 ) == "ammo" then
      et.gentity_set( tonumber(et.trap_Argv( 2 )), "ps.ammo", tonumber(et.trap_Argv( 3 )) )
      -- it needs 2 extra parametrs or just use et_ClientCommand callback
   end
end
end


Nice! So we can do that already? I'll experiment with it later today!

#15
OFFLINE   gaoesa

gaoesa

    Advanced Member

  • Management
  • PipPipPipPip
  • 4391 posts
341
Will become famous
  • LocationFinland
Parameter substitution should also work.

[command]
command = ammo
exec = lua ammo [1]
desc = bla bla bla
levels = 0 1 2 3 4 5

The benefit of this approach is that you can use the inbuilt admin system to control who has access to the commands and the commands also work in the exact same way as inbuilt shrubbot commands. I.e. no need to handle admin levels, console writes and chat writes in scripts. The downside is that there is some amount of replication and dependencies between shrubbot.cfg and Lua scripts.

Nice! So we can do that already? I'll experiment with it later today!


That should work. Let us know how it works out.

#16
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction
Quick question: In defining a command like this, how do you get the clientNum that executed the command. Is it just the next unused parameter? So if the exec line was "exec = lua ammo [1]", the clientNum would be in "et.trap_Argv(3)"?

#17
OFFLINE   gaoesa

gaoesa

    Advanced Member

  • Management
  • PipPipPipPip
  • 4391 posts
341
Will become famous
  • LocationFinland
et.trap_Argv(0) = the actual command, in this case "lua"
et.trap_Argv(1) = ammo
et.trap_Argv(2) = [1]

#18
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction
I understand that part, but can you also get the clientNum of the player that executed the command? Maybe my question is better served with a simple example. Lets say that you want all players with level 2, 4, and 5 to have a command called !sayname. Now, lets define !sayname as a command that does a global chat (from the player that executed the command) of the name of the player in a slot that was passed as a parameter.

Here is the code I would start with (following the examples in this thread)...
[command]
command = sayname
exec = sayname [1]
desc = bla bla bla
levels = 2 4 5
function et_ConsoleCommand(command)
   if et.trap_Argv(0) == "sayname" then
      if et.trap_Argc() == 2 then
         local targetSlotNum = et.trap_Argv(1)
         local targetName = et.gentity_get(targetSlotNum, "pers.netname")
         local clientNum = ??? -- How do I get this?
         et.G_Say(clientNum, et.SAY_ALL, targetName)
      else
         -- Error: Need two parameters
      end
      return 1
   end
   return 0
end


#19
OFFLINE   Sol

Sol

    Regular Member

  • Coders
  • PipPipPip
  • 113 posts
28
On a distinguished road
  • LocationPoland
Like I said, you have to use 2 parametrs or just get it from et_ClientCommand() callback.
for example:

clientnum = 0 -- global variable!!!
function et_ClientCommand( num, cmd )

-- for example, sayname is also simillar command ( !sayname )
if et.trap_Argv( 1 ) == "!sayname" then
clientnum = num
end
end
function et_ConsoleCommand(command)
if et.trap_Argv(0) == "sayname" then
		 if et.trap_Argc() == 2 then
				 local targetSlotNum = et.trap_Argv(1)
				 local targetName = et.gentity_get(targetSlotNum, "pers.netname")
				 local clientNum = clientnum
				 et.G_Say(clientNum, et.SAY_ALL, targetName)
		 else
				 -- Error: Need two parameters
		 end
		 return 1
end
return 0
end
but it works for !sayname command only, you can also check for last client who used command like:
function et_ClientCommand( num, cmd )
-- check for silent commands too
if string.find(cmd, "!") or string.find(et.trap_Argv( 1 ), "!") then
  clientnum = num
end
end

Edited by Sol, 22 October 2012 - 06:57 PM.


#20
OFFLINE   JvIasterMind

JvIasterMind

    Regular Member

  • Members
  • PipPipPip
  • 116 posts
8
A step in the right direction
Oh, so you do both the et_ClientCommand() and the et_ConsoleCommand() in the same script and only use the et_ClientCommand() function to set the clientNum? I suppose then you would want to return 0 in the et_ClientCommand() so it doesn't appear to be a valid command to the client? And in your example, shouldn't it be et.trap_Argv(0) instead of et.trap_Argv( 1 )?

So the full code would be...
[command]
command = sayname
exec = sayname [1]
desc = Say a player's name
levels = 2 4 5
clientNum = 0
function et_ClientCommand(num, cmd)
   if et.trap_Argv(0) == "sayname" then
	  clientNum = num
   end
   return 0
end
function et_ConsoleCommand(cmd)
   if et.trap_Argv(0) == "sayname" then
	  if et.trap_Argc() == 2 then
		 local targetSlotNum = et.trap_Argv(1)
		 local targetName = et.gentity_get(targetSlotNum, "pers.netname")
		 et.G_Say(clientNum, et.SAY_ALL, targetName)
	  else
		 et.trap_SendServerCommand(clientNum, 'print "Error: Need two parameters"')
	  end
	  return 1
   end
   return 0
end

Edited by JvIasterMind, 22 October 2012 - 07:11 PM.






Also tagged with one or more of these keywords: lua, api, scripting

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users