The Dink Network

Reply to Dink with Lua scripting

If you don't have an account, just leave the password field blank.
Username:
Password:
Subject:
Antispam: Enter Dink Smallwood's last name (surname) below.
Formatting: :) :( ;( :P ;) :D >( : :s :O evil cat blood
Bold font Italic font hyperlink Code tags
Message:
 
 
December 16th 2013, 03:32 PM
wizardb.gif
Phoenix
Peasant He/Him Norway
Back from the ashes 
Hi, everybody!

Man, I can't believe how active this place still is, for being so old. Well done. I'm also kind of surprised at how active some of the old veteran Dinkers are. Just having looked through some of the latest forum threads, I recognize a lot of usernames from the old days still being present. Hello Kyle, scratcher, magicman and SabreTrout, among others!

For those of you who are now asking yourselves "Who is this guy?" I am Phoenix (real name Alex). I was very active in the Dink community from about 1999 to sometime in the mid to late 2000s, when I more or less lost all interest in anything Dink-related. This place still holds a very special place in my heart, though, and I always find myself coming back. Sooner or later, no matter how long I go, I will probably always be coming back.

So, at this point, you're probably wondering what the title of this post means, and why I'm talking about all this other stuff instead of the topic of the title. Well, I figured that since it has literally been years since I was last here, it was appropriate for me to re-introduce myself. Anyway, let us get to the topic at hand!

First and foremost, in case you don't know; what is Lua? Lua is a scripting language. Not that DinkC isn't, except that Lua is a proper, full-fledged, full-featured scripting language, whereas DinkC is more of a... thrown-together text interpreter doohickey, with a bunch of strange quirks.

Second, why am I talking about Dink and Lua in the same sentence? Well, this is where it gets interesting. You see, I'm currently on vacation, and when I'm on vacation, I tend to get a bit bored. When I get bored, I tend to want to have something interesting to do. In recent years, I've kind of fallen in love with the scripting language Lua. I don't know quite what it is, but I just love that language a lot. One of the greatest things about Lua is that it's more or less intended as a scripting language in other projects. It comes ready to use as a C library, to be integrated into your programming projects at will. Now, having been thinking about Dink recently and having such an affinity for Lua, it was only a matter of time before my mind decided it was time to wed the two ideas. I'm bringing Lua to Dink. Sorta.

I will be adding this feature to my own personal fork of the GNU FreeDink project. This probably means that my code won't ever be part of the official Dink game, but that's all right, I'm not really expecting this to take off or anything. To be honest, I'm doing this more for myself than for any other reason.

Okay, so, what would the consequences of adding Lua to Dink be? There are four major features that personally get me excited, but I'm sure there's more awesomeness that comes along with it. So, here they are:

1) Object orientation

Lua supports object orientation. I will make full use of this. Here's a snippet of how DinkLua code might look (this is Dink's start-1 script converted into Lua):

local crap

function buttonon()
  current_sprite.pframe = 2
  dinkc.playsound(20, 22050, 0, nil, false)
  crap = dinkc.create_sprite(204, 86, 0, 199, 1)
  crap.reverse = false
  crap.noclip = true
  crap.seq = 199
end

function buttonoff()
  current_sprite.pframe = 1
  dinkc.playsound(21, 22050, 0, nil, false)
  crap.reverse = true
  crap.seq = 199
  crap.brain = brain.KILL_SEQ_DONE
end


As you can see, create_sprite returns a sprite object rather than a sprite number, and you can operate on this sprite object in an intuitive way, which is to say, you can set its properties directly, rather than having to call functions with the sprite number as a parameter.

2) Proper variables and variable manipulation

DinkC variables are basically a cheap hack. They can only really store numbers. With Lua, you get variables that can store anything Lua and its Dink integration will support. You can store text, numbers, tables, objects, even function "pointers." I'd elaborate more, but just let that sink in and let your imagination run wild.

local start = "This is"
local middle = " all part of "
local end = "one sentence!"

-- Two dots is the concatenation operator in Lua; it combines two strings into one.
start = start..middle

-- Assume "sprite" here is a sprite object, as mentioned previously.
-- This would have the sprite say "This is all part of one sentence!"
sprite ay(start..end)


3) Real functions, with real parameters and real return values

Yes, indeed. DinkC has a pathetic function support, and in most cases, it's best just left alone. With Lua comes full-fledged function support. Cleaner scripts and helper functions, anyone? Lua functions are also fun in that they allow multiple return values.

4) File includes

Helper functions would be kind of useless if you'd have to put them over and over again in every file. So, an obvious addition is to add support for file includes. Typical usage would go something like this:

local helpers = include("helpers")

function main()
  heart = helpers.create_heart(345, 123) -- x and y coords, e.g.
end


Awesome, right? So, how close am I to completion? Well, before writing this post, I made sure to implement a proof-of-concept version, just to make sure I could accomplish this project. It went superbly, so now I've started the more elaborate and daunting task of adding Lua support proper. I'm currently rewriting the Dink scripting system in such a way that DinkC is just another scripting language to the Dink engine. In fact, if you wanted to, after I'm done, you could compile my version of Dink without DinkC support. Not sure that's ever a good idea, but that's how detached I want DinkC to be. Why? Because for one, it will make the Lua integration cleaner (and to be honest, the DinkC integration will become cleaner too), and second, because it will make adding any scripting language to Dink equally clean (and a lot easier).

I'm mostly writing this post to have some accountability on me, in such a way that I have sort of promised to do this to someone, and can't back out as easily. Feel free to come with reactions, comments, questions, what have you. I will make sure to return to this post frequently to reply to them.