Food and consume_effect

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
kaypy
Artisan
Posts: 187
Joined: Sun Jan 19, 2014 7:11 am

Food and consume_effect

Post by kaypy »

Between system/sys_click_mouth, util/eatdrink and the consume_effect on a given object, there is a bit of a mismatch.

The consume_effect is called with the id of the object being consumed. But because food has an animation and thus a delay, the object is no longer valid by the time its consume_effect is called.

I have a tweaked version that uses the stop_con return to prevent the food item being do_convert-ed, then manually handles the do_convert after the delay and consume_effect.

Code: Select all

function eatdrink(self, id, who)
	local delay = false

	if (self.foodval) then
		inventory_info.mouth.icon = gfx.mouth_chewing
		dsb_update_inventory_info()
		dsb_hide_mouse()
		dsb_delay_func(1, function() dsb_lock_game() end)	
		dsb_delay_func(4, function ()
			dsb_show_mouse()
			dsb_sound(base_eat_sound)
			inventory_info.mouth.icon = base_mouth_icon
			dsb_update_inventory_info()
			dsb_set_food(who, dsb_get_food(who) + self.foodval)
			
			if (self.waterval) then
				dsb_set_water(who, dsb_get_water(who) + self.waterval)
			end
			
			if (self.consume_effect) then
				self:consume_effect(id, who)
			end
			
			-- manually apply conversion
			local arch = dsb_find_arch(id)
			do_convert(arch, id, "consume")
			
			dsb_unlock_game()
		end)
		
		delay = true
	
	elseif (self.waterval) then
		dsb_set_water(who, dsb_get_water(who) + self.waterval)
	end

	if (not delay) then
		if (self.consume_effect) then
			self:consume_effect(id, who)
		end
		dsb_sound(base_eat_sound)
	end
	
	if delay then
		-- dont do_convert until after delay
		return true
	end
end
Friends don't let friends eat worm round
User avatar
Sophia
Concise and Honest
Posts: 4306
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Food and consume_effect

Post by Sophia »

Makes sense. I'm not going to change "official" DSB because it has been this way for years and nobody complained, so there is probably code somewhere that actually relies on this weird behavior... because there always is. Thanks for pointing it out, though.
Post Reply