[love.thread] Communication between thread and main.lua

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
freedmo
Prole
Posts: 2
Joined: Sun Jul 15, 2012 2:27 pm

[love.thread] Communication between thread and main.lua

Post by freedmo »

You can send data from one thread to the main program?

Following the example from here: viewtopic.php?f=5&t=9934.
It is no problem to get data from a thread.

But I want to send data from the "main.lua" to the thread.
If this is possible, if so how?

Thank you for your help.

freedmo
Last edited by freedmo on Tue Jan 15, 2013 5:09 pm, edited 1 time in total.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: [love.thread] Communication between thread and main.lua

Post by Nixola »

The main thread is called 'main'; by getting the thread (main_thread = love.thread.getThread()) inside main.lua (or anything inside that thread) you can then set a message there; by getting the 'main' thread from another thread (main_thread = love.thread.getThread('main')) you can then get the message. Example:

main.lua:

Code: Select all

self = love.thread.getThread()
thread = love.thread.newThread('thread', 'thread.lua')
thread:start()
self:set('some var', 'something')
thread.lua:

Code: Select all

self = love.thread.getThread()
main = love.thread.getThread('main')
while true do
   if main:get('some var') == 'something' then
      --well, just do something
   end
   love.timer.speel(.001)
end
I didn't test this yet but it should work

(P.S: I did something similar, anyway I was able to send data from the main thread to another with this method)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: [love.thread] Communication between thread and main.lua

Post by Germanunkol »

You can also do it the other way around:

Start the thread somewhere in the main thread:

Code: Select all

myThread = love.thread.newThread('thread', 'thread.lua')
myThread:start()
myThread:set('some var', 'something')
And inside the thread, do:

Code: Select all

self = love.thread.getThread()
self:set("myVar", "hello from inside the thread")
and again in the main thread, do:

Code: Select all

msg = myThread:get("myVar")

I prefer this way, because I find it cleaner and - more importantly - because this way you can start many threads in the main thread and they can all communicate with the main thread without interfering with each other. If you'd use the main thread for getting/setting values, then all the threads would use the same variables in the set and get methods, which might get complicated and crowded.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
freedmo
Prole
Posts: 2
Joined: Sun Jul 15, 2012 2:27 pm

Re: [love.thread] Communication between thread and main.lua

Post by freedmo »

Nixola wrote:The main thread is called 'main'; by getting the thread (main_thread = love.thread.getThread()) inside main.lua (or anything inside that thread) you can then set a message there; by getting the 'main' thread from another thread (main_thread = love.thread.getThread('main')) you can then get the message. Example:

main.lua:

Code: Select all

self = love.thread.getThread()
thread = love.thread.newThread('thread', 'thread.lua')
thread:start()
self:set('some var', 'something')
thread.lua:

Code: Select all

self = love.thread.getThread()
main = love.thread.getThread('main')
while true do
   if main:get('some var') == 'something' then
      --well, just do something
   end
   love.timer.speel(.001)
end
I didn't test this yet but it should work

(P.S: I did something similar, anyway I was able to send data from the main thread to another with this method)
That's what I was looking for.

Thank you both.

Topic done
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests