make the unbreakable breakable?

Need help with any SMBX game-related issues? Ask your questions here.

Moderator: Userbase Moderators

_SleepyWhirl_
Bob-Omb
Bob-Omb
Posts: 23
Joined: Thu Aug 19, 2021 9:52 pm
Flair: Discord: SleepyWhirl#6093
Pronouns: he/him
Contact:

make the unbreakable breakable?

Postby _SleepyWhirl_ » Tue Sep 21, 2021 10:14 am

hello yet again, i just have a quick question, is it possible to make an unbreakable block (such as a ground tile) breakable by bumping or attacking it somehow? in my project im including breakable walls but i ran out of breakable blocks to use them all with, do i have to make a text file like with NPCs and give it a command? or is there another way? thank you all!

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

Re: make the unbreakable breakable?

Postby deice » Wed Sep 22, 2021 11:10 am

the simplest solution would be to create a single (or multiple, depending on how you want them to be broken) custom breakable block, and give it a value in it's "data" array that decides which image should be drawn (controlled in the onDraw event via an if/switch statement), completely saving you the hassle of using multiple blocks altogether

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

Re: make the unbreakable breakable?

Postby Emral » Wed Sep 22, 2021 2:13 pm

deice wrote:
Wed Sep 22, 2021 11:10 am
the simplest solution would be to create a single (or multiple, depending on how you want them to be broken) custom breakable block, and give it a value in it's "data" array that decides which image should be drawn (controlled in the onDraw event via an if/switch statement), completely saving you the hassle of using multiple blocks altogether
And replacing it with the hassle of having to manually define the look of each on a per-instance basis... not quite ideal, I think.

The most important part is knowing what kind of things you want to be able to harm these block IDs. Fundamentally, you can break any block by calling its :remove(true) method (true meaning that the effects and sound are generated) from lua code. Certain NPCs like Thwomps and Skewers utilize this in the Block config "smashable", where "smashable=2" is the flag that will cause a thwomp to break a block when smashing into it.

Beyond those, it's a matter of picking and choosing the interactions you want, and finding situations in code where they are met. For example, using the onPostExplosion event, you can perform a Block.getIntersecting over the radius of an explosion that just happened, check the ID of the blocks hit, and remove those that match your ID you want removed (here: 751). This basically amounts to "Break the blocks of id 751 when an explosion happens nearby", and can be extended by just adding more IDs to the if statement.

Code: Select all

function onPostExplosion(explosion)
	for k,v in Block.iterateIntersecting(explosion.x, explosion.y, explosion.width, explosion.height) do
		if v.id == 751 and not v.isHidden then
			v:remove(true)
		end
	end
end
This approach is perhaps a little more work up-front (I'm not sure if this is entirely more effort than making a custom draw system for these blocks like suggested in the other post), but is balanced out by it being easily extensible and not requiring you to set anything per instance of a block. It's also more performant since you don't have to draw anything and only need to loop over blocks very occasionally.

If you're attempting this idea and find yourself with tens of IDs and the if statements are a hassle to maintain, you can also do something like this:

Code: Select all

local breakableIDs = table.map{751, 752, 753}

function onPostExplosion(explosion)
	for k,v in Block.iterateIntersecting(explosion.x, explosion.y, explosion.width, explosion.height) do
		if breakableIDs[v.id] and not v.isHidden then
			v:remove(true)
		end
	end
end
This uses a lookup table (first line) so you only need to add more IDs to the table.map call, rather than going into all your ID check if-statements every time to extend your code. Might be useful should this become a large list.


Return to “Help and Support”

Who is online

Users browsing this forum: No registered users and 1 guest

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari