How can I get the "index" of the current character in "script. On_init"
Posted: Mon May 23, 2022 1:13 am
This is included in multiplayer games. How can I get it? thank you
www.factorio.com
https://forums.factorio.com/
Code: Select all
for index, player in pairs(game.players) do
local character = player.character
if character ~= nil then
-- Do stuff with character
end
end
Sorry, I don't quite understand. I just want to find my own "index". How should I write it?robot256 wrote: ↑Mon May 23, 2022 1:25 amMod scripts run at the game level, and execute the same code on every player's machine. They can access every player's data all the time.
To find every player's characters, use:
Code: Select all
for index, player in pairs(game.players) do local character = player.character if character ~= nil then -- Do stuff with character end end
Thank you. I seem to suddenly understand that I just want to name some variables related to index, such as global Mods [player_index], I think it would be more appropriate for me to add it to "on_player_created". Thank you, I finally understandrobot256 wrote: ↑Mon May 23, 2022 3:29 amThe mod doesn't know whose computer it is running on--it runs on everyone's simultaneously. There is no "me", there is only player 1, player 2, etc. The mod can receive events every time any player does something like open a GUI or press a key, with a link to the player that did it. There are also "runtime per player" mod settings, which the player changes in the mod settings window and the mod can read for each player.
Typically, mods that need to do something different for a particular player keep a table in global with the unique data for each player. This includes settings, handles to custom GUIs, etc. Then whenever a particular player triggers an action, the mod can decide what to do, where to pick up where it left off for that player, etc.
What do you need to do once you find the character you want?
You will need to have a control of some sort to tell your mod that "this player" wants something done. That is how it will know which player is "me". You will have to write it so that it could be any player, or all of them at once.