Page 1 of 1

Local Variables Shared Between Blocks?

Posted: Wed Feb 09, 2022 4:48 am
by Cloth Pocket
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!

Re: Local Variables Shared Between Blocks?

Posted: Wed Feb 09, 2022 5:58 am
by deice
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

Re: Local Variables Shared Between Blocks?

Posted: Wed Feb 09, 2022 6:23 am
by Cloth Pocket
It works now, thanks!