Jump to content

Removing a loot of the list ?


Recommended Posts

Hi,


I would like to replace the "monster meat" drop for the spider by a "small monster meat", but it will implie first to remove the "monster meat" of the lootdropper list. How can i do this ?


    inst:AddComponent("lootdropper")
    inst.components.lootdropper:AddRandomLoot("monstermeat", 1)
    inst.components.lootdropper:AddRandomLoot("silk", .5)
    inst.components.lootdropper:AddRandomLoot("spidergland", .5)
    inst.components.lootdropper:AddRandomHauntedLoot("spidergland", 1)
    inst.components.lootdropper.numrandomloot = 1

 

Link to comment
Share on other sites

with AddPrefabPostInit for spider, try this:
 

for i,entry in pairs(GLOBAL.deepcopy(inst.components.lootdropper.randomloot)) do
    if entry.prefab=="monstermeat" then
         table.remove(inst.components.lootdropper.randomloot, entry)
         inst.components.lootdropper.totalrandomweight = inst.components.lootdropper.totalrandomweight - entry.weight
    end
end

 

Link to comment
Share on other sites

I got this error, did i do something wrong ?


[00:05:07]: error calling PrefabPostInit: spider in mod SummerFloraFauna: 
[string "../mods/SummerFloraFauna/scripts/postinit.l..."]:213: bad argument #2 to 'remove' (number expected, got table)
LUA ERROR stack traceback:
        =[C] in function 'remove'
        ../mods/SummerFloraFauna/scripts/postinit.lua(213,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        scripts/mods.lua(131,1) in function 'mod'
        scripts/mainfunctions.lua(155,1)
        =[C] in function 'SpawnPrefab'
        scripts/mainfunctions.lua(191,1) in function 'SpawnPrefab'
        scripts/util.lua(24,1) in function 'DebugSpawn'
        scripts/consolecommands.lua(159,1) in function 'c_spawn'
        c_spawn("spider", 50)(1,1) in main chunk
        =[C] in function 'pcall'
        scripts/mainfunctions.lua(1334,1)	
[00:05:07]: Disabling SummerFloraFauna because it had an error.	

 

The line in question :

 

         table.remove(inst.components.lootdropper.randomloot, entry)

 

Link to comment
Share on other sites

So it doesn't crash anymore, but it doesn't work either. Spiders still drop meat.


local function SpiderPostInit( inst )
        print("step 1.")


for i,entry in pairs(GLOBAL.deepcopy(inst.components.lootdropper.randomloot)) do
    if entry.prefab=="monstermeat" then
        print("step 2.")
	GLOBAL.table.removetablevalue(inst.components.lootdropper.randomloot, entry)
	inst.components.lootdropper.totalrandomweight = inst.components.lootdropper.totalrandomweight - entry.weight
    end
end

    if inst.components.lootdropper then
        print("step 3.")
    inst.components.lootdropper:AddRandomLoot("log", 1)
    end
 
    return inst
end

AddPrefabPostInit( "spider", SpiderPostInit) 

 

[00:01:09]: step 1.	
[00:01:09]: step 2.	
[00:01:09]: step 3.	

So at least something is working, but not the line removing the meat or the add of the loot.

(I used log for testing purpose)

Link to comment
Share on other sites

local lootdropper = inst.components.lootdropper

local key
local weight
for i, v in ipairs(lootdropper.randomloot) do
	if v.prefab == "monstermeat" then
		key = i
		weight = v.weight
		break
	end
end

table.remove(lootdropper.randomloot, key)
lootdropper.totalrandomweight = lootdropper.totalrandomweight - weight

Use table.remove and give it the key, not the entry.

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