Jump to content

how to spawn prefab to inventory


Recommended Posts

Well, in game, capturing a butterfly with the bug net, planting it as a flower, gives unlimited butterflies that can be caught with a bug net..

i wanna create a trader actually , the player give it butterflywings,then it return a butterfly that can be placed,but it still not work right.

Link to comment
Share on other sites

Look into components\trader.lua and prefabs\pigking.lua, also the Inventory:GiveItem function in inventory.lua which is used in the Trader:AcceptGift function of trader.lua, or paste your code here.

local assets =

{

Asset("ANIM","anim/kazuhaamulet.zip"),

Asset("ANIM","anim/torso_kazuhaamulet.zip"),

Asset( "IMAGE", "images/inventoryimages/kazuhaamulet.tex" ),

Asset( "ATLAS", "images/inventoryimages/kazuhaamulet.xml" ),

}

local prefabs = {

"crystal_tiny", --2%

"crystal_tiny_n", --2%

"redgem", --5%

"bluegem", --5%

"goldnugget", --80%

"nightmarefuel", --5%

"butteryfly",

}

local function fn()

local function onequip(inst,owner)

owner.AnimState:OverrideSymbol("swap_body","torso_kazuhaamulet","blueamulet")

--if isKazuha Remove debuff

if ThePlayer.prefab == "kazuha" then

owner.components.combat.damagemultiplier = 1

owner.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE

owner.components.sanity.dapperness = TUNING.DAPPERNESS_SMALL

end

if inst.components.fueled then

inst.components.fueled:StartConsuming()

end

end

local function onunequip(inst,owner)

owner.AnimState:ClearOverrideSymbol("swap_body")

--on unequip if is Kazuha Add debuff

if ThePlayer.prefab == "kazuha" then

owner.components.combat.damagemultiplier = .45

owner.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE * 1.25

owner.components.sanity.dapperness = TUNING.DAPPERNESS_HUGE * (- 1.25)

end

if inst.components.fueled then

inst.components.fueled:StopConsuming()

end

end

local function ontradeforgold(inst,item)

local goldchance = .5

for k = 1, item.components.tradable.goldvalue do

if math.random() < goldchance then

local nug = SpawnPrefab("goldnugget")

local pt = Vector3(inst.Transform:GetWorldPosition()) + Vector3(0, 1, 0)

nug.Transform:SetPosition(pt:Get())

local down = TheCamera:GetDownVec()

local angle = math.atan2(down.z, down.x) + (math.random() * 60 - 30) * DEGREES

local sp = math.random() * 4 + 2

nug.Physics:SetVel(sp / 4 * math.cos(angle), math.random() * 2 + 8, sp / 4 * math.sin(angle))

end

end

end

local function ontradeforlargergoldval(inst,item)

local nug = SpawnPrefab("crystal_tiny")

local pt = Vector3(inst.Transform:GetWorldPosition()) + Vector3(0, 1, 0)

nug.Transform:SetPosition(pt:Get())

local down = TheCamera:GetDownVec()

local angle = math.atan2(down.z, down.x) + (math.random() * 60 - 30) * DEGREES

local sp = math.random() * 4 + 2

nug.Physics:SetVel(sp / 4 * math.cos(angle), math.random() * 2 + 8, sp / 4 * math.sin(angle))

end

local function ontradeforbutterfly(inst,item)

local owner = inst.components.inventoryitem ~= nil and inst.components.inventoryitem.owner or nil

if owner then

owner.components.inventory:GiveItem("butteryfly")

end

end

local function ShouldAcceptOrNot(inst, item, giver)

local can_accept = item.components.tradable

--edible and item.components.edible.foodtype == FOODTYPE.MEAT

return can_accept

--return true

end

local function OnGetItemFromPlayer(inst, giver, item)

-- if item.components.edible ~= nil and item.components.edible.foodtype == FOODTYPE.MEAT then

-- print("its ok")

-- end

--giver.components.inventory:GiveItem("butteryfly",2)

if item.components.tradable.goldvalue > 0 and item.components.tradable.goldvalue <= 1 then

inst:DoTaskInTime(20/30, ontradeforgold, item)

elseif item.components.tradable.goldvalue > 1 then

inst:DoTaskInTime(20/30, ontradeforlargergoldval, item)

elseif item.prefab == "butterflywings" then

giver.components.inventory:ConsumeByName("butterflywings", 1)

inst:DoTaskInTime(20/30, ontradeforbutterfly, item)

end

end

local inst = CreateEntity()

inst.entity:AddTransform()

inst.entity:AddAnimState()

inst.entity:AddNetwork()

MakeInventoryPhysics(inst)

inst.AnimState:SetBank("kazuhaamulet")

inst.AnimState:SetBuild("kazuhaamulet")

inst.AnimState:PlayAnimation("idle")

inst:AddTag("kazuhaamulet")

inst.foleysound = "dontstarve/movement/foley/jewlery"

inst.entity:SetPristine()

if not TheWorld.ismastersim then

return inst

end

inst:AddComponent("inspectable")

inst:AddComponent("equippable")

inst.components.equippable.equipslot = EQUIPSLOTS.NECK or EQUIPSLOTS.BODY

inst.components.equippable.dapperness = TUNING.DAPPERNESS_SMALL

inst.components.equippable:SetOnEquip(onequip)

inst.components.equippable:SetOnUnequip(onunequip)

inst:AddComponent("inventoryitem")

inst.components.inventoryitem.atlasname = "images/inventoryimages/kazuhaamulet.xml"

inst.components.inventoryitem.keepondeath = true

inst:AddComponent("fueled")

inst.components.fueled.fueltype = FUELTYPE.NIGHTMARE

inst.components.fueled:InitializeFuelLevel(TUNING.TOTAL_DAY_TIME*6)

inst.components.fueled:SetDepletedFn(inst.Remove)

inst.components.fueled.accepting = true

inst:AddComponent("trader")

inst.components.trader.onaccept = OnGetItemFromPlayer

inst.components.trader:SetAcceptTest(ShouldAcceptOrNot)

MakeHauntableLaunch(inst)

return inst

end

return Prefab("common/inventory/kazuhaamulet", fn, assets, prefabs)

Link to comment
Share on other sites

Look into components\trader.lua and prefabs\pigking.lua, also the Inventory:GiveItem function in inventory.lua which is used in the Trader:AcceptGift function of trader.lua, or paste your code here.

it work for the items that can use Spawnprefab function ,but not work for the inventory items,i havent found any solution till then.

Link to comment
Share on other sites

Following up on this as I'm trying to do something similar. Is there a way I can use a variation of this format on the character itself?

Basically I'm looking to do something like press "B" to spawn a berry in my character inventory. Using "player" or self in place of "inst" seems to yield a "nil" error. 

 

AddModRPCHandler(modname, "POCKETFOOD", function(player)	if not player:HasTag("playerghost") and player.prefab == "fluff" then		if not player.pocketfoodincooldown then			player.components.talker:Say("Oh I found something.")			player.components.health:DoDelta(-1)	  	        player.components.sanity:DoDelta(-5)-- inst.components.inventory:GiveItem(SpawnPrefab("smallmeat"))					end			player:DoTaskInTime(60, function()				player.components.talker:Say("I'm all rest up")				player.pocketfoodincooldown = false			end)		else			player.components.talker:Say("I need some time.")		end	endend)

 

Edited by Hyaciao
Link to comment
Share on other sites

 

Following up on this as I'm trying to do something similar. Is there a way I can use a variation of this format on the character itself?

Basically I'm looking to do something like press "B" to spawn a berry in my character inventory. Using "player" or self in place of "inst" seems to yield a "nil" error. 

 

AddModRPCHandler(modname, "POCKETFOOD", function(player)	if not player:HasTag("playerghost") and player.prefab == "fluff" then		if not player.pocketfoodincooldown then			player.components.talker:Say("Oh I found something.")			player.components.health:DoDelta(-1)	  	        player.components.sanity:DoDelta(-5)-- inst.components.inventory:GiveItem(SpawnPrefab("smallmeat"))					end			player:DoTaskInTime(60, function()				player.components.talker:Say("I'm all rest up")				player.pocketfoodincooldown = false			end)		else			player.components.talker:Say("I need some time.")		end	endend)
 
 
 
 

 

Edited by Milllk
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...