Jump to content

Using variables in a function? [Solved]


Kraxie
 Share

Recommended Posts

Hai!

 

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)
App.source:Stop()
local sound = Sound:Load("Sound/BGM/" .. name .. ".wav")
App.source:SetSound(sound)
App.source:Play()
end

 

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

Windows 7 | Intel Core i7-4790K | 16 GB RAM | Nvidia GTX 980

Link to comment
Share on other sites

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()
self.source:SetSound(sound)
sound:Release()
self.source:Play()

 

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.

Link to comment
Share on other sites

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...

Windows 7 | Intel Core i7-4790K | 16 GB RAM | Nvidia GTX 980

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
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.

 Share

×
×
  • Create New...