Jump to content

Recommended Posts

Hi, Lawrence here.

 

I'm just testing a thing:  I want to change item colors (and maybe assets) if my char sanity goes below (number)

 

here's what I made:

 

 

function Visions( inst )
    
    inst:DoTaskInTime(0, function(inst)
        if GetPlayer().prefab == "NAME" then
          if GetPlayer().components.sanity.current < 100 then
          print ("TEST < 100")
          inst:AddComponent("visions1")
        else
          print ("TEST > 100")
          inst:RemoveComponent("visions1")
          end
        end
    end)
end

    AddPrefabPostInit("berrybush", Visions)
    AddPrefabPostInit("grass", Visions)

 

(the component "visions1" changes the prefab color, I tested it and it works)

 

When I call the console ingame, it shows "TEST < 100" so I guess the check-sanity-thing is working,  but it seems the function stops after adding the prefab, so if I change the sanity, nothing happens.

 

Any help?

Thanks.

Hi, Lawrence here.

 

I'm just testing a thing:  I want to change item colors (and maybe assets) if my char sanity goes below (number)

 

here's what I made:

 

 

function Visions( inst )

    

    inst:DoTaskInTime(0, function(inst)

        if GetPlayer().prefab == "NAME" then

          if GetPlayer().components.sanity.current < 100 then

          print ("TEST < 100")

          inst:AddComponent("visions1")

        else

          print ("TEST > 100")

          inst:RemoveComponent("visions1")

          end

        end

    end)

end

    AddPrefabPostInit("berrybush", Visions)

    AddPrefabPostInit("grass", Visions)

 

(the component "visions1" changes the prefab color, I tested it and it works)

 

When I call the console ingame, it shows "TEST < 100" so I guess the check-sanity-thing is working,  but it seems the function stops after adding the prefab, so if I change the sanity, nothing happens.

 

Any help?

Thanks.

It's because you're doing DoTaskIntTime(), as opposed to DoPeriodicTask(). One runs a function within an allocated time, the other runs a function at set intervals. However, with that many bushes and grass tufts, it may cause slowdown.

Thanks man, but yes it slows down.

 

UFFF...

 

Is there a similar way to do this without that problem?

Maybe changing only the visible prefabs, is that possible?

With doing AddPrefabPostInit(), it will always do it do every instance of that prefab . However, if you're adding a custom character, you can do DoPeriodicTask() on your character (it won't cause slowdown, since its only one instance), and then do FindClosestEntityWithTag("vision") and then add a "vision" tag to grass and berrybushes through AddPrefabPostInit(). Then you could do the color changes there in the character prefab.

 

Edit: Well, actually, that wouldn't do it. You would have to do something like this within DoPeriodicTask():

        local x,y,z = GetPlayer().Transform:GetWorldPosition()        local range = 50        local ents = TheSim:FindEntities(x,y,z, range, {"vision"})        for k,v in pairs(ents) do            if v:HasTag("vision") then                v:AddComponent("visions1")            end        end
Edited by debugman18

With doing AddPrefabPostInit(), it will always do it do every instance of that prefab . However, if you're adding a custom character, you can do DoPeriodicTask() on your character (it won't cause slowdown, since its only one instance), and then do FindClosestEntityWithTag("vision") and then add a "vision" tag to grass and berrybushes through AddPrefabPostInit(). Then you could do the color changes there in the character prefab.

But this will only take into account the closest enitity, and as far as I understand Lawrence wants all entities within a certain radius, or onscreen to be affected.

So my suggestion would be to add an eventcallback for "sanitydelta" and depending on data.newpercent add a component (or only a task) that checks all prefabs in a certain radius from the player. Let me have a few seconds and I might come up with the code.

 

EDIT: Damnit, now I got edit-ninja'd by debugman xD

But i still want to post my code, it looks so pwetty

local radius = 30local sanitythresh = 0.5local r = 0.4local g = 0.9local b = 0.1local function DeColourizePrefab(inst)	inst.AnimState:SetMultColour(1, 1, 1)	inst:RemoveTag("recoloured")	inst:RemoveEventCallBack("sanitydelta", DeColourizePrefab, GetPlayer())endlocal function ColourizePrefabs(inst)	local x, y, z = inst:GetPosition():Get()	local ents = TheSim:FindEntities(x, y, z, radius)	for _, ent in pairs(ents) do		if not ent:HasTag("recoloured") and ent ~= inst then			ent.AnimState:SetMultColour(r, g, b)			ent:AddTag("recoloured")			ent:ListenForEvent("sanitydelta", DeColourizePrefab, GetPlayer())		end	endendinst:ListenForEvent("sanitydelta", function()	if data.newpercent <= sanitythresh and not inst.task then		inst.task = inst:DoPeriodicTask(1, ColourizePrefabs)	elseif inst.task then		inst.task:Cancel()		inst.task = nil	endend)

Edited by Malacath

But this will only take into account the closest enitity, and as far as I understand Lawrence wants all entities within a certain radius, or onscreen to be affected.

So my suggestion would be to add an eventcallback for "sanitydelta" and depending on data.newpercent add a component (or only a task) that checks all prefabs in a certain radius from the player. Let me have a few seconds and I might come up with the code.

I edited my post above already after seeing that mistake, just before you posted. :razz:

 

Edit: Also, the HasTag() check is redundant. I haven't tested it, but it looks like its done properly. (I'm working on Up & Away right now, specifically the weather machine and the random weather outcomes.)

Edited by debugman18

I edited my post above already after seeing that mistake, just before you posted. :razz:

 

Edit: Also, the HasTag() check is redundant. I haven't tested it, but it looks like its done properly. (I'm working on Up & Away right now, specifically the weather machine and the random weather outcomes.)

Damn you, you're to fast for me  ^^

 

EDIT: Why is the HasTag redundant? It will spit out a "Component visions1 already added" error which is really annoying...

Edited by Malacath

Damn you, you're to fast for me  ^^

 

EDIT: Why is the HasTag redundant? It will spit out a "Component visions1 already added" error which is really annoying...

Because of FindEntity(). It finds entities with a given tag, which is this case is "vision". Since it's already finding entities with "vision", and only those entities, checking the HasTag("vision") is searching for entities with the tag "vision" among entities with the tag "vision".

 

It may be good as a failsafe though, in case the instance is removed before it can add the component.

Because of FindEntity(). It finds entities with a given tag, which is this case is "vision". Since it's already finding entities with "vision", and only those entities, checking the HasTag("vision") is searching for entities with the tag "vision" among entities with the tag "vision".

 

It may be good as a failsafe though, in case the instance is removed before it can add the component.

Ohhhhhhh.... I should really open my eyes from time to time...

 

Aaanyways, tell us if you get it working Lawrence!

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