Jump to content

Recommended Posts

6 hours ago, lakhnish said:

Just of the top of my head, you can probably make a conditional that checks the player's age to give them your extra-extra items. 

Thanks for replying!

So I checked the age, but it seems the age is always 0 after using the lunar portal. But when I used the same function right after the spawn, the age is normal (several days old).

Code below

Spoiler
local OnPlayerSpawn = function(src, player)
    print(player.prev_OnNewSpawn)
    print(player.OnNewSpawn)
    player.prev_OnNewSpawn = player.OnNewSpawn
    player.OnNewSpawn = function()
        local start_items = { }
		
        print(player.components.age:GetAge())
        if player.components.age:GetAge() <= 1 then
      
            -- new player get new player resources
            insert(start_items, "cutgrass", 40)
        else			
            -- old players gets advanced stuff
            if count("greenstaff") < 1 then
                insert(start_items, "greenstaff", 1)
            end
        end
		
        -- add items to player
        for _,v in pairs(start_items) do
            local item = GLOBAL.SpawnPrefab(v) 
            player.components.inventory:GiveItem(item)
        end   
		   
        if player.prev_OnNewSpawn ~= nil then
            player:prev_OnNewSpawn()
            player.prev_OnNewSpawn = nil
        end
    end
end


local function ListenForPlayers(inst)
    if GLOBAL.TheWorld.ismastersim then
        inst:ListenForEvent("ms_playerspawn", OnPlayerSpawn)
    end
end

AddPrefabPostInit("world", ListenForPlayers)

Because I know very little about Lua or modding. what is .prev_OnNewSpawn? Are both .prev_OnNewSpawn and .OnNewSpawn instance methods of player?

Edited by goatt
4 hours ago, goatt said:

Because I know very little about Lua or modding. what is .prev_OnNewSpawn? Are both .prev_OnNewSpawn and .OnNewSpawn instance methods of player?

Unfortunately, I've got no clue. As for it going back to zero, maybe try something with adding tags to players and checking for that tag, though I'm not sure if even that would work.

2 hours ago, lakhnish said:

Unfortunately, I've got no clue. As for it going back to zero, maybe try something with adding tags to players and checking for that tag, though I'm not sure if even that would work.

Idk much about tags or anything. But thank you for your help!

On 5/6/2023 at 4:13 AM, goatt said:

I want to make a mod that gives new players extra extra starting items. But I don't want to give them to those who just want to switch characters through lunar portal.

How can I accomplish that?

Thanks!

Look at the data/scripts/prefabs/player_common.lua file's OnNewSpawn function.  It uses the following check:

if TheWorld.components.playerspawner ~= nil and TheWorld.components.playerspawner:IsPlayersInitialSpawn(inst) then

Followed up by sending an event:

TheWorld:PushEvent("ms_newplayerspawned", inst)

You should be able to listen for this event and do the same check as above in your callback to do what you want.  You can self-validate the call flow by looking at what calls these, and end up in data/scripts/networking.lua where the function SpawnNewPlayerOnServerFromSim calls player:OnNewSpawn before playerspawner:SpawnAtNextLocation (which marks IsPlayersInitialSpawn to be false).

  • Sanity 1

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
×
  • Create New...