util.lua -> spawn_in_pack

This forum is for the Lua scriptable clone of DM/CSB called Dungeon Strikes Back by Sophia. Use DSB to build your own highly customised games.

Moderator: Sophia

Forum rules
Please read the Forum rules and policies before posting. You may Image to help finance the hosting costs of this forum.
Post Reply
User avatar
ian_scho
High Lord
Posts: 2807
Joined: Fri Apr 07, 2006 8:30 am
Location: Zaragoza, Spain

util.lua -> spawn_in_pack

Post 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
User avatar
Sophia
Concise and Honest
Posts: 4307
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Post 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. :)
User avatar
ian_scho
High Lord
Posts: 2807
Joined: Fri Apr 07, 2006 8:30 am
Location: Zaragoza, Spain

Post 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!
Post Reply