NPC refuses to stand still.

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

Moderator: Userbase Moderators

Pluixx
Koopa
Koopa
Posts: 16
Joined: Mon May 14, 2018 11:26 am

NPC refuses to stand still.

Postby Pluixx » Tue Oct 19, 2021 5:00 pm

Some context - I'm trying to create a custom NPC/Boss that every-so-often stands still and performs an attack. However, regardless of my efforts it just does not want to stand still. I took a look at the Dry Bones AI script to see if maybe I did something wrong, yet even after setting the speedX to 0, it keeps sliding around. The only time it somewhat worked was when I set it to not be a Walker NPC and then inflicting damage onto it.

Here's said code.

Code: Select all

local npcManager = require("npcManager")
local rng = API.load("rng");

local DAKU_1 = {}
local Units = {}
local npcID = NPC_ID

local DAKU_1_Settings = {
	id = npcID,
	score = 0,

	gfxheight = 74,
	gfxwidth = 126,

	width = 126,
	height = 74,

	gfxoffsetx = 0,
	gfxoffsety = 0,

	frames = 6,
	framestyle = 0,
	framespeed = 5000,
	speed = 2.2,

	npcblock = true,
	npcblocktop = true,
	playerblock = false,
	playerblocktop = false,

	nohurt = false,
	nogravity = false,
	noblockcollision = false,
	nofireball = true,
	noiceball = true,
	noyoshi= true,
	nowaterphysics = true,

	jumphurt = false,
	spinjumpsafe = false,
	harmlessgrab = false,
	harmlessthrown = false,

	grabside = false,
	grabtop = false,

	iswalker = true,

	--lightradius = 100,
	--lightbrightness = 1,
	--lightoffsetx = 0,
	--lightoffsety = 0,
	--lightcolor = Color.white,
}
npcManager.setNpcSettings(DAKU_1_Settings)
npcManager.registerHarmTypes(npcID,
	{
		HARM_TYPE_JUMP,
		HARM_TYPE_SPINJUMP,
	}
);

function DAKU_1.onInitAPI()
	npcManager.registerEvent(npcID, DAKU_1, "onTickNPC")
	npcManager.registerEvent(npcID, DAKU_1, "onTickEndNPC")
	npcManager.registerEvent(npcID, DAKU_1, "onDrawNPC")
	registerEvent(DAKU_1, "onNPCKill")
end
function DAKU_1.onTickNPC(v)
	if Defines.levelFreeze then return end
	
	local data = v.data
	
	if v.despawnTimer <= 0 then
		data.initialized = false
		return
	end

	v.AnimationTimer = 99999999
	if not data.initialized and Units[v] == nil then
		Units[v] = {
			State = "Normal",
			AnimationDebounce = 5,
			AnimationDebounceAttack = 2,
			Health = 5,
			AttackTimer = 200,
			DebounceTimer = 0,
			DefaultDebounceTimer = 75,
		}
		data.initialized = true
	end
end
function DAKU_1.onTickEndNPC(v)
	if Defines.levelFreeze then return end
	local data = v.data

	if v.despawnTimer <= 0 then
		data.initialized = false
		return
	end

	v.AnimationTimer = 99999999
	if Units[v] then
		v.despawnTimer = 100
		Table = Units[v]
		if Table.State ~= "[DEAD]" then
			if Table.State == "Normal" then
				v.speed = 2.2
				v.speedX = v.direction * 1
				Table.AttackTimer = Table.AttackTimer - 1
				if Table.AttackTimer <= 0 then
					v.speedX = 0
					Table.AttackTimer = rng.random(50,300)
					Table.DebounceTimer = Table.DefaultDebounceTimer
					Table.State = "Charging"
				else
					if Table.AnimationDebounce > 0 then
						Table.AnimationDebounce = Table.AnimationDebounce - 1
					else
						Table.AnimationDebounce = 5
						if v.animationFrame >= 3 then
							v.animationFrame = 0
						else
							v.animationFrame = v.animationFrame + 1
						end
					end
				end
			elseif Table.State == "Charging" or Table.State == "Hurt" then
				v.speedX = 0
				Table.DebounceTimer = Table.DebounceTimer - 1
				if Table.DebounceTimer <= 0 then
					Table.State = "Normal"
				else
					if Table.AnimationDebounceAttack > 0 then
						Table.AnimationDebounceAttack = Table.AnimationDebounceAttack - 1
					else
						Table.AnimationDebounceAttack = 3
						if v.animationFrame == 4 then
							v.animationFrame = 5
						else
							v.animationFrame = 4
						end
					end
				end
			end
		else
			v.speedX = 0
		end
	end
	
	if v.collidesBlockBottom then
	end
end

function DAKU_1.onNPCKill(eventObj, v, killReason, culprit)
	if v.id ~= npcID then return end

	eventObj.cancelled = true
	if killReason then
		if Units[v] then
			local Table = Units[v]
			if Table.State ~= "Charging" then
				player.speedY = -4
				SFX.play(2)
			else
				Table.Health = Table.Health - 1
				player.speedY = -6
				if Table.Health == 0 then
					SFX.play(2)
					Table.State = "[DEAD]"
					Animation.spawn(267,x,y,player.section,false,true)
					v:kill()
				else
					SFX.play(39)
					Table.State = "Hurt"
					Table.DebounceTimer = Table.DefaultDebounceTimer
				end
			end
		end
	end
end

return DAKU_1

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

Re: NPC refuses to stand still.

Postby Emral » Tue Oct 19, 2021 5:23 pm

the "iswalker" config auto-applies goomba properties to an npc, and goombas have the annoying habit of just running forever. I believe the dry bones either don't set the iswalker flag and handle speedX manually (v.speedX = v.direction * some multiplier, commonly) or set dontMove while in the collapsed state to prevent movement (though dontMove has the annoying side-effect of also making the NPC face the player, so if dry bones do that they also have some code to lock the direction value)

Pluixx
Koopa
Koopa
Posts: 16
Joined: Mon May 14, 2018 11:26 am

Re: NPC refuses to stand still.

Postby Pluixx » Tue Oct 19, 2021 5:30 pm

Enjl wrote:
Tue Oct 19, 2021 5:23 pm
the "iswalker" config auto-applies goomba properties to an npc, and goombas have the annoying habit of just running forever. I believe the dry bones either don't set the iswalker flag and handle speedX manually (v.speedX = v.direction * some multiplier, commonly) or set dontMove while in the collapsed state to prevent movement (though dontMove has the annoying side-effect of also making the NPC face the player, so if dry bones do that they also have some code to lock the direction value)
Ah, thanks. I wasn't aware of the fact you could set dontMove to true in Lua.


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari