Query about Lua programming in DSB

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

Query about Lua programming in DSB

Post by Gambit37 »

There's something I don't understand about functions and parameters.

I was trying out the code to make a full screen scroll, and this works fine:

Code: Select all

function render_large_scroll(self, id, whose_eye)
	if (exvar[id] and exvar[id].textID) then
		scroll_ID = exvar[id].textID
		dsb_fullscreen(show_large_scroll_and_text, EXIT_ON_CLICK, nil, true, false)
	end
end

scroll_strings = {
	[1] = "Scroll 1 Text",
	[2] = "Scroll 2 Text",
	[3] = "Scroll 3 Text"
}

obj.scroll_large = clone_arch(obj.scroll, {
	name="LARGE SCROLL"
	on_look=render_large_scroll
})

function show_large_scroll_and_text(bmp)
	dsb_bitmap_textout(bmp, gfx.font_colortest, string.upper(scroll_strings[scroll_ID]), 320, 240, CENTER, colour_white)
end
But I also noticed I could do it a slightly different way -- when cloning the scroll, don't assign the on_look method to a function, and instead do it like this:

Code: Select all

function obj.scroll_large:on_look(self, id, whose_eye)
	scroll_ID = exvar[self].textID
	dsb_fullscreen(show_large_scroll_and_text, EXIT_ON_CLICK, nil, true, false)
end

scroll_strings = {
	[1] = "Scroll 1 Text",
	[2] = "Scroll 2 Text",
	[3] = "Scroll 3 Text"
}

obj.scroll_large = clone_arch(obj.scroll, {
	name="LARGE SCROLL"
})

function show_large_scroll_and_text(bmp)
	dsb_bitmap_textout(bmp, gfx.font_colortest, string.upper(scroll_strings[scroll_ID]), 320, 240, CENTER, colour_white)
end
Why do I use exvar[id] in the first example, but exvar[self] in the second? What's the difference? Is one of these methods better than the other? I'm demonstrating that I don't really know what I'm doing and just fiddle around until I get what I want, but I'd like to understand better what's the difference between these two examples.
User avatar
Sophia
Concise and Honest
Posts: 4239
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Query about Lua programming in DSB

Post by Sophia »

Gambit37 wrote:Why do I use exvar[id] in the first example, but exvar[self] in the second?
Because you did it wrong. :P

The idea behind using the colon is that it's trying to simulate object-oriented programming by assuming a hidden first parameter containing the table that the function is part of, which Lua names self. So, if you're using a colon, you don't need a self parameter because Lua already creates one for you.

As such, this is the proper way to write the second version:

Code: Select all

function obj.scroll_large:on_look(id, whose_eye)
   scroll_ID = exvar[id].textID
   dsb_fullscreen(show_large_scroll_and_text, EXIT_ON_CLICK, nil, true, false)
end
self still exists, but it's created automatically. You don't need it in your parameter list.
User avatar
Gambit37
Should eat more pies
Posts: 13714
Joined: Wed May 31, 2000 1:57 pm
Location: Location, Location
Contact:

Re: Query about Lua programming in DSB

Post by Gambit37 »

"Because you did it wrong" makes sense to me. The sentence after that goes right over my head though. ;-) But thanks for showing the right way to do it :-)
User avatar
Sophia
Concise and Honest
Posts: 4239
Joined: Thu Sep 12, 2002 9:50 pm
Location: Nowhere in particular
Contact:

Re: Query about Lua programming in DSB

Post by Sophia »

By declaring the function using a colon instead of a dot, you're telling Lua to add self to the parameter list automatically.

These declarations are identical:

Code: Select all

function mytable.myfunction(self, blah)
-- do stuff
end

function mytable:myfunction(blah)
-- do stuff
end
Post Reply