Jump to content

Recommended Posts

I've been having a spawning problem with my mod. What is meant to happen is when the player first loads up the game into their world, a follower will spawn which it does. However whenever the player enters a cave, it spawns another follower which is not what i want, and this is the same with when exiting the cave so if they entered a cave and then left... they would have 3 of the same follower.

To try and prevent this, i thought of the idea to create a variable called spawnAmount which will go up by one each time a follower is spawned and then if the spawnAmount goes above 1, it won't spawn another follower. This is how I've done it:

What calls the function for spawning the follower:

inst:DoTaskInTime(0, function(inst) SpawnTerrorbeakpet(inst) end)

The variable which is at the top of the script:

local spawnAmount = 0

and the follower spawning function:

local function SpawnTerrorbeakpet(inst)
if spawnAmount < 1 then
	spawnAmount = spawnAmount + 1
    local pos = Vector3(inst.Transform:GetWorldPosition())
    local offset = (FindWalkableOffset(pos,math.random()*PI*2,0.5,false))
    if offset == nil then
          if  inst.components.talker then
          inst.components.talker:Say("No Space!", 2.5)
          end
          return
    end
    pos=pos+offset
	
    local unit = SpawnPrefab("terrorbeakpet")
    unit.Transform:SetPosition (pos:Get())

inst.components.leader:AddFollower(unit)
unit.spawnedforplayer=inst
local brain = require("brains/nightmarepetbrain")
unit:SetBrain(brain)
end
end

That to me seems like it would work but it doesn't. It just keeps spawning new followers whenever i enter a cave or exit a cave. What I think is happening is it's changing "spawnAmount" back to zero for some reason but sadly I'm unsure.

Would anyone be able to help me with this?

3 minutes ago, Mobbstar said:

You should check for whether it's day 0 to that player. In singleplayer, this is really easy, since you can literally just check the date and daytime. Example: Tomes of Knowledge (book spawning around player)

What do you mean exactly?

14 minutes ago, codelyoko373 said:

What do you mean exactly?

That means you should check the world to see whether the player loads up the world the first time. Variables don't get saved (unless you use a mod-prefab with save and load functions).

If you don't feel like checking the clock on world load, you could check whether there is already a prefab of this kind.

Just now, Mobbstar said:

That means you should check the world to see whether the player loads up the world the first time. Variables don't get saved (unless you use a mod-prefab with save and load functions).

If you don't feel like checking the clock on world load, you could check whether there is already a prefab of this kind.

Ah i get it! That does seem like it would work though do you know how i would check the daytime within my script?

38 minutes ago, codelyoko373 said:

Ah i get it! That does seem like it would work though do you know how i would check the daytime within my script?

From "Tomes of Knowledge":

if GLOBAL.GetClock().numcycles == 0 //it's the first day
and GLOBAL.GetClock():GetNormTime() == 0 //it's exactly 0:00
and GLOBAL.GetWorld().topology.level_type == "survival" then //we're not first time going into a cave (warning: this breaks adventure mode) If the follower does not change level with the player, try removing this part

 

2 minutes ago, Mobbstar said:

From "Tomes of Knowledge":


if GLOBAL.GetClock().numcycles == 0 //it's the first day
and GLOBAL.GetClock():GetNormTime() == 0 //it's exactly 0:00
and GLOBAL.GetWorld().topology.level_type == "survival" then //we're not first time going into a cave (warning: this breaks adventure mode) If the follower does not change level with the player, try removing this part

 

Thanks :)

there also other ways :

1) add component to player, which will save followers counter, but you have to track their death

2) checking entities before spawning, if there already enough of them - do not spawn. But if one or more followers can stay in old world/cave and mission fails

look at cave entrance code, it's all about preventing spawning more chesters and handling meat effigy.

Edited by Rincevvind

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