Jump to content

Moar loot from stuff


Recommended Posts

Hi

 

I have a question and I saw that this is really helpful community soo let's try it :)

 

I wanted to change a prefab's loot table so my character can gather different stuff from it

I searched the forum a bit, and found this

AddPrefabPostInit("koalefant_summer",function(inst)    table.insert(inst.components.<span class="searchlite">loot</span>dropper.<span class="searchlite">loot</span>,"goldnugget")end)

I was so happy but then I tested it aaaaand it didn't work, it says "<name> expected near <" in that forum thread the guy wrote that it works  I don't understand what's the problem then...also is there a way how I can add a hastag to it so only an exact type of characters can get the different loot from the koalefant for example?

 

thanks the help :)

 

cheers

Link to comment
Share on other sites

You copied the code with the BBcode of the forum that makes search results yellow.

 

The code is:

AddPrefabPostInit("koalefant_summer", function(inst)	table.insert(inst.components.lootdropper.loot, "goldnugget")end)

However, this has a problem.

It will add a goldnugget to the loot table each time a koalefant_summer spawns, and the loot_summer table is shared among koalefants.

Therefore, for example, the third koalefant you kill will drop three goldnuggets.

 

Instead of using this, we will jump straight to an example for extra drops for a specific character.

For example, let us give Wilson a goldnugget loot drop when he kills a koalefant.

I attach the example.

Lootbags.zip

Link to comment
Share on other sites

There is no multipurpose solution, because there are few ways to change loot for prefab.

 

For example, replace loot:

local loot_summer = {	"meat","meat","meat","meat",	"meat","meat","meat","meat",	"trunk_summer","goldnugget", -- <<<<}AddPrefabPostInit("koalefant_summer",function(inst)	inst.components.lootdropper:SetLoot(loot_summer)end)

If you want just add loot to existing loot, you should use some kind of injection to Klei code. For koalefant:

local loot_koalefant_summerAddPrefabPostInit("koalefant_summer", function(inst)	if not loot_koalefant_summer then		loot_koalefant_summer = inst.components.lootdropper.loot		table.insert(loot_koalefant_summer, "goldnugget")	endend)

Link to comment
Share on other sites

Thank you both the awesome answers even tho I have three other questions now :/

- How can I make it random...like there's a chance that it drops but not always

- Why it doesn't work with picking items like I want to get two cut grass from grass...or get my own prefab from picking up flowers...it only works now with animals

- Can I use a hastag or something to make it character specific?

 

Thanks the help again guys :) you are awesome

There is no multipurpose solution, because there are few ways to change loot for prefab.

 

For example, replace loot:

local loot_summer = {	"meat","meat","meat","meat",	"meat","meat","meat","meat",	"trunk_summer","goldnugget", -- <<<<}AddPrefabPostInit("koalefant_summer",function(inst)	inst.components.lootdropper:SetLoot(loot_summer)end)

If you want just add loot to existing loot, you should use some kind of injection to Klei code. For koalefant:

local loot_koalefant_summerAddPrefabPostInit("koalefant_summer", function(inst)	if not loot_koalefant_summer then		loot_koalefant_summer = inst.components.lootdropper.loot		table.insert(loot_koalefant_summer, "goldnugget")	endend)

 

Link to comment
Share on other sites

@RealStarver, I update lootbags.

 

For the first thing, refer to the AddPrefabPostInit("willow".

 

For the second. We are only modifying the lootdropper of koalefants. Grass doesn't have lootdropper.

A different approach is needed.

What grass has is the pickable component, that when a player interacts with it, it spawns something and it is then given to the player.

Refer to the AddPrefabPostInit("wolfgang".

 

You can use the HasTag function on some stuff. But it depends on what you want to use it.

If you use AddPrefabPostInit("yourcharacterprefab", or you put the code inside the master_postinit of it, no need for tags.

Lootbags.zip

Link to comment
Share on other sites

So because of the two different style..I mean the kill and the pick you can't use the willow method...I mean the sharedloottable method if you pick up something? If that's the case can you change the prefab you get from picking up grass for example...like a goldnugget or something, or you can only control the amount of loot it gives?

 

Btw thanks so much for everything you do :) it helps a lot and you even describe it ^^

@RealStarver, I update lootbags.

 

For the first thing, refer to the AddPrefabPostInit("willow".

 

For the second. We are only modifying the lootdropper of koalefants. Grass doesn't have lootdropper.

A different approach is needed.

What grass has is the pickable component, that when a player interacts with it, it spawns something and it is then given to the player.

Refer to the AddPrefabPostInit("wolfgang".

 

You can use the HasTag function on some stuff. But it depends on what you want to use it.

If you use AddPrefabPostInit("yourcharacterprefab", or you put the code inside the master_postinit of it, no need for tags.

 

 

Link to comment
Share on other sites

@RealStarver, you just have to set it up differently.

Example:

AddPrefabPostInit("wolfgang", function(inst)	-- When Wolfgang picks up something	inst:ListenForEvent("picksomething", function(inst, data)		-- We check the item where he picked something from		if data.object.prefab == "grass" then			-- We check a similar table to the chance table			local chancetable = {				{"goldnugget", 1.00},				{"goldnugget", 1.00},				{"flint", 0.50},				{"flint", 0.33},			}			-- For each element of chancetable			-- k being keys, v being values			-- As in chancetable[1] = {"goldnugget", 1.00}			-- I didn't define keys, so they are numbers by default			for k, v in pairs(chancetable) do				-- Lua function that gives value between 0-1				-- v is {"goldnugget", 1.00}				-- Therefore v[1] is goldnugget, and v[2] is 1.00				if math.random() <= v[2] then					local loot = SpawnPrefab(v[1])					inst.components.inventory:GiveItem(loot, nil, data.object:GetPosition())				end			end		end	end)end)

For a chance table for picking grass.

Link to comment
Share on other sites

Thanks soo much for the code and even more for the understandable explanation but there's something wrong with the code :/ it crashes with SpawnPrefab a nill value

@RealStarver, you just have to set it up differently.

Example:

AddPrefabPostInit("wolfgang", function(inst)	-- When Wolfgang picks up something	inst:ListenForEvent("picksomething", function(inst, data)		-- We check the item where he picked something from		if data.object.prefab == "grass" then			-- We check a similar table to the chance table			local chancetable = {				{"goldnugget", 1.00},				{"goldnugget", 1.00},				{"flint", 0.50},				{"flint", 0.33},			}			-- For each element of chancetable			-- k being keys, v being values			-- As in chancetable[1] = {"goldnugget", 1.00}			-- I didn't define keys, so they are numbers by default			for k, v in pairs(chancetable) do				-- Lua function that gives value between 0-1				-- v is {"goldnugget", 1.00}				-- Therefore v[1] is goldnugget, and v[2] is 1.00				if math.random() <= v[2] then					local loot = SpawnPrefab(v[1])					inst.components.inventory:GiveItem(loot, nil, data.object:GetPosition())				end			end		end	end)end)

For a chance table for picking grass.

 

Link to comment
Share on other sites

I'm back once again with a similar question so I don't want to make a post for it...if I want to make Melted Mini Glacier (ice_spalsh) pickable and give it loot like you did with the grass prefab would this do the magic?:

AddPrefabPostInit("ice_splash", function(inst)        -- Give it pickable component first        inst:AddComponent ("pickable")	inst:ListenForEvent("picksomething", function(inst, data)		-- We check if the picker is wolfgang		if data.picker.prefab == "wolfgang" then			-- We check a similar table to the chance table			local chancetable = {				{"goldnugget", 1.00},				{"goldnugget", 1.00},				{"flint", 0.50},				{"flint", 0.33},			}			-- For each element of chancetable			-- k being keys, v being values			-- As in chancetable[1] = {"goldnugget", 1.00}			-- I didn't define keys, so they are numbers by default			for k, v in pairs(chancetable) do				-- Lua function that gives value between 0-1				-- v is {"goldnugget", 1.00}				-- Therefore v[1] is goldnugget, and v[2] is 1.00				if math.random() <= v[2] then					local loot = SpawnPrefab(v[1])					inst.components.inventory:GiveItem(loot, nil, data.object:GetPosition())				end			end		end	end)end)

 

@RealStarver, you just have to set it up differently.

Example:

AddPrefabPostInit("wolfgang", function(inst)	-- When Wolfgang picks up something	inst:ListenForEvent("picksomething", function(inst, data)		-- We check the item where he picked something from		if data.object.prefab == "grass" then			-- We check a similar table to the chance table			local chancetable = {				{"goldnugget", 1.00},				{"goldnugget", 1.00},				{"flint", 0.50},				{"flint", 0.33},			}			-- For each element of chancetable			-- k being keys, v being values			-- As in chancetable[1] = {"goldnugget", 1.00}			-- I didn't define keys, so they are numbers by default			for k, v in pairs(chancetable) do				-- Lua function that gives value between 0-1				-- v is {"goldnugget", 1.00}				-- Therefore v[1] is goldnugget, and v[2] is 1.00				if math.random() <= v[2] then					local loot = SpawnPrefab(v[1])					inst.components.inventory:GiveItem(loot, nil, data.object:GetPosition())				end			end		end	end)end)

For a chance table for picking grass.

 

Thank you guys :-)

Edited by RealStarver
Link to comment
Share on other sites

@RealStarver, ice_splash is a FX effect, ice_puddle is also one.

 

The mini melted glacier is the rock_ice prefab, that spawns with the ice_splash and the ice_puddle prefabs. ice_splash appears when the rock_ice prefab is being mined, as it gets told to play the white particle animation. ice_puddle appears with the ice, and it gets the animation where the water moves when the glacier is empty, else, it just looks like a puddle, and you can see only part of it because the huge ice rock is on top of it.

 

You have to give more info on what you want to do here.

Link to comment
Share on other sites

Yeah you're right I tested out some stuff but it turned out those are just fx effects...basically what I'm trying to achieve is when the ice melts and becomes melted glacier during summer and other times the player can gather water from it. I was guessing because you can highlight the melted ice glacier and it has its own string it's an individual prefab but it seems it's just part of the ice_rock animation so I'm not sure now if it's even possible to make it pickable/harvestable 

@RealStarver, ice_splash is a FX effect, ice_puddle is also one.

 

The mini melted glacier is the rock_ice prefab, that spawns with the ice_splash and the ice_puddle prefabs. ice_splash appears when the rock_ice prefab is being mined, as it gets told to play the white particle animation. ice_puddle appears with the ice, and it gets the animation where the water moves when the glacier is empty, else, it just looks like a puddle, and you can see only part of it because the huge ice rock is on top of it.

 

You have to give more info on what you want to do here.

 

Link to comment
Share on other sites

@RealStarver,

Well, I looked into it and I managed to hook into SetWorkable of the workable component.

The glacier, when it melts, calls SetWorkable(false).

 

So I have the pickable component there or not depending on the function call.

 

Result:

AddPrefabPostInit("rock_ice", function(inst)	if GLOBAL.TheWorld.ismastersim then		local old_setw = inst.components.workable.SetWorkable		inst.components.workable.SetWorkable = function(self, able)			if able then				if inst.components.pickable then					inst:RemoveComponent("pickable")				end			else				if not inst.components.pickable then					inst:AddComponent("pickable")					inst.components.pickable:SetUp("ice", 10, 1) -- product, regen in seconds, harvest number				end			end			old_setw(self, able)		end	endend)

Now I'm able to pick 1 ice every 10 seconds from a melted glacier.

Link to comment
Share on other sites

As always you save the day but what does able do? Does able mean "= true" like SetWorkable(true)? also why do you remove the pickable component if it's already pickable? BTW thank you soo much once again :)

@,

Well, I looked into it and I managed to hook into SetWorkable of the workable component.

The glacier, when it melts, calls SetWorkable(false).

 

So I have the pickable component there or not depending on the function call.

 

Result:

AddPrefabPostInit("rock_ice", function(inst)	if GLOBAL.TheWorld.ismastersim then		local old_setw = inst.components.workable.SetWorkable		inst.components.workable.SetWorkable = function(self, able)			if able then				if inst.components.pickable then					inst:RemoveComponent("pickable")				end			else				if not inst.components.pickable then					inst:AddComponent("pickable")					inst.components.pickable:SetUp("ice", 10, 1) -- product, regen in seconds, harvest number				end			end			old_setw(self, able)		end	endend)

Now I'm able to pick 1 ice every 10 seconds from a melted glacier.

 

Link to comment
Share on other sites

As always you save the day but what does able do?

 

SetWorkable(true) means that the glacier will have ice on it, therefore the puddle is blocked. Therefore, no water.

Therefore, I remove the pickable component.

 

Does able mean "= true" like SetWorkable(true)?

 

In lua, a variable can be nil, false, or true and everything else.

In the if expression, nil gets evaluated to false, false to false, everything else is true.

So if able is true, then that means we have ice, so we remove the component.

If able is nil or false, we add the component.

 

also why do you remove the pickable component if it's already pickable?

 

So that I don't have to tinker with the component in order to prevent it from giving stuff when the ice is there.

I just remove the component and it handles its removal nicely.

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