Weird love.thread behaviour or my struggle with loading screen
Posted: Sat Apr 04, 2020 7:24 pm
Im actually making a tile-based game that generating tiles ingame, but the bigger map is, the longer it takes to actually generate a map. While generating, screen leaves empty and nothing happens. So I decided to make a loading screen where player can see how long he has to wait. But generator makes game dead for any updates until the generation is finished. This means i cant do anything until the map is finished
To solve this problem, I decided to put it into thread, so i could have some control on the screen. But it didnt go well.
After launch it fails with this error
attempt to index field 'graphics' (a nil value)
Also i have code to send out information how many unloaded tiles left and it should send out information each with every tile made. For test purpose i actually receiving it once, but it sends out only nil (or nothing, i have no idea how to check it)
I would really appreciate if you help me with existing code or just give me some ideas how could i avoid using cursed thread code
Code: Select all
function tile.genTiles(wight, height)
tileTable["sizeX"] = wight - 1
tileTable["sizeY"] = height - 1
for h = 0, height - 1 do
for w = 0, wight - 1 do
tileTable[w.."/"..h.."_valid"] = true
tileTable[w.."/"..h.."_quad"] = love.graphics.newQuad(0, 0, 48, 48, 48, 48)
end
end
end
To solve this problem, I decided to put it into thread, so i could have some control on the screen. But it didnt go well.
Code: Select all
function tile.genTiles(wight, height)
tileTable["sizeX"] = wight - 1
tileTable["sizeY"] = height - 1
local threadCode = [[
Channel = {}
Channel.inp = love.thread.getChannel("tileChannelIn")
Channel.out = love.thread.getChannel("tileChannelOut")
local draw = require "bin.graph" -- looks like it cant take it as global so it is there too
local tileTable = Channel.inp:demand()
local progressFinal = tileTable["sizeX"] * tileTable["sizeY"]
local progress = 0
local progressOut
local wight = tileTable["sizeX"] + 1
local height = tileTable["sizeY"] + 1
for h = 0, height - 1 do
for w = 0, wight - 1 do
progress = progressFinal + 1
tileTable[w.."/"..h.."_valid"] = true
tileTable[w.."/"..h.."_quad"] = love.graphics.newQuad(0, 0, 48, 48, 48, 48)
progressOut = progress / progressFinal
Channel.out:push(progressOut)
end
end
]]
thread = love.thread.newThread(threadCode)
thread:start()
Channel = {}
Channel.inp = love.thread.getChannel("tileChannelIn")
Channel.out = love.thread.getChannel("tileChannelOut")
Channel.inp:push(tileTable)
print("success")
print(Channel.out:pop()) -- enough to print it into console for testing
end
After launch it fails with this error
attempt to index field 'graphics' (a nil value)
Also i have code to send out information how many unloaded tiles left and it should send out information each with every tile made. For test purpose i actually receiving it once, but it sends out only nil (or nothing, i have no idea how to check it)
I would really appreciate if you help me with existing code or just give me some ideas how could i avoid using cursed thread code