Page 1 of 1

Does Text Sub Work in JIT?

Posted: Wed Nov 19, 2014 4:31 am
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?

Re: Does Text Sub Work in JIT?

Posted: Wed Nov 19, 2014 6:33 am
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