Jump to content

How to activate/wake a small bunch of off-screen entities [solved]


Recommended Posts

I want to make all plants (evergreens, grass, berrybushes and farmplants) near a non-player entity (ocuvigil) grow when no player is around. Is there any inst.entity:xxx() function to do so?

I have tried AddPrefabPostInit to set nearby entities not going sleep:

AddPrefabPostInit("sentryward", function(inst)
    if not GLOBAL.TheWorld.ismastersim then
		return inst
	end

    local function SetActive(inst)
        local x, y, z = inst.Transform:GetWorldPosition()
        local producerList = TheSim:FindEntities(x, y, z, 20, nil, nil, { "plant", "wx" })
        for k, v in pairs(producerList) do
            v.entity:SetCanSleep(false)
            if v.components.growable ~= nil then
                v.components.growable.growoffscreen = true
            end
        end
    end

    inst:DoTaskInTime(0, SetActive)
    if inst.task ~= nil then
        inst.task:Cancel()
        inst.task = nil
    end
    inst.task = inst:DoPeriodicTask(60, SetActive)
end)

But it seems not working. I guess it's because the ocuvigil tried to set plants entities not sleeping after they were loaded.

And I don't know if AddPrefabPostInitAny could work.

AddPrefabPostInitAny(function(inst)
	local KEEP_WORKING_DIST = 20
	local nearby_sentryward = FindEntity(inst, KEEP_WORKING_DIST, function(ent) return ent.prefab == "sentryward" end)
	if inst:HasTag("plants") and nearby_sentryward ~= nil then
		inst.entity:SetCanSleep(false)
	end
end)

 

Or is there any other way to do so? Like periodically update the state of these plants entities? Thanks.

Edited by XueYanYu
Link to comment
Share on other sites

AddPrefabPostInit("sentryward", function(inst)
    if not GLOBAL.TheWorld.ismastersim then
		return inst
	end

    local function GrowPlants(inst)
        local x, y, z = inst.Transform:GetWorldPosition()
        local producerList = TheSim:FindEntities(x, y, z, 20, nil, nil, { "plant" })
        for k, v in pairs(producerList) do
            if v.components.growable ~= nil then
                v.components.growable:OnEntityWake()
            end
        end
    end

    if inst.task ~= nil then
        inst.task:Cancel()
        inst.task = nil
    end
    inst.task = inst:DoPeriodicTask(60, GrowPlants, 0)
end)

It seems that inst.entity:SetCanSleep(false) is not necessary.

A periodic task of inst.components.growable:OnEntityWake() can make plants update its growing stages.

(However, I still don't know how to wake mobs.)

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