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.
-
NoAim91
- Prole
- Posts: 38
- Joined: Mon Feb 20, 2017 10:28 am
- Location: Germany
Post
by NoAim91 » Fri Jun 16, 2017 1:05 pm
Hey, the love.thread thing really need a bit more description at the wiki. I like to input a variable in the thread and get a return after a function call. In this case I put 10 in, the function should add 3 and the return should be 13. But the function inside the thread doesn´t get called. and in generell I have no clue what is going on.
main.lua
Code: Select all
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
t:start(c1:push(input))
table = c1:demand()
print(table)
end
thread.lua
Code: Select all
function test(input)
a = 3
a = a + input
print(a)
return a
end
input = c1:demand()
c1:push(test(input))
-
zorg
- Party member
- Posts: 3043
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
-
Contact:
Post
by zorg » Fri Jun 16, 2017 1:31 pm
Anything you pass into Thread:start will be available as a "file-level" vararg that you can access by doing this:
for example; you can also use the select function too.
Haven't tested the following, but it may or may not work:
Code: Select all
-- main.lua
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
t:start(c1)
c1:push(input)
table = c1:demand()
print(table)
end
Code: Select all
--thread.lua
local c1 = {...}[1]
function test(input)
a = 3
a = a + input
print(a)
return a
end
input = c1:demand()
c1:push(test(input))
Me and my stuff
True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
-
NoAim91
- Prole
- Posts: 38
- Joined: Mon Feb 20, 2017 10:28 am
- Location: Germany
Post
by NoAim91 » Fri Jun 16, 2017 2:30 pm
@zorg: nope, doesn´t work.
Maybe I don´t need channels for this.
I tryed this
Code: Select all
--main.lua
function love.load()
t = love.thread.newThread("thread.lua")
input = 10
output = t:start( input )
print(output)
end
Code: Select all
--thread.lua
local input = input
function test(input)
a = 3
a = a + input
print(a)
return a
end
test(input)
and this did not work.
print(output) --> true
print(a) --> doesn´t print anything
-
Nixola
- Inner party member
- Posts: 1949
- Joined: Tue Dec 06, 2011 7:11 pm
- Location: Italy
Post
by Nixola » Fri Jun 16, 2017 2:32 pm
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
-
NoAim91
- Prole
- Posts: 38
- Joined: Mon Feb 20, 2017 10:28 am
- Location: Germany
Post
by NoAim91 » Fri Jun 16, 2017 3:04 pm
Code: Select all
local input = input
function test(input)
a = input
a = a + 3
print(a)
return a
end
test(input)
Thread error!
thread.lua:5: attempt to perform arithmetic on global 'a' (a nil value)
line 5 is a = a + 3
so what?! define a = input is fine but after that a is unknown?! o.O
-
bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
-
Contact:
Post
by bartbes » Fri Jun 16, 2017 3:18 pm
Yes, because input is nil, change your first line to
-
NoAim91
- Prole
- Posts: 38
- Joined: Mon Feb 20, 2017 10:28 am
- Location: Germany
Post
by NoAim91 » Fri Jun 16, 2017 3:25 pm
thank you! :-)
Ok, now how I get the resault ( a ) out of the thread? XD
-
bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
-
Contact:
Post
by bartbes » Fri Jun 16, 2017 3:44 pm
The easiest way is using a channel, so if we take your initial code again...
Code: Select all
-- main.lua
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
c1:push(input)
t:start(c1)
table = c1:demand()
print(table)
end
-- thread.lua
local c1 = ...
function test(input)
a = 3
a = a + input
print(a)
return a
end
input = c1:demand()
c1:push(test(input))
Of course you probably don't want to use threads for this, but it should work.
-
NoAim91
- Prole
- Posts: 38
- Joined: Mon Feb 20, 2017 10:28 am
- Location: Germany
Post
by NoAim91 » Fri Jun 16, 2017 3:56 pm
@bartbes: this code doesn´t work :-/
I changed something that it work, but it is not finaly right:
Code: Select all
main.lua
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
c1:push(input)
t:start(c1)
end
Code: Select all
thread.lua
local c1 = ...
input = c1:demand()
print("thread/ input: "..input) -- print 10
function test(input)
a = input
a = a + 3
print("thread/ a: "..a) -- print 13
return a
end
c1:push(test(input))
and the strange thing is, when I write this:
Code: Select all
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
c1:push(input)
t:start(c1)
output = c1:demand()
print("main/ output: "..output) --print 10
end
it only print 10 ... and nothing from the thread ... and the thread gives no error
-
zorg
- Party member
- Posts: 3043
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
-
Contact:
Post
by zorg » Fri Jun 16, 2017 4:02 pm
So your issue is that the print command doesn't work in the thread? Because it seems like the thread did return 10 to the main thread, and it was printed...
Me and my stuff
True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Users browsing this forum: No registered users and 64 guests