Jump to content

Loot drop when fuel runs out


Recommended Posts

Hello everyone!

I want when fuel ends, then item don't just disappear, but give back something.
How do I change code so that after fuel runs out, give back "dug_berrybush"?

 

		inst:AddComponent("fueled")
        inst.components.fueled.fueltype = FUELTYPE.USAGE
        inst.components.fueled:InitializeFuelLevel(TUNING.BUSHHAT_PERISHTIME)
        inst.components.fueled:SetDepletedFn(inst.Remove)

 

Edited by Tezumoto
Link to comment
Share on other sites

6 hours ago, IronHunter said:

Very similar to this recent thread

If you need clarification I can do that.

local function bushdel(inst)
	local item = SpawnPrefab("cutgrass")
	owner.components.inventory:GiveItem(item)
	inst:Remove()
end

AddPrefabPostInit("bushhat", function(inst)
	if TUNING.BUSHHAT_PERISHTIME > 0 and GLOBAL.TheWorld.ismastersim then
		inst:AddComponent("fueled")
        inst.components.fueled.fueltype = FUELTYPE.USAGE
        inst.components.fueled:InitializeFuelLevel(TUNING.BUSHHAT_PERISHTIME)
        inst.components.fueled:SetDepletedFn(bushdel)
	end
end)

Is it possible to do this using only modmain.lua or do I have to edit item file?

Edited by Tezumoto
Link to comment
Share on other sites

The whole point of prefabpostinit is to be able to modify existing stuff in the modmain.

You would just wrap the existing function and just spawn a bush into your inventory.

Edit:

Give me an hour and I'll be able to help you out more detailed if you have more questions.

Edit2:

That should work, but make sure you add a if statement to make this server side only.

 

Edited by IronHunter
See above
Link to comment
Share on other sites

14 hours ago, IronHunter said:

The whole point of prefabpostinit is to be able to modify existing stuff in the modmain.

You would just wrap the existing function and just spawn a bush into your inventory.

Edit:

Give me an hour and I'll be able to help you out more detailed if you have more questions.

Edit2:

That should work, but make sure you add a if statement to make this server side only.

 

This code working.

local TUNING = GLOBAL.TUNING
local FUELTYPE = GLOBAL.FUELTYPE
local SpawnPrefab = GLOBAL.SpawnPrefab

TUNING.BUSHHAT_PERISHTIME = GetModConfigData("BUSHHAT_PERISHTIME")

	--	--	--	--	--	--	--	--	--	--	--
AddPrefabPostInit("bushhat", function(inst)
	if TUNING.BUSHHAT_PERISHTIME > 0 and GLOBAL.TheWorld.ismastersim then
	
	local function bushdel(inst)
		local owner = inst.components.inventoryitem.owner
		local item = SpawnPrefab("dug_berrybush")
		owner.components.inventory:GiveItem(item)
		inst:Remove()
	end
	
        inst:AddComponent("fueled")
        inst.components.fueled.fueltype = FUELTYPE.USAGE
        inst.components.fueled:InitializeFuelLevel(TUNING.BUSHHAT_PERISHTIME)
        inst.components.fueled:SetDepletedFn(bushdel)
		
	end
end)
	--	--	--	--	--	--	--	--	--	--	--

I want write code for spawn two items.
Can you help?

local item = SpawnPrefab("dug_berrybush"; "silk")

Or

local item = SpawnPrefab("dug_berrybush", "silk")

This my edit don't work.

P.S.
And, how to create a SpawnPrefab item with a 50% chance?

Edited by Tezumoto
Link to comment
Share on other sites

you mean you want it to spawn a random item?

There are many ways to go about it, using lootdropper as examples.

But you just call a seperate spawnprefab for each item you want to spawn and you can use if statements with a math.random to check if it should spawn/run.

local function bushdel1(inst)
		local owner = inst.components.inventoryitem.owner
		local item = Math.Random()<=0.5 and SpawnPrefab("dug_berrybush") or SpawnPrefab("silk")
		owner.components.inventory:GiveItem(item)
		inst:Remove()
end
local function bushdel2(inst)
  	local owner = inst.components.inventoryitem.owner
  	local item = nil
  	if Math.Random()<=0.5 then
    	item = SpawnPrefab("dug_berrybush")
    else
    	item = SpawnPrefab("silk")
    end
    if item ~= nil then owner.components.inventory:GiveItem(item)
    inst:Remove()
end
--Making a expandable equally distributed array
local bushloot = {"dug_berrybush", "silk"}
local function randomindex(t) --Selects a random item from a table
    local keys = {}
    for key, value in pairs(t) do
        keys[#keys+1] = key --Store keys in another table
    end
    local index = keys[math.random(1, #keys)]
    return t[index]
end
local function bushdel3(inst)
  	local owner = inst.components.inventoryitem.owner
  	local item = SpawnPrefab(randomindex(bushloot))
  	owner.components.inventory:GiveItem(item)
  	inst:Remove()
end

If you wanted to make it a 50% to spawn nothing you would use one of the first 2 methods and adjust probability accordingly.

Link to comment
Share on other sites

47 minutes ago, IronHunter said:

you mean you want it to spawn a random item?

There are many ways to go about it, using lootdropper as examples.

But you just call a seperate spawnprefab for each item you want to spawn and you can use if statements with a math.random to check if it should spawn/run.


local function bushdel1(inst)
		local owner = inst.components.inventoryitem.owner
		local item = Math.Random()<=0.5 and SpawnPrefab("dug_berrybush") or SpawnPrefab("silk")
		owner.components.inventory:GiveItem(item)
		inst:Remove()
end
local function bushdel2(inst)
  	local owner = inst.components.inventoryitem.owner
  	local item = nil
  	if Math.Random()<=0.5 then
    	item = SpawnPrefab("dug_berrybush")
    else
    	item = SpawnPrefab("silk")
    end
    if item ~= nil then owner.components.inventory:GiveItem(item)
    inst:Remove()
end

--Making a expandable equally distributed array
local bushloot = {"dug_berrybush", "silk"}
local function randomindex(t) --Selects a random item from a table
    local keys = {}
    for key, value in pairs(t) do
        keys[#keys+1] = key --Store keys in another table
    end
    local index = keys[math.random(1, #keys)]
    return t[index]
end
local function bushdel3(inst)
  	local owner = inst.components.inventoryitem.owner
  	local item = SpawnPrefab(randomindex(bushloot))
  	owner.components.inventory:GiveItem(item)
  	inst:Remove()
end

If you wanted to make it a 50% to spawn nothing you would use one of the first 2 methods and adjust probability accordingly.


Mod "bushhat" is working (need spawn only 1 item).
Now I need create code for "nightmare_timepiece" spawn two iteams.

 

	if TUNING.NIGHTMARE_TIMEPIECE_FUEL_MAX > 0 and GLOBAL.TheWorld.ismastersim then
		inst:AddComponent("fueled")
		inst.components.fueled.maxfuel = TUNING.NIGHTMARE_TIMEPIECE_FUEL_MAX
		inst.components.fueled.fueltype = FUELTYPE.NIGHTMARE
		inst.components.fueled:InitializeFuelLevel(TUNING.NIGHTMARE_TIMEPIECE_FUEL_MAX / TUNING.NIGHTMARE_TIMEPIECE_START)
		inst.components.fueled:StartConsuming()
		inst.components.fueled:SetDepletedFn(inst.Remove)
		inst.components.fueled.accepting = true
	end


This item can't is wearing character (this is not clothing).
This item is in inventory or in a backpack or on the ground.
This item loses fuel permanently.
Therefore, it is necessary that he created things where he is (inventory or backpack or on the ground).

P.S.
Math.Random - don't need for this.

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