Question about Threads

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
User avatar
ChicoGameDev
Citizen
Posts: 70
Joined: Thu Feb 14, 2019 6:02 pm
Location: Switzerland
Contact:

Question about Threads

Post by ChicoGameDev »

Hi everybody,

Am I missing something or love.math.random cannot be accessed from a new thread created using love.thread.newThread?

I get this error : attempt to index field 'math' (a nil value)

on this line :

Code: Select all

lgMRandom = love.math.random
Lionel Leeser

Luven : https://github.com/chicogamedev/Luven

--

Always keep Game Dev as a passion.
siberian
Prole
Posts: 14
Joined: Sat Apr 06, 2019 1:42 pm

Re: Question about Threads

Post by siberian »

You can use math.random()

Code: Select all

local threadCode = [[
local count = ...
count = count or 1

math.randomseed(os.time())
for i = 1, count do
    print(math.random(10))
end
]]
 

local th
function love.load()
    th = love.thread.newThread( threadCode )
    th:start(10)
end
or add

Code: Select all

require 'love.math'
into thread code.

"When a Thread is started, it only loads love.data, love.filesystem, and love.thread module. Every other module has to be loaded with require."
Last edited by siberian on Tue Sep 01, 2020 4:06 pm, edited 1 time in total.
User avatar
ChicoGameDev
Citizen
Posts: 70
Joined: Thu Feb 14, 2019 6:02 pm
Location: Switzerland
Contact:

[SOLVED] Re: Question about Threads

Post by ChicoGameDev »

Hi,

That's should be precised in the wiki x)

Thanks for your answer, indeed in the love table there is only love.thread, love.filesystem (wonderful!) and love.data.

I'll go with math.random then.

[Edit]

I did not see the full answer ! Waouw that's really wonderful ! Thanks I was missing something indeed !

Awesome! Thanks!


Regards,
Last edited by ChicoGameDev on Tue Sep 01, 2020 4:09 pm, edited 1 time in total.
Lionel Leeser

Luven : https://github.com/chicogamedev/Luven

--

Always keep Game Dev as a passion.
siberian
Prole
Posts: 14
Joined: Sat Apr 06, 2019 1:42 pm

Re: Question about Threads

Post by siberian »

I added a second option into my answer, it's simpler.
User avatar
zorg
Party member
Posts: 3160
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [SOLVED] Re: Question about Threads

Post by zorg »

ChicoGameDev wrote: Tue Sep 01, 2020 4:04 pm That's should be precised in the wiki x)
It is mentioned (verbatim) on the page for threads: https://love2d.org/wiki/love.thread
Me and my stuff :3True 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.
User avatar
ChicoGameDev
Citizen
Posts: 70
Joined: Thu Feb 14, 2019 6:02 pm
Location: Switzerland
Contact:

Re: Question about Threads

Post by ChicoGameDev »

Yes I'm so sorry my eyes have just jumped this information :'(

Thanks for answering.


Regards,
Lionel Leeser

Luven : https://github.com/chicogamedev/Luven

--

Always keep Game Dev as a passion.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests