Jump to content

Any way of detecting when hat runs out?


_Q_

Recommended Posts

Is there any way of detecting that hat just broken?

The hats with durability like flower hat, top hat and so on.

 

I haven't tested it, but something like this should work:

local depletedfn(inst)    --push an event here or do thingsendinst.components.fueled:SetDepletedFn(depletedfn)

It's not necessarily 'detecting it', instead it's skipping straight to whatever you want to happen as a result of detecting it.

Link to comment
Share on other sites

I haven't tested it, but something like this should work:

local depletedfn(inst)    --push an event here or do thingsendinst.components.fueled:SetDepletedFn(depletedfn)

It's not necessarily 'detecting it', instead it's skipping straight to whatever you want to happen as a result of detecting it.

I'm making a mod that gives random hats to pigs when they spawn, then I have to preplace the hat with new one if it brakes up so I need to know when hat runs out on them. They are set so they don't drop any hats, but you can still give a hat to pig and then its set to drop hat on death.

Its all messed up cause I can't tell when the hat runs out or if it was hat given by player or just random hat.

Link to comment
Share on other sites

I'm making a mod that gives random hats to pigs when they spawn, then I have to preplace the hat with new one if it brakes up so I need to know when hat runs out on them. They are set so they don't drop any hats, but you can still give a hat to pig and then its set to drop hat on death.

Its all messed up cause I can't tell when the hat runs out or if it was hat given by player or just random hat.

Well, that snippet above will definitely run when the hat dies, and you can point it to a function that replaces the hat.

 

As for knowing whether or not the hat was giving by a player I would just use tags to do that. So when a hat is given to the pig, give that particular hat a tag like "playergiven" and just exclude hats with that tag when replacing the broken hat. Same goes for dropping, only drop hats on death if the hat has that tag.

Link to comment
Share on other sites

Well, that snippet above will definitely run when the hat dies, and you can point it to a function that replaces the hat.

 

As for knowing whether or not the hat was giving by a player I would just use tags to do that. So when a hat is given to the pig, give that particular hat a tag like "playergiven" and just exclude hats with that tag when replacing the broken hat. Same goes for dropping, only drop hats on death if the hat has that tag.

 

That would require post init to all hats, something I'm trying to avoid.

 

Link to comment
Share on other sites

That would require post init to all hats, something I'm trying to avoid.

 

 

You can modify the individual hats on each pig, at the same point and in the same way as you give them the hats, in the way I described.

 

How are you spawning them with hats?

Link to comment
Share on other sites

You can modify the individual hats on each pig, at the same point and in the same way as you give them the hats, in the way I described.

 

How are you spawning them with hats?

I just made component for that, its all messy atm:

local PigHats = Class(function(self, inst)	self.inst = inst    self.playerhat = false	self.randomhat = false		if self.playerhat == false and self.randomhat = false then self:GiveRandomHat() endend)function PigHats:PickHat()	local hats = {"strawhat", "tophat", "beefalohat" , "featherhat", "flowerhat", "earmuffshat", "winterhat", "spiderhat", "walrushat", "icehat", "molehat", "catcoonhat", "watermelonhat", "rainhat"}	local hatnum = math.random(1, #hats)	local hat = hats[hatnum]	return hatendfunction PigHats:GiveRandomHat()		local hat = self:PickHat()		local item = SpawnPrefab(hat)		self.inst.components.inventory:Equip(item)		self.inst.AnimState:Show("hat")endfunction PigHats:OnSave()	local data = {}	data.playerhat = self.playerhat	data.randomhat = self.randomhat	return dataendfunction PigHats:OnLoad(data)	self.playerhat = data.hasplayerhat or false	self.randomhat = data.hasrandomhat or falseendreturn PigHats

Then in modmain:

local function OnGetItemFromPlayer(inst, giver, item)    --I eat food    if item.components.edible then        --meat makes us friends (unless I'm a guard)        if item.components.edible.foodtype == "MEAT" or item.components.edible.foodtype == "HORRIBLE" then            if inst.components.combat.target and inst.components.combat.target == giver then                inst.components.combat:SetTarget(nil)            elseif giver.components.leader and not inst:HasTag("guard") then				inst.SoundEmitter:PlaySound("dontstarve/common/makeFriend")				giver.components.leader:AddFollower(inst)                inst.components.follower:AddLoyaltyTime(item.components.edible:GetHunger() * TUNING.PIG_LOYALTY_PER_HUNGER)            end        end        if inst.components.sleeper:IsAsleep() then            inst.components.sleeper:WakeUp()        end    end        --I wear hats    if item.components.equippable and item.components.equippable.equipslot == GLOBAL.EQUIPSLOTS.HEAD then		if inst.components.pighats.playerhat then			local current = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)				if current then				inst.components.inventory:DropItem(current)				end        			inst.components.inventory:Equip(item)			inst.AnimState:Show("hat")		else			local current = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)			if current then			inst.components.inventory:RemoveItem(current)			end			inst.components.inventory:Equip(item)			inst.AnimState:Show("hat")			inst.components.pighats.playerhat = true		end    endendfunction PigmanPostinit(inst)	inst:AddComponent("pighats")	if inst.components.pighats.playerhat  == true then		inst.components.inventory.dropondeath = true	else		inst.components.inventory.dropondeath = false	end		if inst and inst.components.trader then		inst.components.trader.onaccept = OnGetItemFromPlayer	endendAddPrefabPostInit("pigman", PigmanPostinit)
Link to comment
Share on other sites

 

I just made component for that, its all messy atm:

local PigHats = Class(function(self, inst)	self.inst = inst    self.playerhat = false	self.randomhat = false		if self.playerhat == false and self.randomhat = false then self:GiveRandomHat() endend)function PigHats:PickHat()	local hats = {"strawhat", "tophat", "beefalohat" , "featherhat", "flowerhat", "earmuffshat", "winterhat", "spiderhat", "walrushat", "icehat", "molehat", "catcoonhat", "watermelonhat", "rainhat"}	local hatnum = math.random(1, #hats)	local hat = hats[hatnum]	return hatendfunction PigHats:GiveRandomHat()		local hat = self:PickHat()		local item = SpawnPrefab(hat)		self.inst.components.inventory:Equip(item)		self.inst.AnimState:Show("hat")endfunction PigHats:OnSave()	local data = {}	data.playerhat = self.playerhat	data.randomhat = self.randomhat	return dataendfunction PigHats:OnLoad(data)	self.playerhat = data.hasplayerhat or false	self.randomhat = data.hasrandomhat or falseendreturn PigHats

Then in modmain:

local function OnGetItemFromPlayer(inst, giver, item)    --I eat food    if item.components.edible then        --meat makes us friends (unless I'm a guard)        if item.components.edible.foodtype == "MEAT" or item.components.edible.foodtype == "HORRIBLE" then            if inst.components.combat.target and inst.components.combat.target == giver then                inst.components.combat:SetTarget(nil)            elseif giver.components.leader and not inst:HasTag("guard") then				inst.SoundEmitter:PlaySound("dontstarve/common/makeFriend")				giver.components.leader:AddFollower(inst)                inst.components.follower:AddLoyaltyTime(item.components.edible:GetHunger() * TUNING.PIG_LOYALTY_PER_HUNGER)            end        end        if inst.components.sleeper:IsAsleep() then            inst.components.sleeper:WakeUp()        end    end        --I wear hats    if item.components.equippable and item.components.equippable.equipslot == GLOBAL.EQUIPSLOTS.HEAD then		if inst.components.pighats.playerhat then			local current = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)				if current then				inst.components.inventory:DropItem(current)				end        			inst.components.inventory:Equip(item)			inst.AnimState:Show("hat")		else			local current = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)			if current then			inst.components.inventory:RemoveItem(current)			end			inst.components.inventory:Equip(item)			inst.AnimState:Show("hat")			inst.components.pighats.playerhat = true		end    endendfunction PigmanPostinit(inst)	inst:AddComponent("pighats")	if inst.components.pighats.playerhat  == true then		inst.components.inventory.dropondeath = true	else		inst.components.inventory.dropondeath = false	end		if inst and inst.components.trader then		inst.components.trader.onaccept = OnGetItemFromPlayer	endendAddPrefabPostInit("pigman", PigmanPostinit)

 

Again, not tested, but you should be able to do this in the function where you check to see if the pig has an item, using current to represent the instanced item:

current.components.fueled:SetDepletedFn(depletedfn)

and point it to your own function.

Link to comment
Share on other sites

Again, not tested, but you should be able to do this in the function where you check to see if the pig has an item, using current to represent the instanced item:

current.components.fueled:SetDepletedFn(depletedfn)

and point it to your own function.

 

Its working, must see where it will get me. Thanks.

Link to comment
Share on other sites

Got next problem.

How to call function that gives a hat to the pig after all data from pighat components is loaded, cause now it gives hat to the pig every game load.

 

What qualifications do you need fulfilled for a hat to be given?

 

I'd create an if block in GiveRandomHat, which makes sure that whatever qualifications are fulfilled.

 

I'm not sure what you mean by they're always given a hat, though. Do you mean that they're given a random hat even if they have one?

 

Edit: It doesn't seem like you're changing inst.randomhat to true anywhere, so onload and onsave aren't really doing anything useful with it.

Link to comment
Share on other sites

What qualifications do you need fulfilled for a hat to be given?

 

I'd create an if block in GiveRandomHat, which makes sure that whatever qualifications are fulfilled.

 

I'm not sure what you mean by they're always given a hat, though. Do you mean that they're given a random hat even if they have one?

Yes they always get a hat even if they had one and since they use standard inventory component they just put it away if they alredy have one until the invenotry get filled with hats, at that point they just drop one hat every load:D

I think I got that problem resolved with some magic:

self.inst:DoTaskInTime(0, function(inst) inst.components.pighats:GiveRandomHat() end)

This ensures the saved flag for random hat is loaded before that function is called, so no more rain of hats.

Link to comment
Share on other sites

Yes they always get a hat even if they had one and since they use standard inventory component they just put it away if they alredy have one until the invenotry get filled with hats, at that point they just drop one hat every load:D

I think I got that problem resolved with some magic:

self.inst:DoTaskInTime(0, function(inst) inst.components.pighats:GiveRandomHat() end)

This ensures the saved flag for random hat is loaded before that function is called, so no more rain of hats.

 

See my edit above. :p

 

When you run GiveRandomHat, you should set inst.randomhat to true. Secondly, in your onload, you're using data.hasrandomhat instead of data.randomhat.

 

At least in your code above.

Link to comment
Share on other sites

See my edit above. :razz:

 

When you run GiveRandomHat, you should set inst.randomhat to true. Secondly, in your onload, you're using data.hasrandomhat instead of data.randomhat.

 

At least in your code above.

That was changed a while ago, but if you put function call directly in component definition it will run before any saved data is loaded, and deflaut setting for random hat was false every load.

 

Also editing the hats like that:

local function depletedfn(inst)    if inst.components.equippable and inst.components.equippable:IsEquipped() then        local owner = inst.components.inventoryitem.owner		inst:Remove()                            if owner then        owner:PushEvent("randomhatrunout", {inst = inst})        end    endendlocal function ice_perish(inst)	local player = GetPlayer()        if inst.components.inventoryitem and player and inst.components.inventoryitem:IsHeldBy(player) then            if player.components.moisture then            player.components.moisture:DoDelta(20)			end		end    depletedfn(inst)endlocal PigHats = Class(function(self, inst)	self.inst = inst    self.playerhat = false	self.randomhat = false		self.inst:DoTaskInTime(0, function(inst) inst.components.pighats:GiveRandomHat() end)	self.inst:ListenForEvent("randomhatrunout", function() self.randomhat = false self:GiveRandomHat() end)end)function PigHats:PickHat()	local hats = {"strawhat", "tophat", "beefalohat" , "featherhat", "flowerhat", "earmuffshat", "winterhat", "walrushat", "icehat", "catcoonhat", "watermelonhat", "rainhat"}	local hatnum = math.random(1, #hats)	local hat = hats[hatnum]	return hatendfunction PigHats:GiveRandomHat()				if self.randomhat == false then		local hat = self:PickHat()		print(hat)		local item = SpawnPrefab(hat)		if hat == "icehat" then 			if item.components.perishable then 			item.components.perishable:SetOnPerishFn(ice_perish)			end		else			if item.components.fueled then 			item.components.fueled:SetDepletedFn(depletedfn)			elseif item.components.perishable then			item.components.perishable:SetOnPerishFn(depletedfn)			end		end		self.inst.components.inventory:Equip(item)		self.inst.AnimState:Show("hat")		self.randomhat = true		endendfunction PigHats:OnSave()	local data = {}	data.playerhat = self.playerhat	data.randomhat = self.randomhat	print("save")	print(self.playerhat)	print(self.randomhat)	return dataendfunction PigHats:OnLoad(data)	if data then	self.playerhat = data.playerhat	self.randomhat = data.randomhat	print("load")	print(self.randomhat)	print(self.playerhat)	end	if self.playerhat == false and self.randomhat == false then self:GiveRandomHat() endendreturn PigHats

Is that hat edit saved for that hat? Like it was prefab post init or something. It will not go away after game reloading, cause i'm not sure, didn't make edits like that so far.

Link to comment
Share on other sites

That was changed a while ago, but if you put function call directly in component definition it will run before any saved data is loaded, and deflaut setting for random hat was false every load.

 

Interesting. Glad you found a solution though.

 

You may want to move that part with the depletedfn to a function that will run every time the component is loaded, doing a boolean check on randomhat.

Link to comment
Share on other sites

:razz:

 

I had edited my above post in response already.

Yup that edits on hats are going away after reload, must do hats post init in that case, or just a check function like you said. Will see what will be better.

Link to comment
Share on other sites

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.

×
  • Create New...