player.character == nill because of sandbox intro

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
palmic
Inserter
Inserter
Posts: 22
Joined: Fri Apr 21, 2017 2:44 pm
Contact:

player.character == nill because of sandbox intro

Post 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.

Xorimuth
Filter Inserter
Filter Inserter
Posts: 623
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

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

Post 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!)
My mods
Content: Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Remote Configuration | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings

palmic
Inserter
Inserter
Posts: 22
Joined: Fri Apr 21, 2017 2:44 pm
Contact:

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

Post by palmic »

It works, thanks! ;)

Post Reply

Return to “Modding discussion”