(fixed) moneybox

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.
Post Reply
User avatar
Parallax
DMwiki contributor
Posts: 424
Joined: Mon Aug 28, 2006 7:56 pm
Location: Back in New Jersey

(fixed) moneybox

Post by Parallax »

The code for the money box is faulty. I mean, it works as long as the item is called "moneybox" in objects.lua, something that ROOT_NAME tries to take care of, unsuccesfully. Change "moneybox" to "banana" in dsb_import_arch("moneybox.lua", "moneybox") (and the dsb_spawn in dungeon.lua, of course) and it all goes down.

I've found one fix:
in moneybox.lua, change obj[ROOT_NAME] to the following:

Code: Select all

obj[ROOT_NAME] = {
	name="MONEY BOX",
	type="THING",
	class="CONTAINER",
	mass=11,
	icon=gfx[ROOT_NAME .. "_icon"],              -- Change here
	alt_icon=gfx[ROOT_NAME .. "_alticon"],    -- and here
	dungeon=gfx[ROOT_NAME],                 -- and here
	
	msg_handler = {
		[M_MONEYBOX] = moneybox_click,
		[M_DESTROY] = self_destruct
	},
	
	zone_obj = {
		"gem_blue",
		"gem_orange",
		"gem_green",
		"coin_gold",
		"coin_silver",
		"coin_copper"
	},
	
	subrenderer = moneybox_subrenderer,
	
	to_r_hand = alticon,
	from_r_hand = normicon,
	max_throw_power=30
}
You can also change gfx.moneybox.inside to gfx[ROOT_NAME .. "_inside"] in function moneybox_subrenderer.

This will let the moneybox compile even if the dungeon designer happened to call the "moneybox" something else. Unfortunately, DSB will still crash when you pick up the moneybox as ROOT_NAME is not saved past compilation and that modified line in function moneybox_subrenderer is now trying to concatenate a nil value, something that Lua looks down upon.

I have not yet found a fix for that.
Remy
Craftsman
Posts: 111
Joined: Wed Sep 05, 2007 5:24 pm
Contact:

Post by Remy »

There are a couple of ways to handle this. One is to declare a global to contain whatever 'ROOT_NAME' the designer designates, so that it persists beyond the compilation.
For example, this near the top:

Code: Select all

moneybox_base_name = ROOT_NAME
And then you can use 'moneybox_base_name' where you'd normally use ROOT_NAME. The problem is that 'moneybox_base_name' is now a global variable, and could potentially be changed accidentally by other dsb_import_arch unless the arch-designer uses a very specific name.
The other solution is to contain all the neccessary graphics, sounds, and other resources within the obj table itself. Like this:

Code: Select all

obj[ROOT_NAME] = { 
   name="MONEY BOX", 
   type="THING", 
   class="CONTAINER", 
   mass=11, 
   icon=gfx[ROOT_NAME .. "_icon"],              -- Parallax Change here 
   alt_icon=gfx[ROOT_NAME .. "_alticon"],    -- and here 
   dungeon=gfx[ROOT_NAME],                 -- and here 
   inside_gfx = gfx[ROOT_NAME.."_inside"], --Remy's change here
    
   msg_handler = { 
      [M_MONEYBOX] = moneybox_click, 
      [M_DESTROY] = self_destruct 
   }, 
    
   zone_obj = { 
      "gem_blue", 
      "gem_orange", 
      "gem_green", 
      "coin_gold", 
      "coin_silver", 
      "coin_copper" 
   }, 
And then when you need to access the "inside" graphic, use 'obj[arch].inside_gfx' (for the specific problem with moneybox's subrenderer, you'd just use 'self.inside_gfx'), instead of the wierd code with 'ROOT_NAME'. I prefer this way, since it encapsulates all you need for the arch in one place, and doesn't crowd up the global namespace with potential places to cause problems.
User avatar
Parallax
DMwiki contributor
Posts: 424
Joined: Mon Aug 28, 2006 7:56 pm
Location: Back in New Jersey

Post by Parallax »

Yes, I just tested your second solution and it works. I like it over the first one on principle as well, it's all self-contained and there is no global variable declared that disn't need declaring. Thanks!
Post Reply