Unexpected behaviour when time is >= 10

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
User avatar
1u4
Prole
Posts: 16
Joined: Tue May 01, 2012 6:27 am
Location: Oh no, not Finland!

Unexpected behaviour when time is >= 10

Post by 1u4 »

My code works fine, until the number in [start <number>] (in text = ) is 10 or more. In the example below, it displays the first string, "string 1", then at 5 seconds displays the string, "string 3" instead of "string 2". If you change the last start number in "text =" to less than 10, the code works as expected.

I have no idea why this is. Anyone?

Oh, and please feel free to criticize the way I've gone about coding this. I'm as equally new to lua as I am löve and am generally a rubbish programmer :P

Thanks :)

(Change [start 10.0] to a lower number to see it working correctly).

Code: Select all

function love.load()
    -- some text to try...
    text = "Sample text [start 2.0]string 1[end 4.5] this should not be visible [start 5.0]string 2 [end 7.0]more ignored text [start 10.0]string 3.[end 12.5]"
    audioSource = love.audio.newSource("audioFile.mp3")
    love.audio.play(audioSource) 
    playing = true

    --break up the text in the file and inset into categorised table arrays.
    startsArray = {}
    endsArray = {}
    wordsArray = {}
    for starts, words, ends in text:gmatch("%[start%s([%d%.]+)%](.-)%[end%s([%d%.]+)%]") do
        table.insert(startsArray, starts)
        table.insert(endsArray, ends)
        table.insert(wordsArray, words)
    end
    
    index = 1 -- this is the start of the table array and a base for incrementation.
    textCall = "Easy Video Language"
    font = love.graphics.newFont(75)
    love.graphics.setFont(font)
end

--

function love.keypressed(key)    
    if key == "escape" then 
        love.event.push("quit") 
    end
end

--

function love.update(dt)
    position = audioSource:tell( "seconds" )

    if index < 4 then
            if string.format("%f", position) > startsArray[index] then
            textCall = tostring(wordsArray[index])
            print(startsArray[index] .. " | " .. wordsArray[index] .. " | " .. string.format("%f", position) )
            index = index + 1
        end
    end
end

--

function love.draw()
    love.graphics.printf(textCall, 10, 75, 800, 'center') -- Default title -- set in love.load()
    love.graphics.printf(position, 10, 0, 800, 'center') -- check text is arriving on time.
end
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Unexpected behaviour when time is >= 10

Post by Boolsheet »

You compare strings where you want to compare numbers. "10.0" > "0.5" is false.
Use tonumber() to convert a string into a number.
Last edited by Boolsheet on Fri May 18, 2012 4:42 pm, edited 1 time in total.
Shallow indentations.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Unexpected behaviour when time is >= 10

Post by Kadoba »

Yeah when you compare strings then you are comparing their alphabetical order even if they display as a number ("10.0" > "0.5" is actually true since 1 comes after 0). You can use strings as numbers in expressions but even that isn't a good idea because it can cause some confusion.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Unexpected behaviour when time is >= 10

Post by Boolsheet »

Kadoba wrote:("10.0" > "0.5" is actually true since 1 comes after 0)
Oops. I'm using my Lua wrong.
Shallow indentations.
User avatar
1u4
Prole
Posts: 16
Joined: Tue May 01, 2012 6:27 am
Location: Oh no, not Finland!

Re: Unexpected behaviour when time is >= 10

Post by 1u4 »

Thank folks. I did that fix, made a few improvements and attached a copy with a working example -- if you are interested. It's still far from complete though.
Attachments
easy-video-language-example.love
(5.15 MiB) Downloaded 156 times
There is a story of a farmer whose horse ran away. That evening the neighbors gathered to commiserate with him since this was such bad luck.
He said, "May be." Continued...
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 13 guests