Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

jimmosio

Conditional night vision and increase crafted armor HP

Recommended Posts

jimmosio    0
local fn = function(inst)		-- choose which sounds this character will play	inst.soundsname = "s1mon"	-- Minimap icon	inst.MiniMapEntity:SetIcon( "s1mon.tex" )		-- Stats		inst.components.health:SetMaxHealth(165)	inst.components.hunger:SetMax(130)	inst.components.hunger:SetRate(0.9*TUNING.WILSON_HUNGER_RATE)	inst.components.sanity:SetMax(140)	local lightradius=3	local lightfalloff=.7	local lightintensity=.5        local currenthunger=inst.components.hunger.current	inst:ListenForEvent( "nighttime", function()	if currenthunger>=104 then		inst.entity:AddLight()	inst.Light:Enable(true)	inst.Light:SetRadius(lightradius)	inst.Light:SetFalloff(lightfalloff)	inst.Light:SetIntensity(lightintensity)	inst.Light:SetColour(65/65,160/160,40/40)	else	inst.entity:AddLight()	inst.Light:Enable(false)	inst.Light:SetRadius(lightradius)	inst.Light:SetFalloff(lightfalloff)	inst.Light:SetIntensity(lightintensity)	inst.Light:SetColour(65/65,160/160,40/40)	end	end, GetWorld() )	inst:ListenForEvent( "daytime", function() 	inst.entity:AddLight()	inst.Light:Enable(false)	inst.Light:SetRadius(lightradius)	inst.Light:SetFalloff(lightfalloff)	inst.Light:SetIntensity(lightintensity)	inst.Light:SetColour(65/65,160/160,40/40)	inst.components.hunger:SetRate(0.75*TUNING.WILSON_HUNGER_RATE) --slower hunger rate during the day	end, GetWorld() )end

This is my character's code. Focus from line 56 to 85.

It basically emits light during the night if the hunger value is higher than 104, the problem is that the hunger check is ran only once, when night starts.

I want to make it that the night vision appears/disappears mid-night if the hunger value is enough/not enough.

 

Another question, is it possible to craft armor with higher durability than usual? For example, crafting a Log Suit with 35% more HP, giving it 607 rather than 450 (not really necessary, but maybe display 135% in the inventory?Even though it's probably going to be consequential).

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Mobbstar    13,634

Regarding the first problem, I suggest simply doing a periodic update (that you can probably quit at daytime)

 

local checklight = inst:DoTaskInTime(time,function) --do "function" in "time" seconds

 

checklight:Cancel() --stop doing it

 

By the way, you only need to add and describe the light once, you can turn it on and off whenever you want afterwards (see this for reference). Move all light things other than Enable() out of the update code and put it with the other stats.

 

Regarding the armour durability, try to just listen for the crafting event and set the items condition higher.

Share this post


Link to post
Share on other sites
Blueberrys    172

@jimmosio Use the hungerdelta event, it updates whenever the hunger value is changed.

local function check_light(inst, data)	if GetClock():IsNight() and inst.components.hunger.current > 104 then		-- if light off, enable light	else		-- if light on, disable light	endendinst:ListenForEvent("hungerdelta", check_light)-- To ensure it's working even if the hunger doesn't change at night/dayinst:ListenForEvent("nighttime", check_light)inst:ListenForEvent("daytime", check_light)

-

 

Not sure what approach you want to take on that, but here's how you can change the durability.

-- 135% of original durabilityarmor_inst.components.armor:SetPercent(1.35)

You can do it upon crafting with:

inst:ListenForEvent("builditem", function(inst, data)	if data.item.prefab == "armor_prefab" then		-- ...	endend)

Edit: Fixed data.item.prefab. (see below)

Share this post


Link to post
Share on other sites
jimmosio    0

The night vision thing works and all, but I can't get the increased armor working, it always stays 100%, and it doesn't seem to have more HP either (a hound always removes 4% of armor).

This is what I'm using:

    if data.item == "armorwood" then        --armor_inst.components.armor:SetPercent(1.35)		armor_inst.maxcondition = 135		armor_inst.components.armor:InitCondition(TUNING.ARMORWOOD * 1.35, TUNING.ARMORWOOD_ABSORPTION)	end	end)

I noticed how the max durability is capped at 100 in armor.lua, so I tried to increase it, but still nothing.

I crave help.

Share this post


Link to post
Share on other sites
Mobbstar    13,634

The night vision thing works and all, but I can't get the increased armor working, it always stays 100%, and it doesn't seem to have more HP either (a hound always removes 4% of armor).

This is what I'm using:

    if data.item == "armorwood" then        --armor_inst.components.armor:SetPercent(1.35)		armor_inst.maxcondition = 135		armor_inst.components.armor:InitCondition(TUNING.ARMORWOOD * 1.35, TUNING.ARMORWOOD_ABSORPTION)	end	end)

I noticed how the max durability is capped at 100 in armor.lua, so I tried to increase it, but still nothing.

I crave help.

 

lol do you even know what you're doing? :wilson_laugh:

 

  1. The condition is now 135 %
  2. The Maximum hitpoints is now 135 damage points
  3. The maximum and current condition is now 135 % of what is declared in the tuning file (technically still a 35% increase, it just shows as 100 %)

The first line *should* work. I don't know what cap you mean.

EDIT: Setting the initial condition changes the max hitpoints. The armor condition is handles in hitpoints, not percent (because of what would percent be?). It's only displayed in percent.

 

Please report back after removing the second and third line (you can comment them out using two "--") :wilson_nerdy:

Share this post


Link to post
Share on other sites
Blueberrys    172

@jimmosio See Mobbstar's solution above.

 

Also.. where is armor_inst coming from? Are you doing something like "armor_inst = data.item"? If what you provided here is all you have in your event function, it should probably crash with "attempt to index armor_inst a nil value". Considering that isn't happening, there may be another issue.

Testing the event myself to confirm.

 

Edit: data.item contains the prefab name, not the instance of the item. armor_inst still needs to come from somewhere though.

 

Edit 2: Nope, actually it is the instance. You would need to use data.item.prefab to check if you've got the right item. Testing to confirm.

 

Edit 3: Works.

inst:ListenForEvent("builditem", function(inst, data)	if (data.item.prefab == "armorwood") then		data.item.components.armor:SetPercent(1.35)	endend)

Share this post


Link to post
Share on other sites
jimmosio    0

 

@jimmosio See Mobbstar's solution above.

 

Also.. where is armor_inst coming from? Are you doing something like "armor_inst = data.item"? If what you provided here is all you have in your event function, it should probably crash with "attempt to index armor_inst a nil value". Considering that isn't happening, there may be another issue.

Testing the event myself to confirm.

 

Edit: data.item contains the prefab name, not the instance of the item. armor_inst still needs to come from somewhere though.

 

Edit 2: Nope, actually it is the instance. You would need to use data.item.prefab to check if you've got the right item. Testing to confirm.

 

Edit 3: Works.

inst:ListenForEvent("builditem", function(inst, data)	if (data.item.prefab == "armorwood") then		data.item.components.armor:SetPercent(1.35)	endend)

You're a god among men.

/thread

Share this post


Link to post
Share on other sites
jimmosio    0

I need to bump this post again. I want to do the same thing, but in DST. Should be easy enough to write down, but what variable should I use to identify the item?

This is the code in its actual state:

local function ab(inst,data)        if data.item.prefab == "armorwood" or "armormarble" or "armorruins" or "ruinshat" or "armordragonfly" or "footballhat" or "armorgrass" or "armor_sanity" or "beehat" then        data.item.components.armor:SetPercent(1.35)    endend

Share this post


Link to post
Share on other sites
Blueberrys    172
what variable should I use to identify the item?

You mean prefab name? Not sure what you're looking for, but that if statement won't work how you think.

if data.item.prefab == "armorwood" or "armormarble" ...

Means "if the name is 'armorwood', OR if 'armormarble exists..."

 

Should be:

if data.item.prefab == "armorwood" or data.item.prefab == "armormarble" ...

-

Alternately:

if data.item.prefab:sub(1, 5) == "armor" ...

for everything starting with "armor"

 

 

Edit:

More code.

-- ending in hatif data.item.prefab:sub(-3) == "hat" ...-- containing armorif data.item.prefab:find("armor", 1, true) ...-- containing hatif data.item.prefab:find("hat", 1, true) ...

Share this post


Link to post
Share on other sites
jimmosio    0

I used the code you handed me for 2 weeks or so, with the one I wrote I tried to "optimize space", but I realised that crashes my game if I try to craft anything other than armor.

 

I'll try to explain my problem more clearly.

The code works perfectly in DS, but if I put the same code in DST, the crafted armor starts at 100% durability rather than 135%. I suspect that I have to use a different instance rather than "data". A bit like GetWorld() in DS and TheWorld in DST, hopefully you get the comparison.

Share this post


Link to post
Share on other sites
Blueberrys    172
the one I wrote I tried to "optimize space"

You can't just cut out parts of the code to optimise space xD It has to make sense too.

 

works perfectly in DS, but if I put the same code in DST...
 

I see. First of all, fix the if statement. Either write them all out correctly, or use one of the shorter methods I suggested.

 

The builditem event is still being used in DST through the builder component.

The armor component still has a operational SetPercent function.

I think the issue may be related to the networking aspect. Perhaps the code isn't executing because it's not running on the host. I suggest you post on the DST mod forums. Include your full code, logs, etc.

Share this post


Link to post
Share on other sites