on_tick in multiplayer

Place to get help with not working mods / modding interface.
User avatar
withers
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Apr 08, 2016 4:54 pm
Contact:

on_tick in multiplayer

Post by withers »

How does this work? As an example I have a script running at regular intervals. If it's a multiplayer game with 4 players, would that mean this script runs 4 times every interval? Or just once?
Rseding91
Factorio Staff
Factorio Staff
Posts: 16442
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_tick in multiplayer

Post by Rseding91 »

From a mod perspective just pretend that multiplayer doesn't exist. All you care about as a mod developer is handling events and reactions to those events. If you want to do something you either do it to a specific player or all players you simply do that.

A basic example: you want to send a message to "the player" when they build something:

In the event handler the handler gives you the player_index of the player that did the building so you would then do: game.players[event.player_index] and you have the player that did the building. You don't care how many players there are - you're just reacting to events.

Now that event works for single player and multiplayer. In multiplayer the player_index will just be what ever player is doing the action at the given time.
If you want to get ahold of me I'm almost always on Discord.
User avatar
withers
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Apr 08, 2016 4:54 pm
Contact:

Re: on_tick in multiplayer

Post by withers »

Okay thanks for that helpful information but my specific question involves doing something to a custom global table. (not any particular player) I am doing things with this table every 6 seconds.

The table keeps track of the age of every entity in the game of a specific type among other things. I'd like to know if, with 4 players for example, the table is getting updated 4 times per 6 seconds and not just once per 6 seconds as intended. If so, I need to adjust my on_tick function accordingly.

Mod is here.

viewtopic.php?f=94&t=41892


Thanks for the help! :)
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5431
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: on_tick in multiplayer

Post by Klonan »

withers wrote:Okay thanks for that helpful information but my specific question involves doing something to a custom global table. (not any particular player) I am doing things with this table every 6 seconds.

The table keeps track of the age of every entity in the game of a specific type among other things. I'd like to know if, with 4 players for example, the table is getting updated 4 times per 6 seconds and not just once per 6 seconds as intended. If so, I need to adjust my on_tick function accordingly.

Mod is here.

viewtopic.php?f=94&t=41892


Thanks for the help! :)
The tick event will only ever fire once per tick, so in this case your table will be updated properly, and not once per player per interval
Post Reply

Return to “Modding help”