Love2D,线程执行和输入

Love2D, Threads execution and input

拜托,我遇到了 Love2d 线程函数的问题,我没有找到任何我能理解的示例或解释。

首先: 在主文件中我得到:

thread1 = love.thread.newThread("ambient.lua")
thread2 = love.thread.newThread("ambient.lua")
thread1:start()
thread2:start()

ambient.lua 包含:

random1 = love.math.random(1, 10)
gen1 = love.audio.newSource("audio/ambient/gen/a/gen_a_".."random1"..".mp3", 
"static")
gen1:setVolume(1.0)
gen1:setLooping(false)
gen1:play()

工作正常,问题是当我在同一步骤或延迟询问 var = Thread1:isRunning( ) 时,当音频正在播放并尝试打印时,它抛出错误(应该是空的)。当音频结束时,我看到内存已被清除。另外,如果我 link thread1:start() 鼠标单击然后在短时间内多次启动它,内存使用量会疯狂上升,然后经过一段时间后类似于样本长度开始减少。问题是,我是在创建多个线程吗?在那种情况下,他们甚至在样本结束后正确终止了吗?还是线程生命周期只有 1 步长,而我只创建了多个使用同一线程播放的音频源?支票本身有问题吗?

下一个问题是我需要将 thread1:start() 与值一起使用:

thread1:start(volume, sampleID)

而且我不知道如何在线程本身中添加它们。指南和示例说 "vararg" 参考。我没有看到任何体面的解释或任何包含“...”的示例在线程的变量输入中的用法。我需要如何编写它的示例。即使这个音频 fiddle 不是一个很好的例子,我肯定会需要它用于 AI。无需复杂的输入,只需简单的 x,y,size,target_x,target_y values.

and i have no clue how to addres them in the thread itself. guides and examples says "vararg" reference. i didnt see any decent explanation or any example containing "..." usage in variable input into threads

你看说明书还不够。每个加载的 Lua 块(Lua 5.1 手册的第 2.4.1 节)都是一个参数数量可变的匿名函数。
当您调用 love.thread.newThread("ambient.lua") 时,Love2D 将创建新的块,所以基本 Lua 规则适用于这种情况。
在您的示例中,线程内的 volume 和 sampleID 参数将按如下方式检索:

local volume, sampleID = ...

gen1 = love.audio.newSource(get_stream_by_id(sampleID), "static")
gen1:setVolume(volume)
gen1:setLooping(false)
gen1:play()