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

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

AlanLive2020
Buster Beetle
Buster Beetle
Posts: 98
Joined: Fri Nov 27, 2020 8:15 am
Flair: The guy with no social skills or self-esteem :')

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

Postby AlanLive2020 » Wed Dec 29, 2021 4:06 pm

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.

CaptainMonochrome
Hoopster
Hoopster
Posts: 48
Joined: Fri Aug 21, 2020 3:40 pm
Flair: SMW sprites rock!
Pronouns: he/they

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

Postby CaptainMonochrome » 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!

AlanLive2020
Buster Beetle
Buster Beetle
Posts: 98
Joined: Fri Nov 27, 2020 8:15 am
Flair: The guy with no social skills or self-esteem :')

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

Postby AlanLive2020 » Thu Dec 30, 2021 4:58 pm

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.

CaptainMonochrome
Hoopster
Hoopster
Posts: 48
Joined: Fri Aug 21, 2020 3:40 pm
Flair: SMW sprites rock!
Pronouns: he/they

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

Postby CaptainMonochrome » Thu Dec 30, 2021 5:31 pm

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.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari