Dead simple threads example

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.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: Dead simple threads example

Post by Rishavs »

you are right!
And all this time i used to think that order of functions dont matter! damn i am dumb.
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: Dead simple threads example

Post by pgimeno »

Rishavs wrote:Ok. so when i put the round function before the for loop in my thread file, it works.
But this is the first time i have seen anything like it. In my other scripts I can use function calls above the function definitions.
I think i am missing something very basic to lua. :roll:
If you try to use something before you define it, it won't be defined at the time you use it.

That applies to your first snippet. Had you put it like this, it would have worked:

Code: Select all

function doit()
    local smin = 1
    local smax = 1000
    channel = love.thread.getChannel("test")
    for i = smin, smax, 1 do
        print(i .. " > ALL WORK AND NO PLAY MAKES JACK A DULL BOY")
        channel:push(round(i * 100 / smax))
    end
end

function round(num, dp)
    local mult = 10^(dp or 0)
    return (math.floor(num * mult + 0.5)) / mult
end

doit()
There's a difference between when you define the function and when you use it. In your case, normal program execution reaches a call to round before reaching its definition, and that's an error. But when written in the above way, it reaches the definition of doit before the definition of round but that's OK, because at definition time, the runtime just needs to know that it must call a function called round, even if it's not defined yet. It's when the call is executed that it matters.

Hope I've made sense.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: Dead simple threads example

Post by Rishavs »

Thanks pgimeno.
For some reasons i had forgotten forward declaration is a thing. :cry:

Now, for someone who might come in after me and read this thread;
what i wanted to do was to render something in the draw call without the logic bit making the window unresponsive.

threads is obviously one way of doing this.
Another is coroutines. I managed to make the same example work with coroutines.

I have updated the OP post with all the relevant code.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], CPop and 46 guests