Jump to content

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

Custom command with LUA - problem

- - - - -

  • Please log in to reply
14 replies to this topic

#1
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

I'm trying to create funsounds with shrubbot-like commands for my server with LUA. Currently, I have this code and it's working:

    function et_ClientCommand(clientNum, command)
            local arg0 = string.lower(et.trap_Argv(0))
            local arg1 = string.lower(et.trap_Argv(1))
            local level = et.G_shrubbot_level(clientNum)
			local flood = et.ClientIsFlooding(clientNum)

if (level >= 1) and (flood == 0) then
                    if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
                            (arg1 == "!example") then
                                    et.G_globalSound("path/to/example.wav");

However there is one issue: if you type in anything after you do !example, the command won't work. So I tried this:

    function et_ClientCommand(clientNum, command)
            local arg0 = string.lower(et.trap_Argv(0))
            local arg1 = string.lower(et.trap_Argv(1))
            local level = et.G_shrubbot_level(clientNum)
			local flood = et.ClientIsFlooding(clientNum) 

           if (level >= 1) and (flood == 0) then
                    if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
                            if (string.match(arg1, "!example%s?")) then
                                    et.G_globalSound("path/to/example.wav");

 

Now, if I didn't completely misunderstand string.match patterns, %s? should mean that there can be space after string, but it's optional. However, ET reads it so that there can be ANY character, but it's optional. So with this code, you can do !exampleblabla, and the command still works. Also, you don't have to type it as the first thing, it can be in the middle of typing aswell (e.g. blabla !example)

 

So here's how I would like the script to work:

 

 

1. !example has to be the first thing you write.

2. You can type anything after it, as long as it's separated with space. So basically:

!example - works

!example<space> - works

!example blabla - works

!exampleblabla - doesn't work

blabla !example - doesn't work


Edited by Aciz, 19 September 2014 - 02:25 PM.


#2
OFFLINE   gaoesa

gaoesa

    Advanced Member

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

I haven't checekd, but I think your issue in this case is that the text in the say command is coming as quoted. That makes it treated as one argument i.e. trap_Argv behaves similar to ConcatArgs. So you should probably split the arg1 and use the first word from it.



#3
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

I haven't checekd, but I think your issue in this case is that the text in the say command is coming as quoted. That makes it treated as one argument i.e. trap_Argv behaves similar to ConcatArgs. So you should probably split the arg1 and use the first word from it.

 

Removing the quotes makes the whole LUA stop working.



#4
OFFLINE   gaoesa

gaoesa

    Advanced Member

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

No I meant that the client is sending the say text inside quotes. I.e. /say "text with spaces". That makes the server interpret the full string as one parameter. So you should split the parameter using space as a delimiter and so you get what you need.



#5
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

No I meant that the client is sending the say text inside quotes. I.e. /say "text with spaces". That makes the server interpret the full string as one parameter. So you should split the parameter using space as a delimiter and so you get what you need.

 

Hmm, still don't know if I fully understand what you mean (excuse my noobish skills with LUA ^^). Could you maybe give me example?



#6
OFFLINE   Sol

Sol

    Regular Member

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

Just try to use string.find in your case that would work

string.find(arg1, "!example") -- if there is a !example command inside then function will always return value different than nil
-- string.match() is a kinda different function, it's designed for patterns especially, in your case it should be "(!example.*)" but this function will always return string


#7
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

 

Just try to use string.find in your case that would work

string.find(arg1, "!example") -- if there is a !example command inside then function will always return value different than nil
-- string.match() is a kinda different function, it's designed for patterns especially, in your case it should be "(!example.*)" but this function will always return string

 

I don't see how string.find helps me here... I get exactly same results as I would with string.match :S 



#8
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

I don't see how string.find helps me here... I get exactly same results as I would with string.match :S 

 

Maybe I'm blind but I cant find edit button...

 

 

Anyway Sol, could you give me a working example with string.find?

 

 

Btw if it does matter, the way I originally made sounds 

(arg1 == "!example") then
                                    et.G_globalSound("path/to/example.wav");

if I type the command in console, it will work just they way I want it (e.g. I can type !example blabla and it plays sound, but cant do !exampleblabla or blabla!example).



#9
OFFLINE   gaoesa

gaoesa

    Advanced Member

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

I added an example to http://mygamingtalk....Custom_Commands You can use that to check issues. Btw, thanks for giving some code to start adding to it.



#10
OFFLINE   Sol

Sol

    Regular Member

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

I don't see how string.find helps me here... I get exactly same results as I would with string.match :S 

you have not, string.match() is returning substring instead of position thats the problem.

if level >= 1 and flood == 0 then
	if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
		if string.find(arg1, "!example") == 1 then	-- if string is looking like that "!examplebabababala" then it will return anyway position, just in case it check for position '1' to avoid situation like that "bababa!examplehaha" because string.find will return position inside that string of a match
			et.G_globalSound("path/to/example.wav")
		end
	end
end


#11
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

 

you have not, string.match() is returning substring instead of position thats the problem.

if level >= 1 and flood == 0 then
	if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
		if string.find(arg1, "!example") == 1 then	-- if string is looking like that "!examplebabababala" then it will return anyway position, just in case it check for position '1' to avoid situation like that "bababa!examplehaha" because string.find will return position inside that string of a match
			et.G_globalSound("path/to/example.wav")
		end
	end
end

 

Thank you, I got one step closer! Now it won't work anymore if I type blabla!example. However it still works with !exampleblabla, how could I work that out?



#12
OFFLINE   gaoesa

gaoesa

    Advanced Member

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

 

Thank you, I got one step closer! Now it won't work anymore if I type blabla!example. However it still works with !exampleblabla, how could I work that out?

Can you tell me what is wrong with using the split function in the Wiki example?



#13
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

Can you tell me what is wrong with using the split function in the Wiki example?

 

Nothing, it's actually working perfect for me :) just tried to get it to work with string.find because it would be less complex to edit the current LUA to work like that :) Thank you!

 

Edit: actually looking more to it, how would I go on doing different commands for different levels in this LUA? Since the commands are defined in CheckCommand while admin level is in et_ClientCommand.


Edited by Aciz, 22 September 2014 - 09:58 PM.


#14
OFFLINE   gaoesa

gaoesa

    Advanced Member

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

You could add the admin level to the passed arguments to the function:

function CheckCommand(clientNum, level, arguments)
    if( arguments[0] == "!mycommand" ) then
	et.trap_SendServerCommand(clientNum, "chat \"Your command "..arguments[0].." has been received\"")
	return 1
    end

    return 0
end

function et_ClientCommand(clientNum, command)
    local level = et.G_shrubbot_level(clientNum)
    local flood = et.ClientIsFlooding(clientNum)

    -- we're not interested if the level is too low or if the client is flooding
    if (level < 1) or (flood == 1) then
        return 0
    end
	
    local arg0 = string.lower(et.trap_Argv(0))
    local arguments

    -- check to see if the command was given through any of the chat methods or as sparse text from the console
    if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
	arguments = SplitToArguments(et.trap_Argv(1))
    else
	arguments = SplitToArguments(et.ConcatArgs(0))
    end

    -- actual command handling
    return CheckCommand(clientNum, level, arguments)
end

 Or you can declare the variable as close to its intended use as possible. This style is generally preferred when programming:

function CheckCommand(clientNum, arguments)
    local level = et.G_shrubbot_level(clientNum)
    if( (arguments[0] == "!mycommand") and (level >= 1) ) then
	et.trap_SendServerCommand(clientNum, "chat \"Your command "..arguments[0].." has been received\"")
	return 1
    end

    return 0
end

function et_ClientCommand(clientNum, command)
    local flood = et.ClientIsFlooding(clientNum)

    -- we're not interested if the client is flooding
    if flood == 1 then
        return 0
    end
	
    local arg0 = string.lower(et.trap_Argv(0))
    local arguments

    -- check to see if the command was given through any of the chat methods or as sparse text from the console
    if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
	arguments = SplitToArguments(et.trap_Argv(1))
    else
	arguments = SplitToArguments(et.ConcatArgs(0))
    end

    -- actual command handling
    return CheckCommand(clientNum, arguments)
end


#15
OFFLINE   Aciz

Aciz

    Member

  • Members
  • PipPip
  • 38 posts
7
A step in the right direction

 

You could add the admin level to the passed arguments to the function:

function CheckCommand(clientNum, level, arguments)
    if( arguments[0] == "!mycommand" ) then
	et.trap_SendServerCommand(clientNum, "chat \"Your command "..arguments[0].." has been received\"")
	return 1
    end

    return 0
end

function et_ClientCommand(clientNum, command)
    local level = et.G_shrubbot_level(clientNum)
    local flood = et.ClientIsFlooding(clientNum)

    -- we're not interested if the level is too low or if the client is flooding
    if (level < 1) or (flood == 1) then
        return 0
    end
	
    local arg0 = string.lower(et.trap_Argv(0))
    local arguments

    -- check to see if the command was given through any of the chat methods or as sparse text from the console
    if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
	arguments = SplitToArguments(et.trap_Argv(1))
    else
	arguments = SplitToArguments(et.ConcatArgs(0))
    end

    -- actual command handling
    return CheckCommand(clientNum, level, arguments)
end

 Or you can declare the variable as close to its intended use as possible. This style is generally preferred when programming:

function CheckCommand(clientNum, arguments)
    local level = et.G_shrubbot_level(clientNum)
    if( (arguments[0] == "!mycommand") and (level >= 1) ) then
	et.trap_SendServerCommand(clientNum, "chat \"Your command "..arguments[0].." has been received\"")
	return 1
    end

    return 0
end

function et_ClientCommand(clientNum, command)
    local flood = et.ClientIsFlooding(clientNum)

    -- we're not interested if the client is flooding
    if flood == 1 then
        return 0
    end
	
    local arg0 = string.lower(et.trap_Argv(0))
    local arguments

    -- check to see if the command was given through any of the chat methods or as sparse text from the console
    if arg0 == "say" or arg0 == "say_team" or arg0 == "say_buddy" or arg0 == "say_teamnl" then
	arguments = SplitToArguments(et.trap_Argv(1))
    else
	arguments = SplitToArguments(et.ConcatArgs(0))
    end

    -- actual command handling
    return CheckCommand(clientNum, arguments)
end

Thanks again for your help and patience with my very limited LUA skills  :P the 2nd option seems like the way to go for me  :)


Edited by Aciz, 22 September 2014 - 10:25 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users