How to use currencies.lua with Star Coins without already collected ones adding to the counter

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

Moderator: Userbase Moderators

AlphaBlue1011
Fighter Fly
Fighter Fly
Posts: 34
Joined: Sun Feb 02, 2025 3:00 pm
Flair: never underestimate hyperfixations :3
Pronouns: she/her or they/them

How to use currencies.lua with Star Coins without already collected ones adding to the counter

Postby AlphaBlue1011 » Fri Jul 18, 2025 9:45 am

I want to make it so star coins add to a global counter using currencies.lua. However, an issue popped up where if a star coin was already collected, it would count as another Star Coin on the counter even though it shouldn't.

Any way to fix this?
Here's what I got so far:

Code: Select all

local currencies = require("currencies")

local starcoin_currency = currencies.registerCurrency(currencies.CURRENCY_STARCOINS)
starcoin_currency:registerLimit(400, function() end)

-- onDraw can be used to draw the currencies
function onDraw()
    starcoin_currency:draw()
end
Added in 28 minutes 10 seconds:
nvm i figured it out
i had to copy some code from starcoin.lua into the luna.lua for some reason

Code: Select all

local currencies = require("currencies")

local starcoin_currency = currencies.registerCurrency(currencies.CURRENCY_STARCOINS)
starcoin_currency:registerLimit(400, function() end)
local starcoin = require("npcs/ai/starcoin")
local checkpoints = require("checkpoints")
local collected = false

local UNCOLLECTED = 0
local SAVED = 1
local COLLECTED = 2
local COLLECTED_WEAK = 3

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

if not isOverworld then

	if not SaveData._basegame.starcoin[LevelName] then
		SaveData._basegame.starcoin[LevelName] = {}
	end

	function starcoin.getTemporaryData()
		return CoinData
	end

	function starcoin.max()
		return CoinData.maxID
	end

	CoinData.alive = {}
	CoinData.maxID = CoinData.maxID or 0

	function starcoin.registerAlive(id)
		CoinData.alive[id] = true
		if id > CoinData.maxID then
			CoinData.maxID = id
		end
	end
	
	--Called from npc-310 to make sure it runs at the right time
	function starcoin.init()
		-- Reset states
		local activeCheckpoint = checkpoints.getActive()
		for i = 1,CoinData.maxID do
			if (CoinData[i] == COLLECTED and not activeCheckpoint) or CoinData[i] == COLLECTED_WEAK then
				CoinData[i] = 0
			end
		end
	end
	
	function starcoin.collect(coin)
	  local coinEffect
	  if CoinData[coin.ai2] and CoinData[coin.ai2] > UNCOLLECTED then
		coinEffect = Effect.spawn(233, coin.x + coin.width*0.5, coin.y + coin.height*0.5)
	  else
		coinEffect = Effect.spawn(276, coin.x + coin.width*0.5, coin.y + coin.height*0.5)
	  end
	  coinEffect.x = coinEffect.x - coinEffect.width/2
	  coinEffect.y = coinEffect.y - coinEffect.height/2
	
		if CoinData[coin.ai2] == UNCOLLECTED and starcoin.getLevelCollected() + 1 >= starcoin.count() then
			SFX.play(starcoin.sfx_collectall)
		else
			SFX.play(starcoin.sfx_collect)
		end
	
		if coin.ai2 > UNCOLLECTED then
			if CoinData[coin.ai2] == UNCOLLECTED then
				CoinData[coin.ai2] = COLLECTED_WEAK
			end
			local HO = hudoverride.offsets.starcoins
			local currRow = math.floor((coin.ai2 - 1)/HO.grid.width)
			if currRow < HO.grid.offset or currRow > HO.grid.offset + HO.grid.height - 1 then
				HO.grid.offset = currRow
			end
		end
	
		--mem(0x00B2C8E4, FIELD_DWORD, mem(0x00B2C8E4, FIELD_DWORD) + 4000)
	
		--local pointEffect = Effect.spawn(79, coin.x + coin.width/2, coin.y, 8)
		--pointEffect.x = pointEffect.x - pointEffect.width/2
		--pointEffect.animationFrame = 7
		Misc.givePoints(NPC.config[coin.id].score, coin, true)
		coin:kill(9)
	end
end

function onTick()
	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
	starcoin_currency.setMoney = function(c, value)
	    SaveData._basegame.starcoinCounter = value
	end
	starcoin_currency.getMoney = function(c)
	    c._value = SaveData._basegame.starcoinCounter
	    return SaveData._basegame.starcoinCounter
	end
	starcoin_currency.addMoney = function(c, value)
	    SaveData._basegame.starcoinCounter = SaveData._basegame.starcoinCounter + value
	end
	starcoin_currency.compareMoney = function(c, value)
	    return SaveData._basegame.starcoinCounter >= value, SaveData._basegame.starcoinCounter - value
	end
	if starcoin.getLevelCollected(Level.filename()) >= starcoin.count() and not collected then
		collected = true
	end
end

-- onDraw can be used to draw the currencies
function onDraw()
    starcoin_currency:draw()
end

Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari