blob: e8498ddc18518886d32b2e8a56280db224193c54 [file] [log] [blame]
Serge Bazanski18c1a262022-07-07 14:24:53 +02001local ThreadNode = Node:extend('ThreadNode', {
2 threadFile = nil,
3 threadChannel = nil,
4 updateInterval = 5,
5 lastUpdate = 0,
6 state = nil,
7})
8
9function ThreadNode:update(dt)
10 if not self.state then self:checkUpdate() end
11end
12
13function ThreadNode:checkUpdate()
14 if self.threadFile and self.threadChannel then
15 if self.lastUpdate < love.timer.getTime() - self.updateInterval or
16 (not self.state and self.lastUpdate < love.timer.getTime() - 5) then
17 self.lastUpdate = love.timer.getTime()
18 print(self.threadChannel, "Updating...")
19
20 local updateThread = love.thread.newThread(self.threadFile)
21 updateThread:start()
22 end
23
24 local v = love.thread.getChannel(self.threadChannel):pop()
25 if v then
26 self:onUpdate(v)
27 end
28 end
29end
30
31function ThreadNode:afterExit()
32 print('exit')
33 self:checkUpdate()
34end
35
36function ThreadNode:onUpdate(v)
37 self.state = v
38end
39
40return ThreadNode