I am trying to build myself a template project that includes libraries for audio, screen rendering, data saving, etc... I'm getting stuck on something that is probably really simple, but I can't seem to wrap my head around it. I have created a sample audio script that I am trying to import into my main.lua file. The import seems to run successfully when I build the project it doesn't complain. However, anytime I try to call anything from the audio script in my main.lua file it fails.
main.lua
import "audio"
audio.PlaySample(audio.a_sample)
function playdate.update()
end
audio.lua
a_sample = playdate.sound.sampleplayer.new("audio/sfx_sample01")
function PlaySample(m_samplePlayer)
m_samplePlayer:play()
end
Stack Trace
Load failed, error: main.lua:3: attempt to index a nil value (global 'audio')
stack traceback:
main.lua:3: in main chunk
I feel like I'm missing something really simple here, but I have been banging my head against this for a while. So, any help would be appreciated!
This doesn't seem to work. I still get the same nil value error when trying to run the function contained in my audio script. Here's what I tried based on your suggestion
main.lua
import "audio"
audio.PlaySample(audio.a_sample)
audio.lua
a_sample = playdate.sound.sampleplayer.new("audio/sfx_sample01")
audio = {}
:
function audio.PlaySample(m_samplePlayer)
m_samplePlayer:play()
end
Stack Trace
Load failed, error: main.lua:3: attempt to index a nil value (global 'audio')
stack traceback:
main.lua:3: in main chunk
The way it's behaving it feels like the main function accepts the import of "audio" but whenever I reference it in main.lua it claims the value is nil. If I change the path of the audio.lua file the build fails. That tells me that when the build succeeds it is confirming it can import the audio.lua file, but at runtime the reference to the file is nil.
Ok, I think I understand what is going on here now.
The audio object being referenced in main.lua is nil because it doesn't exist in the context of the audio.lua script.
In the audio.lua script the audio object needs to be instantiated as an empty table.
Then the following variable and function declarations essentially extend upon that instantiated audio table.
Once the audio table exists and has variables/functions declared within it then it can be referenced after import in main.lua
main.lua
import "audio"
audio.PlaySample(audio.a_sample)
audio.lua
audio = {}
audio.a_sample = playdate.sound.sampleplayer.new("audio/sfx_sample01")
function audio.PlaySample(m_samplePlayer)
m_samplePlayer:play()
end
A novice asked the Master: "I have a program that sometimes runs and sometimes aborts. I have followed the rules of programming, yet I am totally baffled. What is the reason for this?"
The Master replied: "You are confused because you do not understand Tao. Only a fool expects rational behavior from his fellow humans. Why do you expect it from a machine that humans have constructed? Computers simulate determinism; only Tao is perfect.
The rules of programming are transitory; only Tao is eternal. Therefore, you must contemplate Tao before you receive Enlightenment."
"But how will I know when I have received Enlightenment?" asked the novice.
"Your program will run correctly," replied the Master.