Code: Select all
dir = "songs/"
function love.load()
randomNumber = love.math.random(1, 2)
sfx = love.audio.newSource(dir..randomNumber..".mp3")
end
function love.keypressed(key)
if key == "a" then
love.audio.play(sfx)
end
end
Code: Select all
dir = "songs/"
function love.load()
randomNumber = love.math.random(1, 2)
sfx = love.audio.newSource(dir..randomNumber..".mp3")
end
function love.keypressed(key)
if key == "a" then
love.audio.play(sfx)
end
end
Code: Select all
songs = love.filesystem.getDirectoryItems( 'path/to/songs' )
-- Get random song
song = songs[love.math.random( #songs )]
Code: Select all
--Set our seed, so when we use math.random(), the number is actually random...
love.math.setRandomSeed(os.time())
--Where our loaded song will go, so leave it blank for now
local song
--The directory the songs are in
local dir = "songs/"
--What kind of file extensions we are looking for
local validExtensions = {".mp3", ".ogg"}
--The list of valid songs. We will generate this in love.load, so leave this blank too
local dirList
love.load = function(arg)
--Get a list of things in the song folder
dirList = love.filesystem.getDirectoryItems(dir)
--Take out all the directories from the list
for k,v in pairs(dirList) do
if love.filesystem.isDirectory(dir .. v) then
table.remove(dirList, k)
end
end
--Do the same thing, but this time, check the file extensions!
--Any file without an extension in validExtensions will be removed, even those with no extension
for k,v in pairs(dirList) do
for i,vv in ipairs(validExtensions) do
--If this file has a valid extentsion, then we can break out of this inner loop; the file can stay
if string.find(v, vv) then break end
--If we get this far, and this was the last valid extension, then this must be a bad file...
if i == #validExtensions then table.remove(dirList, k) end
end
end
--[[
So now dirList is a sequence of everything in dir
...minus directories
...minus any files without an extension in validExtensions
]]--
--Let's actually load the song now...
pickNewSong()
end
pickNewSong = function()
--The number of the song we will pick from the list (This is NOT the name of the song, just the place in the list)
local songNumber = love.math.random(1, #dirList)
--Load our song, finally...
song = love.audio.newSource(dir .. dirList[songNumber], "stream")
end
love.keypressed = function(key, scancode, isrepeat)
--Get those repeats outta here!
if isrepeat then return end
if key == "n" then
--Can't just stop at one!
--Just make sure we stop the old song before we start a new one
song:stop()
pickNewSong()
end
if key == "p" then
--Is the song stopped? Then play it!
if song:isStopped() then
song:play()
else
--Ok, well maybe its paused? Resume it then!
if song:isPaused() then
song:resume()
else
--Ok well, the only thing it can be now is playing, so um, PAUSE!
song:pause()
end
end
end
if key == "l" then
--Want it to never stop?
song:setLooping(not(song:isLooping()))
end
if key == "s" then
--GAHH NEVER MIND MAKE IT STOPPPPP
song:stop()
end
end
Code: Select all
{
[1] = "a",
[2] = "b",
[3] = "c"
}
Code: Select all
love.audio.play(song)
song:play()
love.audio.stop(song)
song:stop()
Code: Select all
t = {"a", "b", "c"}
print(#t) --will print 3
s = "Hello"
print(#s) --will print 5
t = {
[1] = "a",
[2] = "b",
[3] = "c",
pasta = "tasty",
[5] = "e"
}
print(#t) --will print 3, because we skipped a number, and string-keys don't count!
Code: Select all
for k,v in pairs(dirList) do
if love.filesystem.isDirectory(dir .. v) then
table.remove(dirList, k)
end
end
Code: Select all
t = {
a = 9001
}
print( t[1] ) -- prints 9001
Code: Select all
t = {
a = 1,
b = 2,
c = 3,
d = 4
}
for key,value in pairs(t) do
print("KEY=" .. key)
print("VALUE=" .. value)
print("")
end
--[[ This will output:
KEY=d
VALUE=4
KEY=a
VALUE=1
KEY=c
VALUE=3
KEY=b
VALUE=2
]]--
A minor detail, it doesn't return the number of characters, but the number of bytes in a string.CorruptionEX wrote:Ok so the hashtag is easy:
Putting a # in front of a variable that is a table or a string, returns its length.
Strings, the length is just how many characters there are in it.
Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests