How to create NPCs with basegame behavior?

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
Donkdonker124
Bob-Omb
Bob-Omb
Posts: 20
Joined: Wed Mar 27, 2024 10:31 am
Flair: Fictionally stronger than man
Pronouns: he/him
Contact:

Re: How to create NPCs with basegame behavior?

Postby Donkdonker124 » Mon Apr 29, 2024 2:25 pm

Code: Select all

--NPCManager is required for setting basic NPC properties
local npcManager = require("npcManager")

--Create the library table
local paraGoombrat = {}
--NPC_ID is dynamic based on the name of the library file
local npcID = NPC_ID

--Defines NPC config for our NPC. You can remove superfluous definitions.
local paraGoombratSettings = {
	id = npcID,
	gfxheight = 48,
	gfxwidth = 40,
	gfxoffsetx = 0,
	gfxoffsety = 0,
	width = 32, 
	height = 32,
	frames = 4,
	framestyle = 0,
	framespeed = 4,
	isflying = true,
}

--Applies NPC settings
npcManager.setNpcSettings(paraGoombratSettings)

--Register the vulnerable harm types for this NPC. The first table defines the harm types the NPC should be affected by, while the second maps an effect to each, if desired.
npcManager.registerHarmTypes(npcID,
	{
		HARM_TYPE_JUMP,
		HARM_TYPE_FROMBELOW,
		HARM_TYPE_NPC,
		HARM_TYPE_PROJECTILE_USED,
		HARM_TYPE_LAVA,
		HARM_TYPE_HELD,
		HARM_TYPE_TAIL,
		HARM_TYPE_SPINJUMP,
		HARM_TYPE_OFFSCREEN,
		HARM_TYPE_SWORD
	}, 
	{
		[HARM_TYPE_JUMP]=753,
		[HARM_TYPE_FROMBELOW]=754,
		[HARM_TYPE_NPC]=754,
		[HARM_TYPE_PROJECTILE_USED]=754,
		[HARM_TYPE_LAVA]={id=13, xoffset=0.5, xoffsetBack = 0, yoffset=1, yoffsetBack = 1.5},
		[HARM_TYPE_HELD]=754,
		[HARM_TYPE_TAIL]=754,
		[HARM_TYPE_SPINJUMP]=10,
		[HARM_TYPE_OFFSCREEN]=10,
		[HARM_TYPE_SWORD]=10,
	}
);

function paraGoombrat.onPostNPCHarm(event, v, rsn, culprit)
	if v.id == NPC_ID and rsn == 1 then
		event.cancelled = true
		v:transform(npcID - 1)
	end
end


function paraGoombrat.onInitAPI()
	npcManager.registerEvent(npcID, paraGoombrat, "onTickNPC")
	npcManager.registerEvent(paraGoombrat, "onPostNPCHarm")
end

return paraGoombrat

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

Re: How to create NPCs with basegame behavior?

Postby Emral » Mon Apr 29, 2024 6:42 pm

The line should just be this:

Code: Select all

registerEvent(paraGoombrat, "onPostNPCHarm")
There's registerEvent, a global function for registering events, and then there's the npcManager variant, which registers npc-specific events. onTickNPC, for example, is the npcManager variant of onTick. onDrawNPC for onDraw, and onPostNPCHarmNPC for onPostNPCHarm. But like, onPostNPCHarmNPC is conceptually so superfluous I don't know if it even exists.
The rule is "the one with NPC at the end only runs for the IDs of NPC you registered it to", but for onPostNPCHarm where the 1st argument is the harmed NPC, a simple id check at the start of the function takes care of it. onTickNPC and onDrawNPC are very useful because it means you don't have to do your own for loops over NPCs to figure out which NPC to run code on. Instead, the for loop happens internally, for a performance boost.

Donkdonker124
Bob-Omb
Bob-Omb
Posts: 20
Joined: Wed Mar 27, 2024 10:31 am
Flair: Fictionally stronger than man
Pronouns: he/him
Contact:

Re: How to create NPCs with basegame behavior?

Postby Donkdonker124 » Mon Apr 29, 2024 7:23 pm

So, I did what you said and got rid of the NPCManager thing, but that threw up an error when I jumped on it. I added the npcID back in, and that error doesn't show up when stomped, nor on startup. So we're making progress :D

The bad news is that there's still no transformation.

Update: IT TRANSFORMS NOW LETS GOOOOOOOO but the alt behaviors still don't work

Update 2: the uprising: I got it to work now yaaay


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 1 guest

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari