Page 1 of 1

util.lua -> spawn_in_pack

Posted: Sun Jul 13, 2008 5:03 pm
by ian_scho
Not sure if this is a bug but...

I'm using your useful spawn_in_pack function in util.lua to add an item to my backpack. I thought I'd test it for when we have the case that the back pack is already full and get the following exception.

Code: Select all

LUA ERROR: base/util.lua:17: dsb_fetch: Invalid inventory index 30
FATAL LUA ERROR: Lua Function FLAME: base/util.lua:21: dsb_spawn: Invalid inventory index 30
Here's your code:

Code: Select all

function spawn_in_pack(arch, whose_pack)
	local slot = INV_PACK
	         
	while(dsb_fetch(CHARACTER, whose_pack, slot, 0)) do
		slot = slot + 1
	end
	
	return (dsb_spawn(arch, CHARACTER, whose_pack, slot, 0))
end
I overloaded your function in my own code, thus harmlessly returning a null value if there is no space:

Code: Select all

function spawn_in_pack(arch, whose_pack)
	local slot = INV_PACK

	while(dsb_fetch(CHARACTER, whose_pack, slot, 0)) do
		slot = slot + 1
		
		-- Backpack full?  30 Slots by default
		if (slot >= MAX_INV_SLOTS) then return nil end
	end

	return (dsb_spawn(arch, CHARACTER, whose_pack, slot, 0))
end

Posted: Sun Jul 13, 2008 7:26 pm
by Sophia
ian_scho wrote:I overloaded your function in my own code, thus harmlessly returning a null value if there is no space:
That's only harmless if the code isn't expecting to do anything with the returned value. I think it should probably error, though perhaps not a fatal error. :)

Posted: Sun Jul 13, 2008 10:35 pm
by ian_scho
I think it was only intended as a helper function for creating champions at the dungeon start, and Ive never seen one with a full inventory!