Is there a music function?

Is there a dedicated music function in Codea? Because I want to change the music depending on what screen of the game I’m on, but I can’t put music() in the draw function or anywhere else it seems. Does anyone know a way to make this work?

Like this?

function setup()
    tracks={"A Hero's Quest:Battle","A Hero's Quest:In the City"}
    --music()
    parameter.integer("track",1,2)
end

function draw()
    if music.name~=tracks[track]then 
        music.stop()
        music(tracks[track])
    end
end

@Coder. Yes, that’s brillant! Thanks. But what is the ~ for?

@YoloSwag ~= means does not equal to, opposite of == and the same as

if not var1 == var2 then
end

@YoloSwag if music.name ~= tracks[track] checks to see that it isn’t already playing the right song before starting to play it, so that it isn’t restarting it 60 times a second.

@Luatee but won’t that just swap var1 and compare with var2. I think you would need

if not ( var1 == var2 ) then
end

@Coder not in my experience, not takes the next conditional so

if not var1 == var2 then 
end

Is the same as yours but

if not var1 and var3 == var2 then
end

Will treat var1 as a boolean of existing/not existing or true/false and treat the rest as normal.
The brackets do make a difference though as you said. You could wrap those two conditions in brackets and check both of them.

There is.

music( track , loops? )

@aurumcoder2624 Please read the orignal poster’s question, and all of the comments after it…

U can have a class for background music like this. I use this trick all the time. Erase all parameters in init. Then put the music() in the init, with looping set to true if U want.

music("SomeLameTracks: SomeTrack", true)

Then u must initiate the class in setup().

Note: Make sure the class is not static.

@aurumcoder2624 I’m not sure I understand. the whole class is just an init function with that line? how is that better (in fact it takes more code) than just putting that line directly in setup?

@arumcoder2624 But what if you want to change track?

@aurumcoder2624 it just seems a bit unnecessary and you should never write more lines of code then you have to.

The user and all related content has been deleted.

@NatTheCoder, @Doge. I’m not sure if this is what you are looking for, but i modified @Coder 's code. This works beautifully, and it allows you to change the music.

music( "A Hero's Quest:Dungeon" )

That function above by itself is not sufficient for changing music.

In setup:

tracks={"Name of song 1","Name of song 1", "so on...."}
track= 1     -- change this number to start with a different song

In the draw function:

    if music.name~=tracks[track]then
        music.stop()
        music(tracks[track])
    end

If you want to change the track, just update “tracknum” to the track number you want.

@YoloSwag, 1) the only modification you did is change the variable track to tracknum in setup

  1. since you only changed it in setup, the code doesn’t work anymore.

@JakAttak. Sorry, I was thinking about another thing I did. I have edited my mistake. ~~~ tracknum ~~~ should be ~~~ track ~~~ . But to change the track, just say track equals song number in the draw function.

Btw, how did you get the blue outline around a single word?

hold down on the apostrophe key, you should see this ` anything in between two of those will have the blue highlight

The user and all related content has been deleted.

@NatTheCoder Wow, that sounds like a frequently asked question. Maybe you should read the FAQ?