Lawrence Posted November 20, 2013 Share Posted November 20, 2013 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. Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/ Share on other sites More sharing options...
debugman18 Posted November 20, 2013 Share Posted November 20, 2013 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. Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374521 Share on other sites More sharing options...
Lawrence Posted November 20, 2013 Author Share Posted November 20, 2013 (edited) 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? Edited November 20, 2013 by Lawrence Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374531 Share on other sites More sharing options...
debugman18 Posted November 20, 2013 Share Posted November 20, 2013 (edited) 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 November 20, 2013 by debugman18 Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374550 Share on other sites More sharing options...
Malacath Posted November 20, 2013 Share Posted November 20, 2013 (edited) 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 xDBut i still want to post my code, it looks so pwettylocal 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 November 20, 2013 by Malacath Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374561 Share on other sites More sharing options...
debugman18 Posted November 20, 2013 Share Posted November 20, 2013 (edited) 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. 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 November 20, 2013 by debugman18 Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374564 Share on other sites More sharing options...
Malacath Posted November 20, 2013 Share Posted November 20, 2013 (edited) I edited my post above already after seeing that mistake, just before you posted. 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 November 20, 2013 by Malacath Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374569 Share on other sites More sharing options...
debugman18 Posted November 20, 2013 Share Posted November 20, 2013 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. Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374573 Share on other sites More sharing options...
Malacath Posted November 20, 2013 Share Posted November 20, 2013 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! Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374576 Share on other sites More sharing options...
Lawrence Posted November 20, 2013 Author Share Posted November 20, 2013 Thank you a lot guys, but I can check only tomorrow... I need to go at work now ç_ç See ya tomorrow, thanks again Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374584 Share on other sites More sharing options...
Lawrence Posted November 21, 2013 Author Share Posted November 21, 2013 Thank you guys, I actually made a "mix" of the two codes you gave me and it works Link to comment https://forums.kleientertainment.com/forums/topic/29861-a-question-about-addprefabpostinit/#findComment-374957 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now