Weird Koopa Shell behaviour

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

Moderator: Userbase Moderators

AlanLive2020
Buster Beetle
Buster Beetle
Posts: 98
Joined: Fri Nov 27, 2020 8:15 am
Flair: The guy with no social skills or self-esteem :')

Weird Koopa Shell behaviour

Postby AlanLive2020 » Sat Mar 12, 2022 11:40 am

So i tried to recreate koopas using lua with different speeds, but sometimes (not always) when i stomp on them, they instaly start moving (like if i kicked them) but at a slower speed

Image

Here's the code. I copied most of it from the bombshell koopa so that might be why.

Code: Select all

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

--Create the library table
local sampleNPC = {}
--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 sampleNPCSettings = {
	id = npcID,
	--Sprite size
	gfxheight = 48,
	gfxwidth = 32,
	--Hitbox size. Bottom-center-bound to sprite size.
	width = 32,
	height = 32,
	--Sprite offset from hitbox for adjusting hitbox anchor on sprite.
	gfxoffsetx = 0,
	gfxoffsety = 0,
	--Frameloop-related
	frames = 2,
	framestyle = 1,
	framespeed = 8, --# frames between frame change
	--Movement speed. Only affects speedX by default.
	speed = 0.5,
	--Collision-related
	npcblock = false,
	npcblocktop = false, --Misnomer, affects whether thrown NPCs bounce off the NPC.
	playerblock = false,
	playerblocktop = false, --Also handles other NPCs walking atop this NPC.

	nohurt=false,
	nogravity = false,
	noblockcollision = false,
	nofireball = false,
	noiceball = false,
	noyoshi= false,
	nowaterphysics = false,
	--Various interactions
	jumphurt = false, --If true, spiny-like
	spinjumpsafe = false, --If true, prevents player hurt when spinjumping
	harmlessgrab = false, --Held NPC hurts other NPCs if false
	harmlessthrown = false, --Thrown NPC hurts other NPCs if false
	cliffturn = true,
	grabside=false,
	grabtop=false,
	--Identity-related flags. Apply various vanilla AI based on the flag:
	iswalker = true,
	--isbot = false,
	--isvegetable = false,
	--isshoe = false,
	--isyoshi = false,
	--isinteractable = false,
	--iscoin = false,
	--isvine = false,
	--iscollectablegoal = false,
	--isflying = false,
	--iswaternpc = false,
	--isshell = false,

	--Emits light if the Darkness feature is active:
	--lightradius = 100,
	--lightbrightness = 1,
	--lightoffsetx = 0,
	--lightoffsety = 0,
	--lightcolor = Color.white,

	--Define custom properties below
}

--Applies NPC settings
npcManager.setNpcSettings(sampleNPCSettings)

--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_NPC]=95,
		[HARM_TYPE_PROJECTILE_USED]=10,
		[HARM_TYPE_LAVA]={id=13, xoffset=0.5, xoffsetBack = 0, yoffset=1, yoffsetBack = 1.5},
		[HARM_TYPE_HELD]=95,
		--[HARM_TYPE_OFFSCREEN]=0,
	}
);

--Custom local definitions below


--Register events
function sampleNPC.onInitAPI()
	npcManager.registerEvent(npcID, sampleNPC, "onTickNPC")
	--npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
	--npcManager.registerEvent(npcID, sampleNPC, "onDrawNPC")
	registerEvent(sampleNPC, "onNPCKill", "onNPCKill")
end

function sampleNPC.onTickNPC(v)
	--Don't act during time freeze
	if Defines.levelFreeze then return end
	
	local data = v.data
	
	--If despawned
	if v.despawnTimer <= 0 then
		--Reset our properties, if necessary
		data.initialized = false
		return
	end

	--Initialize
	if not data.initialized then
		--Initialize necessary data.
		data.initialized = true
	end

	--Depending on the NPC, these checks must be handled differently
	if v:mem(0x12C, FIELD_WORD) > 0    --Grabbed
	or v:mem(0x136, FIELD_BOOL)        --Thrown
	or v:mem(0x138, FIELD_WORD) > 0    --Contained within
	then
		--Handling
	end
	
	--Execute main AI. This template just jumps when it touches the ground.
end
function sampleNPC.onNPCKill(eventObj, npc, reason)
	if npc.id == npcID and (reason == 1 or reason == 2 or reason == 7) then
		eventObj.cancelled = true
		npc:transform(174)
		SFX.play(2)
		if reason == 2 or reason == 7 then
			npc.speedY = -5
		end
		npc.speedX = 0
	end
	if npc.id == npcID and (reason == HARM_TYPE_PROJECTILE_USED) then
		SFX.play(9)
		Effect.spawn(10, npc.x, npc.y)
	end
	if npc.id == npcID and (reason == HARM_TYPE_LAVA) then
		SFX.play(16)
		Effect.spawn(13, npc.x + 0.5, npc.y + 1)
	end
	if npc.id == npcID and (reason == HARM_TYPE_SPINJUMP) then
		SFX.play(36)
	end
	if npc.id == npcID and (reason == HARM_TYPE_NPC) or npc.id == npcID and (reason == HARM_TYPE_HELD) then
		SFX.play(9)
		Effect.spawn(96, npc.x, npc.y)
	end
end
--Gotta return the library table!
return sampleNPC
Anyone knows why this happens?

MrDoubleA
Ripper II
Ripper II
Posts: 394
Joined: Mon Aug 20, 2018 7:02 am
Flair: How much munchers?

Re: Weird Koopa Shell behaviour

Postby MrDoubleA » Sun Mar 13, 2022 10:13 pm

You should set v.speedX to 0 after transforming.

AlanLive2020
Buster Beetle
Buster Beetle
Posts: 98
Joined: Fri Nov 27, 2020 8:15 am
Flair: The guy with no social skills or self-esteem :')

Re: Weird Koopa Shell behaviour

Postby AlanLive2020 » Mon Mar 14, 2022 10:20 am

MrDoubleA wrote:
Sun Mar 13, 2022 10:13 pm
You should set v.speedX to 0 after transforming.
Ah i see, i was setting npc.speedX to 0. Thx

Edit: nvm that causes an error, ill check it later.

Added in 26 minutes 20 seconds:
It's weird, cause it only happens rarely. Most of the time the shell is stopped.

MrDoubleA
Ripper II
Ripper II
Posts: 394
Joined: Mon Aug 20, 2018 7:02 am
Flair: How much munchers?

Re: Weird Koopa Shell behaviour

Postby MrDoubleA » Mon Mar 14, 2022 3:24 pm

AlanLive2020 wrote:
Mon Mar 14, 2022 10:47 am
MrDoubleA wrote:
Sun Mar 13, 2022 10:13 pm
You should set v.speedX to 0 after transforming.
Ah i see, i was setting npc.speedX to 0. Thx

Edit: nvm that causes an error, ill check it later.

Added in 26 minutes 20 seconds:
It's weird, cause it only happens rarely. Most of the time the shell is stopped.
Sorry, I misread the code. What I think you actually need to do is add:

Code: Select all

npc:mem(0x18,FIELD_FLOAT,0)

AlanLive2020
Buster Beetle
Buster Beetle
Posts: 98
Joined: Fri Nov 27, 2020 8:15 am
Flair: The guy with no social skills or self-esteem :')

Re: Weird Koopa Shell behaviour

Postby AlanLive2020 » Mon Mar 14, 2022 9:36 pm

MrDoubleA wrote:
Mon Mar 14, 2022 3:24 pm
AlanLive2020 wrote:
Mon Mar 14, 2022 10:47 am
MrDoubleA wrote:
Sun Mar 13, 2022 10:13 pm
You should set v.speedX to 0 after transforming.
Ah i see, i was setting npc.speedX to 0. Thx

Edit: nvm that causes an error, ill check it later.

Added in 26 minutes 20 seconds:
It's weird, cause it only happens rarely. Most of the time the shell is stopped.
Sorry, I misread the code. What I think you actually need to do is add:

Code: Select all

npc:mem(0x18,FIELD_FLOAT,0)
Thanks. That actually fixed it.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 1 guest

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari