(2.0) 2nd "Star" Counter idea?

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

Moderator: Userbase Moderators

Flarestripe
Spiny
Spiny
Posts: 29
Joined: Wed Jul 19, 2017 4:19 pm

(2.0) 2nd "Star" Counter idea?

Postby Flarestripe » Sun Aug 16, 2020 7:41 pm

I think it would be cool if someone makes a second "Star" counter, that keeps track of a second type of NPC collectable that basically acts like stars.

"Why would you want that?", someone might ask.

Well, here's my reason:
I am wanting to try to make a Crash Bandicoot - styled episode, with Crates, Crystals, Gems, and all of that junk.
And so, I'm wanting 2 counters. 1 to keep track of Gems, and one to keep track of Crystals.
Crystals would be your progress to completing the "story" (and would unlock doors and stuff), while the Gem counter would be for stuff like New Paths in levels, and unlocking the 100% ending.

My idea for this, so far, is:
Replace star graphics with Crystals.

And

Create code for a new counter (that is carried through the entire episode) that levels would check.
And then, certain levels would check the amount you have, and if you have the required amount (5, 10, 20, depends on the level's set requirement), it could reveal / hide layers to allow access to new areas.

Or, just make a 2nd counter similar to the Stars, with a new NPC similar to stars with its own counter variable.

So, why haven't I made this myself?
Well, funny story...
Eheh...
...
I don't know Coding very well AT ALL- pwp
So, if anyone makes this EVER, please tell me. pwp
(I kinda don't expect anyone to, since it would require a lot of work to make, if you literally made it JUST like the Star itself)

If I ever figure out how to make it, I will post a reply telling people, and will post the file for irt.
But that possibility probably won't EVER happen.

Good luck to anyone that maybe tries this.

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9865
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: (2.0) 2nd "Star" Counter idea?

Postby Emral » Wed Aug 26, 2020 1:19 am

It would hardly be any work, since we have a robust saving system.
Take this piece of code for example:

Code: Select all

local npcManager = require("npcManager") -- Load a library that is helpful for checking whether item are collected

SaveData.collectibles = SaveData.collectibles or { -- Create a table of collectibles. SaveData is special because it saves whenever SMBX saves.
	crystals = 0,
	gems = 0,
}

local collectibleIDNames = { -- Maps a NPC ID to the names. I use red coin and sonic ring here.
	[103] = "crystals",
	[153] = "gems"
}

function onPostNPCKill(id, reason) -- Once an NPC has died
	if collectibleIDNames[id] and npcManager.collected(id, reason) then -- Check if it's one we care for and if it was collected
		SaveData.collectibles[collectibleIDNames[id]] = SaveData.collectibles[collectibleIDNames[id]] + 1 -- And increment the respective counter
	end
end

function onDraw()
	Text.print(SaveData.collectibles.crystals, 100, 100) -- Draw the value of the counters
	Text.print(SaveData.collectibles.gems, 100, 120)
end
This is basically the functional aspect of it in its simplest form. There's a counter (or two) and we increment it when an NPC of a specific ID is collected by the player. The segment in onDraw just draws the number contained within. It can be expanded to have more elaborate constructs of Graphics.drawImage too, like here if you have a "gems.png" in the episode folder:

Code: Select all

local gemsImg = Graphics.loadImage(Misc.resolveFile("gems.png"))

function onDraw()
	Graphics.drawImage(gemsImg, 100, 100)
	Text.print(SaveData.collectibles.gems, 140, 100) -- print text 40 pixels to the right of the left edge of the image
end
More information about text and image drawing functions can be found here:
https://wohlsoft.ru/pgewiki/LunaLua_global_functions

More information about tables like SaveData can be found in the official lua documentation:
https://www.tutorialspoint.com/lua/lua_tables.htm
They're a bit like arrays, if you've heard of those.

If you then want to check the value of these, I recommend making 3 events in SMBX. Maybe one called "checkCrystals", "checkCrystalsPassed" and "checkCrystalsFailed". Then you can do this:

Code: Select all

function onEvent(eventname)
	if eventname == "checkCrystals" then
		if SaveData.collectibles.crystals >= 10 then
			triggerEvent("checkCrystalsPassed")
		else
			triggerEvent("checkCrystalsFailed")
		end
	end
end
This will execute checkCrystalsPassed only when the player has 10 or more. You can add more checks by duplicating the if statement with different event names.

Hope this makes sense. Lemme know if any questions arise.


Return to “LunaLua Help”

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari