Page 1 of 1

args of func

PostPosted: Tue Nov 04, 2014 12:25
by Schmeldric
Hello,

I'm new to lua and to minetest, so I might be saying wrongs things.

I've had a look at the API, and at the register chat command page and tried out this example :
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_chatcommand("say", {
   params = "<text>",
   description = "Send text to chat",
   func = function(text)
      minetest.chat_send_all(text)
      return true, "Text was sent successfully"
   end,
})


When used, it prints "single player" on the chat. After looking a few mods, I tried this charge (parameters of the func) :
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_chatcommand("say", {
   params = "<text>",
   description = "Send text to chat",
   func = function(name, text)
      minetest.chat_send_all(text)
      return true, "Text was sent successfully"
   end,
})

Which seems more appropriate.

However, I'm a bit lost on he syntax. Are these assertions true?
- The string in "params" is useless for the computer -- it's just the /help message
- the function given to "func" will always have the same parameters : name, and a string with the end of the command line;
- if line above is correct, is this a generality for all "func" in every method, or is there a documentation somewhere?

By the way, if params contains multiple arguments (such as x y z) how should I parse the string to get them? (I haven't reach this page in my lua tutorial)

Thanks for reading

Re: args of func

PostPosted: Tue Nov 04, 2014 14:07
by srifqi
Schmeldric wrote:Hello,

Hi,
Schmeldric wrote:I'm new to lua and to minetest, so I might be saying wrongs things.

It's okay. Many users have same problem when new to Minetest.
Schmeldric wrote:I've had a look at the API, and at the register chat command page and tried out this example :
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_chatcommand("say", {
   params = "<text>",
   description = "Send text to chat",
   func = function(text)
      minetest.chat_send_all(text)
      return true, "Text was sent successfully"
   end,
})


When used, it prints "single player" on the chat. After looking a few mods, I tried this charge (parameters of the func) :
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_chatcommand("say", {
   params = "<text>",
   description = "Send text to chat",
   func = function(name, text)
      minetest.chat_send_all(text)
      return true, "Text was sent successfully"
   end,
})

Which seems more appropriate.

It works!
Schmeldric wrote:However, I'm a bit lost on the syntax. Are these assertions true?
- The string in "params" is useless for the computer -- it's just the /help message
- the function given to "func" will always have the same parameters : name, and a string with the end of the command line;
- if line above is correct, is this a generality for all "func" in every method, or is there a documentation somewhere?

- But it's useful for player.
- True.
- Not it is not same for all method. I don't know any documentation. But maybe you can find it somewhere in Minetest Developer Wiki.
Schmeldric wrote:By the way, if params contains multiple arguments (such as x y z) how should I parse the string to get them? (I haven't reach this page in my lua tutorial)

You may use Regular Expression (RegExp). (Remeber: Lua doesn't use POSIX Regexp!)
Example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
x, y, z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")

See teleport chat command (/builtin/game/chatcommands.lua at line 261). It has advanced usage example for "x y z".
Schmeldric wrote:Thanks for reading

You're welcome!

Re: args of func

PostPosted: Tue Nov 04, 2014 22:50
by Schmeldric
Oh! There are intersting stuff in this /builtin/game/ ! A lot to read/understand/copy/modify.

Thank you.