Log In  


Cart [#35123#] | Copy | Code | 2017-01-06 | Link

Dialogue Text Box Library (DTB)

DTB is a code snippet that you can use to implement a dialogue text box to any game in just seconds.
Setting it up goes as follows:

  • Add the dtb code to the top of your own code.
  • Call dtb_init in your initialization.
  • Call dtb_update in your update.
  • Call dtb_draw function in your draw.
    And that all there is! You can now simply call "dtb_disp" wherever you want.



Call this to initialize all dtb's variables. Takes an optional parameter which defines the maximum number of lines that will be displayed. Defaults to 3.


Call this every update. All logic is contained within dtb.


Call this every time the screen gets drawn. Preferably at the end so the textbox always appears at the top.

dtb_disp(txt, callback)

Call this to display txt in a textbox. The text is added to a queu so this function can be called multiple times after each other without interfering with the previous text.
Takes an optional callback parameter (function) which is called when this particular piece of text is done displaying.

The actual snippet

Here is the minimized snippet. 1370 characters and 447 tokens:

-- dialogue text box library by oli414. minimized.
function dtb_init(n) dtb_q={}dtb_f={}dtb_n=3 if n then dtb_n=n end _dtb_c() end function dtb_disp(t,c)local s,l,w,h,u s={}l=""w=""h=""u=function()if #w+#l>29 then add(s,l)l=""end l=l..w w=""end for i=1,#t do h=sub(t,i,i)w=w..h if h==" "then u()elseif #w>28 then w=w.."-"u()end end u()if l~=""then add(s,l)end add(dtb_q,s)if c==nil then c=0 end add(dtb_f,c)end function _dtb_c()dtb_d={}for i=1,dtb_n do add(dtb_d,"")end dtb_c=0 dtb_l=0 end function _dtb_l()dtb_c+=1 for i=1,#dtb_d-1 do dtb_d[i]=dtb_d[i+1]end dtb_d[#dtb_d]=""sfx(2)end function dtb_update()if #dtb_q>0 then if dtb_c==0 then dtb_c=1 end local z,x,q,c z=#dtb_d x=dtb_q[1]q=#dtb_d[z]c=q>=#x[dtb_c]if c and dtb_c>=#x then if btnp(4) then if dtb_f[1]~=0 then dtb_f[1]()end del(dtb_f,dtb_f[1])del(dtb_q,dtb_q[1])_dtb_c()sfx(2)return end elseif dtb_c>0 then dtb_l-=1 if not c then if dtb_l<=0 then local v,h v=q+1 h=sub(x[dtb_c],v,v)dtb_l=1 if h~=" " then sfx(0)end if h=="." then dtb_l=6 end dtb_d[z]=dtb_d[z]..h end if btnp(4) then dtb_d[z]=x[dtb_c]end else if btnp(4) then _dtb_l()end end end end end function dtb_draw()if #dtb_q>0 then local z,o z=#dtb_d o=0 if dtb_c<z then o=z-dtb_c end rectfill(2,125-z*8,125,125,0)if dtb_c>0 and #dtb_d[#dtb_d]==#dtb_q[1][dtb_c] then print("\x8e",118,120,1)end for i=1,z do print(dtb_d[i],4,i*8+119-(z+o)*8,7)end end end

A readable/editable version can be found in this demo's code.

Feel free to use and customize this code in your own projects! I'd love to see it being used!

library dialogue textbox rpg code snippet
P#35126 2017-01-06 19:00


Very nice! I might try using this is in my next adventure game.

P#35625 2017-01-13 20:11


Excellent, great job! Any plans to implement scrolling via up/down on dialogs that end up longer than 3 lines?

P#35646 2017-01-14 02:13


@Scathe that's a neat idea! I'm struggling a little with adding more features tho. It rapidly increases the number of tokens even though some people might not need/require all of the features.

P#35845 2017-01-16 06:34


@oli414 I'll be using this in one of my carts. Thank you so much!

P#35866 2017-01-16 16:15


Is there a way to implement this on my #tinytvjam to display directions?

P#35927 2017-01-17 13:48


@JetJaguar not really, you only have 10x11 pixels to work with in the useable draw routine for the jam; anything bigger than that is basically drawn over by the uneditable code.

P#35941 2017-01-17 15:55


Appreciate the insight, @2Tie

P#35976 2017-01-18 00:06


This looks really solid. I've been dreading tacking dialogue in my current project with my dwindling token budget. Totally going to use this :)

P#35985 2017-01-18 03:58


@HotSoup Awesome! Looking forward to it!

@JetJaguar Like 2Tie said, there's just too little space to make it work I think.

@jade Thanks! Feel free

P#36003 2017-01-18 08:23


Wow that's really nice and small!

P#36461 2017-01-23 07:28


@Oli414 This is amazing! I've also added in a thing when it changes the text sound effect depending on what you set the variable 'mood' to.

P#41990 2017-06-29 00:56


Goodness this is sexy! I'll definitely be using this in my game. I was dreading refactoring my own text box library but it looks like I can cut that todo item now!

P#42398 2017-07-12 10:30

Log in to post a comment


New User | Account Help
:: New User
About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2017-11-22 12:57 | 0.369s | 1835k | Q:29