Jump to content

[Solved]Weapon with two modes


Recommended Posts

Hello,

I'm creating a Frying Pan for a character and am trying to give it two modes. The first is just the plain old Frying Pan, the second becomes a Hot Frying Pan after you cook it on a fire. The main issue I'm running into is the durability, in order to cook the pan it has to have a separate prefab and thus a completely separate durability. I was wondering if anyone knew how to store a value (the weapons uses) without it being overwritten. Here is the code I have so far.

local assets=
{ 
    Asset("ANIM", "anim/pan.zip"),
    Asset("ANIM", "anim/swap_pan.zip"), 

    Asset("ATLAS", "images/inventoryimages/pan.xml"),
    Asset("IMAGE", "images/inventoryimages/pan.tex"),
	
	Asset("ATLAS", "images/inventoryimages/hotpan.xml"),
    Asset("IMAGE", "images/inventoryimages/hotpan.tex"),
	
	Asset("INV_IMAGE", "pan")
}

local function OnSave(inst, data)
	--If pan is hot save the amount of time remaining.
end

local function OnLoad(inst, data)
	--If the pan is hot load it's remaining time.
end

local function OnEquip(inst, owner)
	owner.AnimState:OverrideSymbol("swap_object", "swap_pan", "pan")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

local function OnEquip_Hot(inst, owner)
	owner.AnimState:OverrideSymbol("swap_object", "swap_pan", "hotpan")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

local function OnUnequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
end

local function cooked_timer(inst, owner)
	local pantime = inst.components.pantimer:Getptimer()
	--print (pantime)
	local owner = ThePlayer
	local anim = inst.entity:AddAnimState()
	
	if pantime == 0 then
		print ("Timer Done")		
		inst.components.weapon:SetDamage(34)
		
		inst.components.heater:SetThermics(false, false)
		
		anim:SetBank("pan")
		anim:SetBuild("pan")
		anim:PlayAnimation("idle")
		
		inst.components.inventoryitem.imagename = "pan"
		inst.components.inventoryitem.atlasname = "images/inventoryimages/pan.xml"
		inst.components.inventoryitem:ChangeImageName ("pan")--required?
						
		inst._light.Light:Enable(false)
		
		inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
		
		inst.components.equippable:SetOnEquip( OnEquip )
		
		inst.hottask:Cancel()
	else
		inst.components.pantimer:SetTimer(pantime-1)
	end
	
	print("cooked_timer function")
end

local function Stored(inst)
	local remaining_uses = inst.components.finiteuses:GetUses()
	--print (remaining_uses)
	--print("Stored Function")
	--print(remaining_uses)
	return remaining_uses
end

local function pan()
    local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
	
	inst.entity:AddNetwork()
	inst.entity:AddSoundEmitter()
	
    MakeInventoryPhysics(inst)
    
    anim:SetBank("pan")
    anim:SetBuild("pan")
    anim:PlayAnimation("idle")
		
	inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

	inst:AddComponent("cookable")
	inst:AddComponent("equippable")
	inst:AddComponent("finiteuses")
	inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
	--inst:AddComponent("pantimer")
	inst:AddComponent("weapon")
	
	inst.components.cookable:SetOnCookedFn(Stored)
	inst.components.cookable.product = "hotpan"--when cooked perform SpawnPrefab("hotpan")
	
	inst.components.inventoryitem.imagename = "pan"
	inst.components.inventoryitem.atlasname = "images/inventoryimages/pan.xml"
	
    inst.components.weapon:SetDamage(34)
	inst.components.finiteuses:SetMaxUses(300)
	inst.components.finiteuses:SetUses(300)
	inst.components.finiteuses:SetOnFinished(inst.Remove)
	
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )
	
	MakeHauntableLaunch(inst)

	print ("pan fuction")
    return inst
end

local function hot_pan()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	
	inst.entity:AddNetwork()
	inst.entity:AddSoundEmitter()
	
	MakeInventoryPhysics(inst)
	
	anim:SetBank("pan")
    anim:SetBuild("pan")
    anim:PlayAnimation("hot")--Changes ground anim to the hotpan
		
	inst.entity:SetPristine()
	
	if not TheWorld.ismastersim then
        return inst
    end
	
	inst:AddComponent("cookable")
	inst:AddComponent("cooker")
	inst:AddComponent("equippable")
	inst:AddComponent("finiteuses")
	inst:AddComponent("heater")
	inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
	inst:AddComponent("pantimer")
	inst:AddComponent("weapon")
	
	inst.components.cookable.product = "hotpan"
		
	inst.components.inventoryitem.imagename = "hotpan"
	inst.components.inventoryitem.atlasname = "images/inventoryimages/hotpan.xml"

	inst.components.weapon:SetDamage(42.5)
	inst.components.finiteuses:SetMaxUses(300)
	inst.components.finiteuses:SetUses(300)--set uses based on remaining uses before it was cooked.
	inst.components.finiteuses:SetOnFinished(inst.Remove)
	
	inst.components.equippable:SetOnEquip( OnEquip_Hot )
    inst.components.equippable:SetOnUnequip( OnUnequip )
	
	MakeHauntableLaunch(inst)
	
	inst.components.heater:SetThermics(true, false)
	inst.components.heater.heat = 40
	
	inst.components.pantimer:SetTimer(5)--how long hot pan lasts
	inst.hottask = inst:DoPeriodicTask(1, cooked_timer )
	
	inst._light = SpawnPrefab("panlight")
	inst._light.entity:AddFollower()
    inst._light.Follower:FollowSymbol(inst.GUID, "swap_object", 0, -110, 0)
	inst._light.Light:Enable(true)
	
	print ("hot_pan fuction")
	return inst
end

local function lightfn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddLight()
    inst.entity:AddNetwork()

    inst:AddTag("FX")

    inst.Light:SetRadius(.6)
    inst.Light:SetFalloff(1)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(235 / 255, 165 / 255, 12 / 255)
    inst.Light:Enable(false)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

	--print ("light fuction")
    return inst
end

return  Prefab("pan", pan, assets),
		Prefab("hotpan", hot_pan, assets),
		Prefab("panlight", lightfn)

 Any help is appreciated. If you can think of another way around the method I'm using I'm all ears(eyes).

Thanks,

Nathan

Edited by RedHairedHero
Solved the issue
Link to comment
Share on other sites

Rather than cooking it on the fire, maybe use a similar method to the heatrock/Thermal Stone? Where it heats up when near a heat source.
You can even make it so if its currently in a inventory it won't heat up only if its on the ground near a source.
You can set your own range if you don't want it becoming too hot or cold etc.
You don't need to copy the part where it heats up you character btw, but it is a good alternative.

The cookable component will destroy the old prefab and spawn a replacement one, I know the component transfers some data over in the form of perish stat, but not much else. To transfer over durability like you desire would require modifying the cookable component to have an exception for your item and return the original for anything else.

Another alternative idea is to make your item have a custom component action and function when used on a fire that just heats it up rather than cooking it, this way you don't have to use two prefabs as well. This is a little more complex than other methods of copy pasting and tweaking.

Hope that gives you some ideas to work with.

Link to comment
Share on other sites

2 hours ago, IronHunter said:

Rather than cooking it on the fire, maybe use a similar method to the heatrock/Thermal Stone? Where it heats up when near a heat source.
You can even make it so if its currently in a inventory it won't heat up only if its on the ground near a source.
You can set your own range if you don't want it becoming too hot or cold etc.
You don't need to copy the part where it heats up you character btw, but it is a good alternative.

The cookable component will destroy the old prefab and spawn a replacement one, I know the component transfers some data over in the form of perish stat, but not much else. To transfer over durability like you desire would require modifying the cookable component to have an exception for your item and return the original for anything else.

Another alternative idea is to make your item have a custom component action and function when used on a fire that just heats it up rather than cooking it, this way you don't have to use two prefabs as well. This is a little more complex than other methods of copy pasting and tweaking.

Hope that gives you some ideas to work with.

Thanks for the ideas, I decided to go the Thermal Stone route and it's working how I was wanting, removed the cold part of it and added the perks (additional damage and ability to cook) was way easier then the route I was taking.

Link to comment
Share on other sites

24 minutes ago, RedHairedHero said:

Thanks for the ideas, I decided to go the Thermal Stone route and it's working how I was wanting, removed the cold part of it and added the perks (additional damage and ability to cook) was way easier then the route I was taking.

I am glad that I could help =]

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