Page 1 of 1

Is it possible to make star coins save instantly upon collection?

Posted: Wed Dec 29, 2021 4:06 pm
by AlanLive2020
I've been experimenting with a mario odyssey styled level. I decided to use star coins rather than stars (as you can place more than one on a section), but you lose them upon death, and they don't save unless you clear a level.

Re: Is it possible to make star coins save instantly upon collection?

Posted: Thu Dec 30, 2021 3:38 pm
by CaptainMonochrome
Yes, it is!

Code: Select all

local LevelName = Level.filename()
local CoinData = SaveData._basegame.starcoin[LevelName]

local COLLECTED = 2
local SAVED = 1

function onExit()
	for i = 1,CoinData.maxID do
		if CoinData[i] and CoinData[i] >= COLLECTED then
			CoinData[i] = SAVED
			SaveData._basegame.starcoinCounter = SaveData._basegame.starcoinCounter + 1
		end
	end
end
This little snippet of code will save your Star Coins whenever you exit a level, even if you lose a life while doing so! The only real caveat is that it doesn't do anything if you exit the game from the pause menu mid-level because the game doesn't save, though I'd expect that that's not really what you're worried about. Funnily enough, literally all it is is just some of the code from the Star Coin AI file, just without the "actually winning the level" condition.

On a side note, once you're done with your level, could you send me a DM of it or the episode it's a part of? I'd love to check it out!

Re: Is it possible to make star coins save instantly upon collection?

Posted: Thu Dec 30, 2021 4:58 pm
by AlanLive2020
CaptainMonochrome wrote:
Thu Dec 30, 2021 3:38 pm
Yes, it is!

Code: Select all

local LevelName = Level.filename()
local CoinData = SaveData._basegame.starcoin[LevelName]

local COLLECTED = 2
local SAVED = 1

function onExit()
	for i = 1,CoinData.maxID do
		if CoinData[i] and CoinData[i] >= COLLECTED then
			CoinData[i] = SAVED
			SaveData._basegame.starcoinCounter = SaveData._basegame.starcoinCounter + 1
		end
	end
end
This little snippet of code will save your Star Coins whenever you exit a level, even if you lose a life while doing so! The only real caveat is that it doesn't do anything if you exit the game from the pause menu mid-level because the game doesn't save, though I'd expect that that's not really what you're worried about. Funnily enough, literally all it is is just some of the code from the Star Coin AI file, just without the "actually winning the level" condition.

On a side note, once you're done with your level, could you send me a DM of it or the episode it's a part of? I'd love to check it out!
Thank you! I might post the level once i finish it but it might take a while.

Side note: I moved the code to onTick so that exiting isnt requiered.

Re: Is it possible to make star coins save instantly upon collection?

Posted: Thu Dec 30, 2021 5:31 pm
by CaptainMonochrome
AlanLive2020 wrote:
Thu Dec 30, 2021 4:58 pm
Thank you! I might post the level once i finish it but it might take a while.

Side note: I moved the code to onTick so that exiting isnt requiered.

I'm happy I could help! Also, given that the code is so small, I probably should've just left it in onTick() by default, lol. Good call!

Also, don't worry if the level takes a while. SMBX levels are art, and, like all kinds of art, take time.