Does Text Sub Work in JIT?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ekamu
Party member
Posts: 178
Joined: Fri Sep 20, 2013 11:06 am

Does Text Sub Work in JIT?

Post by Ekamu »

Hi this is a code experiment to draw text one character at a time using dt.

I am using concatenation and the text library.

It does not work anymore like with 5.1, Im thinking they switched the text library.

All libraries are in C right?

Code: Select all

function love.load()
  text = {
    ['t'] = 0,
    ['c'] = 0,
    ['n'] = 0,
    ['speed'] = 10,
    ['max'] = 217,
    ['line'] = 31,
    ['msg'] = "Hello World!",
    ['cat'] = "",
    }
end

function love.update(dt)
     function text_loop(dt)--draw text one at a time
     text['t'] = text['t'] + (text['speed']*dt)
     if text['t'] > 0.2 then-- 0.2 seconds.
     text['t'] = 0--add the letter      
     if text['cat']:len() <= text['max'] and text['cat']:len() <= text['msg']:len() then
     text['cat'] = text['cat']..text['msg'].sub(text['msg'],text['c'],text['c']) 
     text['c'] = text['c'] + 1 --next letter (text count)
     text['n'] = text['n'] + 1 --next letter (line count)
     if text['n'] == text['line'] then --end of line
     text['cat'] = text['cat'].."\n" --draw on new line
     text['n'] = 0
     end
     end
     end
     end
end

function love.draw()
     love.graphics.print(text['cat'],5,420)
end
The code has no errors. The text just does not draw at all.

Im thinking its because of some syntax sugar referencing the table library... Not yet JIT eh with len and sub statements?
User avatar
artofwork
Citizen
Posts: 91
Joined: Mon Sep 15, 2014 1:17 am
Location: East Coast USA

Re: Does Text Sub Work in JIT?

Post by artofwork »

It doesn't work because you are defining the function definition inside of love.update
You need to define the function outside of love or any other function & then you can call it inside of a function.
Now it works :)

Code: Select all

function love.load()
  text = {
    ['t'] = 0,
    ['c'] = 0,
    ['n'] = 0,
    ['speed'] = 10,
    ['max'] = 217,
    ['line'] = 31,
    ['msg'] = "Hello World!",
    ['cat'] = "",
    }
end

function love.update(dt)
     text_loop(dt) -- then we can call it here
end

function love.draw()
    love.graphics.print(text['cat'],5,420)
end
-- needs to be defined outside of love's or any other functions
function text_loop(dt)--draw text one at a time
	text['t'] = text['t'] + (text['speed']*dt)
	if text['t'] > 0.2 then-- 0.2 seconds.
		text['t'] = 0--add the letter      
		if text['cat']:len() <= text['max'] and text['cat']:len() <= text['msg']:len() then
			text['cat'] = text['cat']..text['msg'].sub(text['msg'],text['c'],text['c']) 
			text['c'] = text['c'] + 1 --next letter (text count)
			text['n'] = text['n'] + 1 --next letter (line count)
			if text['n'] == text['line'] then --end of line
				text['cat'] = text['cat'].."\n" --draw on new line
				text['n'] = 0
			end
		end
	end
end
As always a working love file :)
main.love
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 97 guests