How do I accept commands with arguments?

Place to get help with not working mods / modding interface.
Post Reply
Zillo7
Inserter
Inserter
Posts: 22
Joined: Sun May 14, 2017 10:45 pm
Contact:

How do I accept commands with arguments?

Post by Zillo7 »

I'm trying to have my mod accept console commands with arguments. Here's what I'm using:

Code: Select all

commands.add_command("setTickRate","Sets how often the mod should scan for things. (default: 20)",
  function(list)
    global.tickRate = list[1]
	game.player.print(global.tickRate)
  end
)
The command is entered like:

Code: Select all

/setTickRate 50
However global.tickRate is set to nil. I thought the table named list would contain the arguments I passed, but it doesn't. How do I accept arguments with a console command?


Rseding91
Factorio Staff
Factorio Staff
Posts: 13175
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: How do I accept commands with arguments?

Post by Rseding91 »

Commands pass a table just like events.

The table contains the following parameters:

name - the command name
player_index - the player doing the command if any
parameter - the parameters provided as a string

In your case list.parameter would == "50".
If you want to get ahold of me I'm almost always on Discord.

Zillo7
Inserter
Inserter
Posts: 22
Joined: Sun May 14, 2017 10:45 pm
Contact:

Re: How do I accept commands with arguments?

Post by Zillo7 »

Rseding91 wrote:Commands pass a table just like events.

The table contains the following parameters:

name - the command name
player_index - the player doing the command if any
parameter - the parameters provided as a string

In your case list.parameter would == "50".
That worked perfectly, thanks!

Code: Select all

commands.add_command("setRate","Sets some value to something",
  function(arguments)
    global.rateThing = tonumber(arguments.parameter)
    game.player.print(global.rateThing )
  end
)

Post Reply

Return to “Modding help”