Jump to content

Is it possible to have an item with both a fueled and infinite uses component?


Recommended Posts

So I'm trying to make an item that both functions as a lantern and a tool, so that when equipped it slowly loses fuel like a lantern would, but loses a set amount of fuel whenever used as a tool (with different amounts of fuel lost based on what action is performed). I'm wondering how I would set this up.

Also, does anyone know where the prefab files are for minerhats and lanterns? I cant seem to find them in the scripts/prefabs folder

Link to comment
Share on other sites

Miner Hat -> hats.lua(yes all of hats are in here, so it is some messy)

Lantern -> mininglantern.lua

 

inst:AddComponent("tool")
inst.components.tool:SetAction(ACTIONS.CHOP, 1.33) -- is 1.33x efficient than axe
inst.components.tool:SetAction(ACTIONS.MINE, 1.33) -- also pickaxe
-------
inst:AddComponent("finiteuses")
inst.components.finiteuses:SetMaxUses(TUNING.MULTITOOL_AXE_PICKAXE_USES)
inst.components.finiteuses:SetUses(TUNING.MULTITOOL_AXE_PICKAXE_USES)
inst.components.finiteuses:SetOnFinished(inst.Remove)
inst.components.finiteuses:SetConsumption(ACTIONS.CHOP, 1) -- will use 1 uses for every chop
inst.components.finiteuses:SetConsumption(ACTIONS.MINE, 3) -- but 3 for mine

(from axe_pickaxe.lua = Pick/Axe)

This is the base of multitool.

 

local function isequipped(inst)
	local owner = inst.components.inventoryitem.owner
	if not owner then return end -- if there are no owner(like dropped) it will not make error or use up itself
	if owner.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS) == inst then -- if equipped in hands,
		inst.components.finiteuses:Use(1) -- consume 1 use
	end
end
-------------------------------------------
inst:DoPeriodicTask(5, isequipped) -- will check every 5 seconds

And adding this function will make tool work like fueled ones.

Of course it is not like fuel in code-wise, but it will work like fuel in game-wise.

 

Edit: but if you want to turn it on or off like lantern... well we have to work with machine.lua like lantern.

Edited by Combustiblemon
Link to comment
Share on other sites

6 hours ago, Combustiblemon said:

but if you want to turn it on or off like lantern... well we have to work with machine.lua like lantern.

I already have it set up like a lantern, but I want it to consume fuel on each use instead of durability.
I have this set up for attacks:
image.thumb.png.ec4774951db389b5bc362c54018393c7.png

but I want it to do the same for ACTIONS.CHOP and ACTIONS.MINE

Link to comment
Share on other sites

@MF99K This is one possible approach. Finiteuses remains the main use system, while fueled is there just to handle taking fuel.
Up to you to figure out where everything needs to go though.

local function OnFueled(inst, data)
	local fuel_value = data.fuel_value -- value of the fuel being given
	local uses_to_restore = math.ceil(fuel_value / 5) -- so hypothetically, if we put in a light bulb, (which has a fuel_value of 90), we would restore 90/5 uses (which is 18)
	
	-- make sure we can't just keep infinitely fueling or fuel negatively
	uses_to_restore = math.clamp(uses_to_restore, 0, inst.components.finiteuses.total - inst.components.finiteuses.current)
	
	-- 2 ways to do this. filter it through :Use() or directly use :SetUses(). we'll use former.
	inst.components.finiteuses:Use(-uses_to_restore)
end

local inst = ...
inst:AddComponent("fueled")
inst.components.fueled.fueltype = FUELTYPE.CAVE -- i'll assume we can be fueled by either cave stuff (ie lightbulbs, fireflies, ...)
inst.comopnents.fueled.secondaryfueltype = FUELTYPE.BURNABLE -- or burnable stuff (logs, twigs, ...)
inst.components.fueled.DoDelta = function() end -- percent to be displayed in inventory needs to come from finiteuses
inst:ListenForEvent("takefuel", OnFueled)

inst:AddComponent("finiteuses")
inst.components.finiteuses:SetConsumption(ACTIONS.CHOP, 1)
inst.components.finiteuses:SetConsumption(ACTIONS.MINE, 3)
inst.components.finiteuses:SetMaxUses(100)
inst.components.finiteuses:SetUses(100)

 

Edited by penguin0616
  • Like 1
Link to comment
Share on other sites

1 hour ago, penguin0616 said:

@MF99K This is one possible approach. Finiteuses remains the main use system, while fueled is there just to handle taking fuel.
Up to you to figure out where everything needs to go though.



inst:AddComponent("fueled")
inst.components.fueled.fueltype = FUELTYPE.CAVE -- i'll assume we can be fueled by either cave stuff (ie lightbulbs, fireflies, ...)
inst.comopnents.fueled.secondaryfueltype = FUELTYPE.BURNABLE -- or burnable stuff (logs, twigs, ...)
inst.components.fueled.DoDelta = function() end -- percent to be displayed in inventory needs to come from finiteuses
inst:ListenForEvent("takefuel", OnFueled)

inst:AddComponent("finiteuses")
inst.components.finiteuses:SetConsumption(ACTIONS.CHOP, 1)
inst.components.finiteuses:SetConsumption(ACTIONS.MINE, 3)
inst.components.finiteuses:SetMaxUses(100)
inst.components.finiteuses:SetUses(100)

 

what I'm wondering here is since fueled and finite uses are usually both shown in inventory as a percent, do they use the same values or work on the same values

I would want consumption to subtract from fuel level

Link to comment
Share on other sites

59 minutes ago, MF99K said:

what I'm wondering here is since fueled and finite uses are usually both shown in inventory as a percent, do they use the same values or work on the same values

Neither. The inventory listens for "percentusedchanged", and displays the new value.

59 minutes ago, MF99K said:

I would want consumption to subtract from fuel level

Then it would probably be best to not use finiteuses at all, and listen for when the player does a specific action and use that to decrease fueled.

Edited by penguin0616
  • Like 1
Link to comment
Share on other sites

Just now, penguin0616 said:

Neither. The inventory listens for "percentusedchanged", and displays the new value.

Then it would probably be best to not use finiteuses at all, and listen for when the player does a specific action and use that to decrease fueled.

that's what I was asking about, but wanted to know how to tell the game to check for a specific action.
I ended up changing the code so that the tool/weapon's efficiency is proportional to fuel level instead

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...