Can't understand the principle of Thread and Channel work
Posted: Fri Sep 15, 2017 7:23 am
Hello, guys! Obviously, i'm new to lua language and game development, so i'm trying to get understanding of the basics before doing something more specific. And so far i've got stucked in the thread and channels, and here comes the problems:
First of all, should "thread:start()" be included in "love.update" function to work?
Second, i'm trying to boot thread on main file load, send variable to him; in the thread itself this variable increases, sends back to the main file and displays on 300,300 point... And its simply not working. Can you guys explain me where am i did mistake?
The main file:
Thread:
-- sending variable back
First of all, should "thread:start()" be included in "love.update" function to work?
Second, i'm trying to boot thread on main file load, send variable to him; in the thread itself this variable increases, sends back to the main file and displays on 300,300 point... And its simply not working. Can you guys explain me where am i did mistake?
The main file:
Code: Select all
local e = 0 -- time limitation variable
function love.load()
vr = 5 -- variable that sends in the thread
thread1 = love.thread.newThread("menu.lua") -- creating thread
channel = love.thread.newChannel("channel") -- creating channel (?)
thread1:start() -- starting thread
channel:push(vr) -- sending variable to the thread (?????)
vr = channel:pop() -- receive increased variable from the thread (????)
end
function love.update(dt)
end
function love.draw(dt)
love.graphics.print(tostring(vr), 300,300) -- draw increased variable on 300.300
end
function love.threaderror(t, e) -- error-capturing function i've found on the internet
return error(e)
end
Code: Select all
require("love") -- including all modules from love (????)
local channel = love.thread.getChannel("channel") -- receive channel from main file
local d = 0 -- variable that will hold sended number
d = channel:pop() -- getting variable from channel (????) after "channel:push(vr)" in the main file (????)
if d == nil then -- d got nothing so without this block love'll give me an error
d = 393
end
local e = 15 + tonumber(d) -- variable that sends back to the main file
channel:push(e)