Jump to content

Recommended Posts

make my character lose little sanity or regenerate sanity when morning

 

Do you want it to be either one or the other, or based on random chance?

Eitherway, here's the basic code to add to master_postinit:

inst:WatchWorldState("startday", function(inst)    inst.components.sanity:DoDelta(<sanity to add or substract>)end) 

About the shining when low on health, it's a bit more tricky.

Start by creating a new prefab, which will be the entity holding the light, that you'll attach to your character:

local function fn()    local inst = CreateEntity()    inst.entity:AddLight() -- adds light to the entity    inst.entity:AddNetwork()    inst:AddTag("FX")    inst.Light:Enable(true)    inst.Light:SetIntensity(.75)    inst.Light:SetColour(197 / 255, 197 / 255, 50 / 255) -- colors in RGB    inst.Light:SetFalloff(0.5) -- precentage of radius after which light will start fading    inst.Light:SetRadius(2)    if not TheWorld.ismastersim then        return inst    end    inst.entity:SetPristine()        inst.persists = false    return instendreturn Prefab("common/fx/characterlight", fn) 

Then, in your character's master_postinit, make your character listen to the healthdelta event:

inst:ListenForEvent("healthdelta", function(inst, data) -- this event is triggered everytime your characters loses/gains health    local threshold = <whatever you want>    if data.newpercent <= threshold and not inst.light then        inst.light = SpawnPrefab("characterlight") -- spawns light entity        local follower = inst.light.entity:AddFollower()        follower:FollowSymbol(inst.GUID, "swap_body", 0, 0, 0) -- makes it follow your character's center of body    elseif data.newpercent > threshold and inst.light then        inst.light:Remove() -- remove light entity if life is above threshold        inst.light = nil    endend)

Let me know if it works properly. :-)

This code is essentially adapted from how torch light is handled.

Edited by Jjmarco

Err...Where do I create this new prebaf ?

local function fn()    local inst = CreateEntity()    inst.entity:AddLight() -- adds light to the entity    inst.entity:AddNetwork()    inst:AddTag("FX")    inst.Light:Enable(true)    inst.Light:SetIntensity(.75)    inst.Light:SetColour(197 / 255, 197 / 255, 50 / 255) -- colors in RGB    inst.Light:SetFalloff(0.5) -- precentage of radius after which light will start fading    inst.Light:SetRadius(2)    if not TheWorld.ismastersim then        return inst    end    inst.entity:SetPristine()        inst.persists = false    return instendreturn Prefab("common/fx/characterlight", fn) 

Then, in your character's master_postinit, make your character listen to the healthdelta event:

inst:ListenForEvent("healthdelta", function(inst, data) -- this event is triggered everytime your characters loses/gains health    local threshold = <whatever you want>    if data.newpercent <= threshold and not inst.light then        inst.light = SpawnPrefab("characterlight") -- spawns light entity        local follower = inst.light.entity:AddFollower()        follower:FollowSymbol(inst.GUID, "swap_body", 0, 0, 0) -- makes it follow your character's center of body    elseif data.newpercent > threshold and inst.light then        inst.light:Remove() -- remove light entity if life is above threshold        inst.light = nil    endend)

 

@Icerzz, Hm, I see. You renamed the light entity's prefab pakelight, but you did not change that line:

return Prefab("common/fx/characterlight", fn) 

You also have to change characterlight to pakelight.

 

Also, why do you have 2's as parameters to these functions?  They shouldn't be there:

local inst = CreateEntity(2) inst.entity:AddLight(2)inst.entity:AddNetwork(2)

same for 51 as parameter to inst.light:Remove. Remove doesn't take any parameter.

local threshold = 50, not <50>, it should be a number! Sorry if "<whatever you want>" confused you. 

inst:ListenForEvent("healthdelta", function(inst, data)    local threshold = <50> -- this should be 50, not <50>    if data.newpercent <= threshold and not inst.light then        inst.light = SpawnPrefab("pakelight")        local follower = inst.light.entity:AddFollower()        follower:FollowSymbol(inst.GUID, "swap_body", 0, 0, 0)    elseif data.newpercent > threshold and inst.light then        inst.light:Remove(51) -- no 51 here!        inst.light = nil    endend)

.

One last thing, did you register pakelight in your modmain? Don't forget! :razz:

Prefabs = {    "pake",    "pakelight"}
Edited by Jjmarco

@Icerzz, Alright. You are putting numbers as parameters to functions that don't need them:

local inst = CreateEntity(1) -- no 1 here inst.entity:AddLight(1) -- and hereinst.entity:AddTransform(0.25) -- no 0.25 hereinst.entity:AddNetwork(1) -- no 1 here

There should never be numbers there.

 

What I meant by percentage as a threshold was here, in healthdelta:

local threshold = 0.25

That means that your character will shine when its life gets below 25% of max. You can change that to whatever other percentage you want, like 30% or 10%.

 

You can't put 50 because healthdelta gives you the percentage of health, not the number value. So if you put 50, it's way bigger than 1 (100%), so the light will always be lit.

Edited by Jjmarco

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