Jump to content

Event fired BEFORE entering caves ?


Recommended Posts

I'm looking for an event that is fired before entering caves, so that I can save the inventory of that player.

I already gathered:
ms_playerleft  -- is called too late, inventory is empty
playerexited  -- I think it is called after player exited, but not sure?
playerdeactivated  -- I think it is called after player exited, but not sure?
ms_playerdespawn -- is only called before leaving game, not called when leaving overwolrd to enter caves
ms_playerdisconnected  -- I think it is called after player exited, but not sure?

Link to comment
Share on other sites

local function CheckAndSave(inst)
	if inst.migration then
		-- player is migrating, do things
	end
end

local function OnRemoveCheckForMigrationData(inst)
	if GLOBAL.TheWorld.ismastersim then
		inst:ListenForEvent("onremove", CheckAndSave)
	end
end

AddPlayerPostInit(OnRemoveCheckForMigrationData)

 

Based on

local function PlayerRemove(player, deletesession, migrationdata, readytoremove)
    if readytoremove then
        player:OnDespawn()
        if deletesession then
            DeleteUserSession(player)
        else
            player.migration = migrationdata ~= nil and {
                worldid = TheShard:GetShardId(),
                portalid = migrationdata.portalid,
                sessionid = TheWorld.meta.session_identifier,
            } or nil
            SerializeUserSession(player)
        end
        player:Remove()
        if migrationdata ~= nil then
            TheShard:StartMigration(migrationdata.player.userid, migrationdata.worldid)
        end
    else
        player:DoTaskInTime(0, PlayerRemove, deletesession, migrationdata, true)
    end
end

of playerspawner.

Link to comment
Share on other sites

thanks... but it seems now the WorldJump does not work anymore... (entered the command in the dedicated server command line, while I was in caves)
after the world is new created, I can't join it automatically... found no error in logs, except maybe this in caves log:

Quote

[00:03:28]: Restarting player migration on master server
[00:03:28]: [Shard] Player KU_Eqx7VQ_x trying to migrate to an unknown world ([SHDMASTER](1))
[00:03:28]: CloseConnectionWithReason: ID_DST_USER_CONNECTION_FAILED

I did in modmain:
 

local function CheckAndSave(inst)
	if inst.migration and GLOBAL.TheWorld.components.worldjump then
        GLOBAL.TheWorld.components.worldjump:OnPlayerExitOverworld(inst)
	end
end

local function OnRemoveCheckForMigrationData(inst)
	if GLOBAL.TheWorld.ismastersim then
		inst:ListenForEvent("onremove", CheckAndSave)
	end
end

AddPlayerPostInit(OnRemoveCheckForMigrationData)

and in worldjump component:
 

function WorldJump:OnPlayerExitOverworld(player) 
    print("exitoverworld: "..tostring(player))
    if TheWorld.components.worldjump and player and player.migration then
        TheWorld.components.worldjump:SavePlayerData(player) -- save player data when leaving overworld (leaving game or entering caves)
    end
end

-- Save a persistent string with all the relevant player info
function WorldJump:SavePlayerData(pl)	-- self.player_data_save is saved and loaded in OnLoad OnSave
    local stuff = {}
    local age_data = nil
    local inventory_data, inventory_references = nil,nil
    local builder_data = nil
    local beard_data = nil
    if pl and pl:HasTag("player") then -- only save for one specific player, eg when he leaves
        age_data = pl.components.age:OnSave()
        inventory_data, inventory_references = pl.components.inventory:OnSave()
        stuff.age_data = age_data
        stuff.inventory_data = inventory_data
        stuff.inventory_references = inventory_references
        builder_data = pl.components.builder:OnSave()-- added by serp
        stuff.builder_data = builder_data
        beard_data = pl.components.beard and pl.components.beard:OnSave() or nil-- added by serp
        stuff.beard_data = beard_data
        self.player_data_save[pl.userid] = stuff -- save or overload the stuff of this player
    else -- in case of worldjump
        for k, v in pairs(AllPlayers) do -- all players that are online and in overworld
            age_data = v.components.age:OnSave()
            inventory_data, inventory_references = v.components.inventory:OnSave()
            stuff.age_data = age_data
            stuff.inventory_data = inventory_data
            stuff.inventory_references = inventory_references
            builder_data = v.components.builder:OnSave()-- added by serp
            stuff.builder_data = builder_data
            beard_data = v.components.beard and v.components.beard:OnSave() or nil-- added by serp
            stuff.beard_data = beard_data
            self.player_data_save[v.userid] = stuff
        end
        self.player_data_save.saveinventory = self.saveinventory
        self.player_data_save.savebuilder = self.savebuilder
        self.player_data_save.saveage = self.saveage
        local encoded_data = json.encode(self.player_data_save)
        TheSim:SetPersistentString(self.info_dir, encoded_data, true) -- only save it in string when the worldjump is done.
    end
end

Before I added the stuff in modmain, everything worked fine, so I'm sure my code in SavePlayerData is okay and works (When calling it on "despawn" event it already works to save data from players that go offline)

If you don't have an answer, I will test it more in some hours...

Edited by Serpens
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...