Local Variables Shared Between Blocks?

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

Moderator: Userbase Moderators

Cloth Pocket
Chain Chomp
Chain Chomp
Posts: 310
Joined: Mon Aug 04, 2014 2:42 pm

Local Variables Shared Between Blocks?

Postby Cloth Pocket » Wed Feb 09, 2022 4:48 am

So I'm trying to make a custom block which behaves in the following way: when hit the block moves up 32 pixels and draws an image of the of the block with a lower opacity it its previous position. This can happen up to 4 times at which point the block will return to its original position if hit again.

The problem that I am having is that the two variables that hold the number of times the block has been hit and the initial positon of the block seem to be shared between all other blocks with the same ID. How can I make it so that every block has its own unique version of these variables?

Also, a gif to help illustrate the problem, top left number shows the "hits" variable:
Spoiler: show
Image

Here's the block's code:

Code: Select all

local blockManager = require("blockManager")
local customBlock = {}
local blockID = BLOCK_ID

local customBlockSettings = {
	id = blockID,
	frames = 1,
	bumpable = true,
}

blockManager.setBlockSettings(customBlockSettings)

local initialY
local hits = 0
local hitEffect = Graphics.loadImage(Misc.resolveFile("test\\block-751effect.png"))

function customBlock.onInitAPI()
	blockManager.registerEvent(blockID, customBlock, "onTickEndBlock")
	registerEvent(customBlock, "onBlockHit")
end

function customBlock.onTickEndBlock(v)
	if v.isHidden or v:mem(0x5A, FIELD_BOOL) then return end
	local data = v.data

	if hits > 0 then
		for i = 0,hits do
			Graphics.drawImageToScene(hitEffect,v.x,initialY+(i*-32))
		end
	end
	if hits == 5 then
		v.y = initialY
		hits = 0
	end
end

function customBlock.onBlockHit(eventObject, v, fromUpper, playerOrNil)
	if initialY == nil then initialY = v.y end

	if hits < 5 then
		v.y = v.y - 32
		hits = hits + 1
	end
end

return customBlock
Thanks!

deice
Rocky Wrench
Rocky Wrench
Posts: 638
Joined: Fri Jul 23, 2021 7:35 am

Re: Local Variables Shared Between Blocks?

Postby deice » Wed Feb 09, 2022 5:58 am

a good place to store per-instance variables would be the "data" table.
using the following code for the block worked for me:

Code: Select all

local blockManager = require("blockManager")
local customBlock = {}
local blockID = BLOCK_ID

local customBlockSettings = {
	id = blockID,
	frames = 1,
	bumpable = true,
}

blockManager.setBlockSettings(customBlockSettings)

local hitEffect = Graphics.loadImage(Misc.resolveGraphicsFile("block-751effect.png"))

function customBlock.onInitAPI()
	blockManager.registerEvent(blockID, customBlock, "onTickEndBlock")
	registerEvent(customBlock, "onBlockHit")
end

function customBlock.onTickEndBlock(v)
	if v.isHidden or v:mem(0x5A, FIELD_BOOL) then return end
	local data = v.data
	
	data.hits = data.hits or 0
	data.initialY = data.initialY or v.y

	if (data.hits > 0) then
		for i = 0, data.hits do
			Graphics.drawImageToScene(hitEffect, v.x, data.initialY + (i * -32))
		end
	end
	
	if (data.hits == 5) then
		v.y = data.initialY
		data.hits = 0
	end
end

function customBlock.onBlockHit(eventObject, v, fromUpper, playerOrNil)
	local data = v.data
	
	data.hits = data.hits or 0
	data.initialY = data.initialY or v.y

	if (data.hits < 5) then
		v.y = v.y - 32
		data.hits = data.hits + 1
	end
end

return customBlock

Cloth Pocket
Chain Chomp
Chain Chomp
Posts: 310
Joined: Mon Aug 04, 2014 2:42 pm

Re: Local Variables Shared Between Blocks?

Postby Cloth Pocket » Wed Feb 09, 2022 6:23 am

It works now, thanks!


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari