Extending default objects?

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
Gambit37
Should eat more pies
Posts: 13769
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Extending default objects?

Post by Gambit37 »

I'm not sure I understand how to extend default objects with some new properties.

Let's say I wanted to add an inside_gfx for all the default objects, and create a subrenderer to display that image, rather than the usual information.

Can I add something like this to my own objects.lua and it will simply extend the base object?

Code: Select all

obj.dagger = {
    inside_gfx=gfx.dagger_large_view,
    subrenderer = inside_gfx_subrenderer,
}
Or do I instead need to do something like this:

Code: Select all

obj.dagger.inside_gfx = gfx.dagger_large_view,
obj.dagger.subrenderer = inside_gfx_subrenderer
I guess my generic subrenderer to render these images would be as simple as this:?

Code: Select all

function inside_gfx_subrenderer(arch, id)
	local sr = dsb_subrenderer_target()
	dsb_bitmap_draw(arch.inside_gfx, sr, 0, 0, false)
end
User avatar
Gambit37
Should eat more pies
Posts: 13769
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Extending default objects?

Post by Gambit37 »

Well, I tried it out and got the results I wanted using the second approach. Clearly the first option re-declares the whole object which DSB doesn't seem to like... ;-)
User avatar
Sophia
Concise and Honest
Posts: 4306
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Extending default objects?

Post by Sophia »

Yes. When you define a table using { }, you are creating a new table. This means that anything formerly in the table will be replaced.
So, to extend a dagger, you need to add the properties, as you have noticed. :)

Note that if you really want to add a subrenderer to all the default archs, it might work better to put something like this at near the bottom of your objects.lua.

Code: Select all

for archid in pairs(obj) do
   local arch = obj[archid]
   if (arch.type == "THING") then
      if (gfx[archid .. "_large_view"]) then
         arch.inside_gfx = gfx[archid .. "_large_view"]
         arch.subrenderer = inside_gfx_subrenderer
      end
   end
end
What this code does is iterate over everything in obj and, if it's a THING, check for an entry in gfx with the name of the arch with "_large_view" appended, e.g., for "dagger" it will look for gfx.dagger_large_view, for "genericthing" it will look for gfx.genericthing_large_view, and so on. If it finds this graphic, it sets up the subrenderer automatically.

Note: I didn't test this code, so if some weird bug happens, I probably made a typo. 8)
User avatar
Gambit37
Should eat more pies
Posts: 13769
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Extending default objects?

Post by Gambit37 »

Wooty, even cooler, thank you -- I'll try that out. I'm tinkering with bigger images for items as I find the 32x32 icons a bit small. Dunno if I'll do it for all items as obviously it's a lot of work to draw the larger images, but it's nice to experiment anyway... :-)

Where's the code for the normal information subrenderer when you hold an item to the eye? I'd like to play with using a larger image and laying some info text over the top...
EDIT: Don't worry, I found sys_render_object in render.lua.
Post Reply