Jump to content

Using variables in a function? [Solved]

Recommended Posts



I have a function for changing the music track in my game and it works fine and all, but I also want it to get the total length of the track...

Here is my function I'm currently using:


function App:SwitchMusic(name)
local sound = Sound:Load("Sound/BGM/" .. name .. ".wav")


I tried adding something like

App.tracklength = Math:Round(self.name:GetLength())

But it only returns in an error, since I guess it's trying to find a loaded sound with the name "name" and not from the variable.


Do anyone have any idea how I can solve this? sad.png


Thanks! KraXarN biggrin.png

Share this post

Link to post

What is self.name?


So your sound variable has the GetLength() function. So after you load the sound store that in another variable.


local sound = Sound:Load()
self.soundLength = sound:GetLength()


If you are inside App functions than you can use self vs App. They mean the same thing because you are in the App "class" but it's general practice to use self vs the table name itself.

Share this post

Link to post

Exactly what I was looking for! Thanks! laugh.png


I don't really know why I used App there instead of self, but changed it and all works fine now so...


EDIT: I found out that it works just fine, but when drawing it, it works for a little while until the game freezes and just randomly closes...

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...