04-19-2024, 10:10 PM
The first thing I notice that needs commenting on is you have multiple memory leaks in the game. Run it and play it long enough, and your computer will self-destruct and send all the nuclear launch codes to North Korea!
For example, let's take a look at what happens when we try to eat food, as above ^. If your food >=10, then you open the sound file ("eat.mp3") and assign it to the handle eat&, and then you play the sound of eatting... So if you eat once, you open that file and have it in memory once. Eat a second time, you now have a second copy of that file in memory. Eat 50 times, and you now have 50 copies of "eat.mp3" in memory!!
If you're going to use a _SNDOPEN in a loop, you have to remember to have a _SNDCLOSE when you're finished with that sound.
My advice, to keep it simple??
Just move all those resource loading commands to the very start of the program, leave them in memory, and then call them as needed without concern. Keep in mind that you can play a sound handle as many times as you want -- you just don't want to OPEN it more than once, without also CLOSING it when you're finished with it first.
eat& = _SndOpen("eat.mp3")
FOR i = 1 to 10
_SNDPLAY eat&
_Delay 1
NEXT
For instance, the above would load the file once, and then play that sound 10 times in a row, with a 1 second delay between playing. No memory leak there, nor issue with anything -- it'll work perfectly fine.
FOR i = 1 to 10
eat& = _SndOpen("eat.mp3")
_SNDPLAY eat&
_Delay 1
NEXT
Compared to the version above which now loads 10 copies into memory, causing a memory leak and app usage to climb constantly as it runs through the loop. The only way the 2nd version above would be workable would be like so:
FOR i = 1 to 10
eat& = _SndOpen("eat.mp3")
_SNDPLAY eat&
_SndClose eat& 'close it after you use it, and before you open it again!
_Delay 1
NEXT
Code: (Select All)
Case 1 'Eat food
If food >= 10 Then
eat& = _SndOpen("eat.mp3")
_SndPlay eat&
If you're going to use a _SNDOPEN in a loop, you have to remember to have a _SNDCLOSE when you're finished with that sound.
My advice, to keep it simple??
Just move all those resource loading commands to the very start of the program, leave them in memory, and then call them as needed without concern. Keep in mind that you can play a sound handle as many times as you want -- you just don't want to OPEN it more than once, without also CLOSING it when you're finished with it first.
eat& = _SndOpen("eat.mp3")
FOR i = 1 to 10
_SNDPLAY eat&
_Delay 1
NEXT
For instance, the above would load the file once, and then play that sound 10 times in a row, with a 1 second delay between playing. No memory leak there, nor issue with anything -- it'll work perfectly fine.
FOR i = 1 to 10
eat& = _SndOpen("eat.mp3")
_SNDPLAY eat&
_Delay 1
NEXT
Compared to the version above which now loads 10 copies into memory, causing a memory leak and app usage to climb constantly as it runs through the loop. The only way the 2nd version above would be workable would be like so:
FOR i = 1 to 10
eat& = _SndOpen("eat.mp3")
_SNDPLAY eat&
_SndClose eat& 'close it after you use it, and before you open it again!
_Delay 1
NEXT