Jump to content

Custom command with LUA - problem


Aciz

Recommended Posts

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
Link to comment
Share on other sites

  • Management

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

  • Management

 

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Management

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
Link to comment
Share on other sites

 

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...