Page 2 of 2

Re: Dead simple threads example

Posted: Wed Sep 07, 2016 12:56 pm
by Rishavs
you are right!
And all this time i used to think that order of functions dont matter! damn i am dumb.

Re: Dead simple threads example

Posted: Wed Sep 07, 2016 6:41 pm
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.

Re: Dead simple threads example

Posted: Mon Sep 12, 2016 7:00 am
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.