Page 1 of 1

player.character == nill because of sandbox intro

Posted: Sat Apr 03, 2021 3:55 pm
by palmic
Hello,

i am trying to make my mod working on current factorio version, but it seams there is missing player.character because of sandbox intro (exploding ship).

In my control.lua i have:

Code: Select all

local function on_player_joined_game(event)
    local player = game.players[event.player_index]
    if player.character == nil then
        game.print("player.character nil")
    end
    player.character.character_running_speed_modifier = -0.4
end
script.on_event(defines.events.on_player_joined_game, on_player_joined_game)
which throws this error:
attempt to index field 'character' (a nil value)
When i changed it to run on every tick:

Code: Select all

local function on_tick(event)
    for i, player in pairs(game.players) do
        local player = game.players[i]
        if player.character == nil then return end
        game.print("ok")
        player.character.character_running_speed_modifier = -0.4
    end
end
script.on_event(defines.events.on_tick, on_tick)
I can clearly see by printing "ok", player.character is present after intro no matter if i skip it or leave it to finish.
player.create_character() also doesn't work before intro finish.


So my question is: Is there some event on_character_creation?
I haven't found it.

I simply dont want to run my functions on every tick.

Thank you!
Michal.

Re: player.character == nill because of sandbox intro

Posted: Sat Apr 03, 2021 4:03 pm
by Xorimuth
You can use https://lua-api.factorio.com/latest/eve ... _cancelled (which is run whether it is skipped or finishes naturally... its a bad name!)

Re: player.character == nill because of sandbox intro

Posted: Sat Apr 03, 2021 4:29 pm
by palmic
It works, thanks! ;)