How to change sprite frames for an npc?

Post here for help and support regarding LunaLua and SMBX2's libraries and features.
LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

How to change sprite frames for an npc?

Postby LunarCatUSA » Wed Dec 27, 2023 4:48 pm

Image
This is the npc, it has 8 frames with a framespeed of 30.
What I want to do is make frames 1,2 for moving to the left and 5,6 for moving to the right but make frames 3,4 for standing still while looking left and frames 7,8 while looking right.

How would I go about doing this in the npc code? (Maybe make two states moving and notmoving?)

Mal8rk
Snifit
Snifit
Posts: 200
Joined: Mon Oct 25, 2021 11:04 pm
Flair: English Speaking Spanish Speaker
Pronouns: He/Him
Contact:

Re: How to change sprite frames for an npc?

Postby Mal8rk » Wed Dec 27, 2023 8:34 pm

If you wanna change npc frames, you would first have to put the following lines of code in an onTickEndNPC function:

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
& you wanna add this snippet of code to the function, which makes it so the npc has the same frame changes for if it's either facing left or right

Code: Select all

	-- animation controlling
	v.animationFrame = npcutils.getFrameByFramestyle(v, {
		frame = data.frame,
		frames = sampleNPCSettings.frames
	});
also a frame speed of 30? that's way too slow. I'd personally aim for a frame speed of 8 which is the average

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: How to change sprite frames for an npc?

Postby LunarCatUSA » Sat Dec 30, 2023 10:46 pm

Mal8rk wrote:
Wed Dec 27, 2023 8:34 pm
If you wanna change npc frames, you would first have to put the following lines of code in an onTickEndNPC function:

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
& you wanna add this snippet of code to the function, which makes it so the npc has the same frame changes for if it's either facing left or right

Code: Select all

	-- animation controlling
	v.animationFrame = npcutils.getFrameByFramestyle(v, {
		frame = data.frame,
		frames = sampleNPCSettings.frames
	});
also a frame speed of 30? that's way too slow. I'd personally aim for a frame speed of 8 which is the average
Alright, but I've seen to have gotten an error message saying "attempt to index npcutils (a nil value)"
Not sure if I have to do something specific to the code.

Mal8rk
Snifit
Snifit
Posts: 200
Joined: Mon Oct 25, 2021 11:04 pm
Flair: English Speaking Spanish Speaker
Pronouns: He/Him
Contact:

Re: How to change sprite frames for an npc?

Postby Mal8rk » Sun Dec 31, 2023 1:09 am

LunarCatUSA wrote:
Sat Dec 30, 2023 10:46 pm
Mal8rk wrote:
Wed Dec 27, 2023 8:34 pm
If you wanna change npc frames, you would first have to put the following lines of code in an onTickEndNPC function:

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
& you wanna add this snippet of code to the function, which makes it so the npc has the same frame changes for if it's either facing left or right

Code: Select all

	-- animation controlling
	v.animationFrame = npcutils.getFrameByFramestyle(v, {
		frame = data.frame,
		frames = sampleNPCSettings.frames
	});
also a frame speed of 30? that's way too slow. I'd personally aim for a frame speed of 8 which is the average
Alright, but I've seen to have gotten an error message saying "attempt to index npcutils (a nil value)"
Not sure if I have to do something specific to the code.
Oh, my bad on that, add this at the top of your npc's lua file:

local npcutils = require("npcs/npcutils")

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: How to change sprite frames for an npc?

Postby LunarCatUSA » Mon Jan 01, 2024 3:50 am

Mal8rk wrote:
Sun Dec 31, 2023 1:09 am
LunarCatUSA wrote:
Sat Dec 30, 2023 10:46 pm
Mal8rk wrote:
Wed Dec 27, 2023 8:34 pm
If you wanna change npc frames, you would first have to put the following lines of code in an onTickEndNPC function:

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames

Code: Select all

v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
& you wanna add this snippet of code to the function, which makes it so the npc has the same frame changes for if it's either facing left or right

Code: Select all

	-- animation controlling
	v.animationFrame = npcutils.getFrameByFramestyle(v, {
		frame = data.frame,
		frames = sampleNPCSettings.frames
	});
also a frame speed of 30? that's way too slow. I'd personally aim for a frame speed of 8 which is the average
Alright, but I've seen to have gotten an error message saying "attempt to index npcutils (a nil value)"
Not sure if I have to do something specific to the code.
Oh, my bad on that, add this at the top of your npc's lua file:

local npcutils = require("npcs/npcutils")
Alright, so I've done all of this and it seems to generally function but now the frames count up to non-existent numbers when moving or standing still.
The code I have:

Code: Select all

v.animationFrame = npcutils.getFrameByFramestyle(v, {frame = v.data.frame, frames = DummehSettings.frames})
if status == 0 then
	function OnTickEndNPC()
		v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
	end
end
if status ~= 0 then
	function OnTickEndNPC()
		v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames
	end
end

Marioman2007
Lakitu
Lakitu
Posts: 466
Joined: Tue Aug 25, 2020 3:19 am
Flair: Dr. Bones
Pronouns: He/Him

Re: How to change sprite frames for an npc?

Postby Marioman2007 » Mon Jan 01, 2024 6:40 am

LunarCatUSA wrote:
Mon Jan 01, 2024 3:50 am
Alright, so I've done all of this and it seems to generally function but now the frames count up to non-existent numbers when moving or standing still.
The code I have:

Code: Select all

v.animationFrame = npcutils.getFrameByFramestyle(v, {frame = v.data.frame, frames = DummehSettings.frames})
if status == 0 then
	function OnTickEndNPC()
		v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
	end
end
if status ~= 0 then
	function OnTickEndNPC()
		v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames
	end
end

That's not how onTickEndNPC works, it's a function that needs to registered to the npc library using npcManager.registerEvent.

Code: Select all

function sampleNPC.onInitAPI()
	npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
end

function sampleNPC.onTickEndNPC(v)
	local extraFrames = 0 -- For walking frames

	if status == 0 then
		extraFrames = 2 -- For the looking & standing frames
	end
		
	local currentFrame = (math.floor(lunatime.tick() / 8) % 2) + extraFrames
	v.animationFrame = npcutils.getFrameByFramestyle(v, {frame = currentFrame, frames = DummehSettings.frames})
end

LunarCatUSA
Hoopster
Hoopster
Posts: 115
Joined: Sat Mar 06, 2021 9:56 am
Flair: Soda-Butler

Re: How to change sprite frames for an npc?

Postby LunarCatUSA » Mon Jan 01, 2024 3:10 pm

Marioman2007 wrote:
Mon Jan 01, 2024 6:40 am
LunarCatUSA wrote:
Mon Jan 01, 2024 3:50 am
Alright, so I've done all of this and it seems to generally function but now the frames count up to non-existent numbers when moving or standing still.
The code I have:

Code: Select all

v.animationFrame = npcutils.getFrameByFramestyle(v, {frame = v.data.frame, frames = DummehSettings.frames})
if status == 0 then
	function OnTickEndNPC()
		v.animationFrame = math.floor(lunatime.tick() / 8) % 2 + 2 --For the looking & standing frames
	end
end
if status ~= 0 then
	function OnTickEndNPC()
		v.animationFrame = math.floor(lunatime.tick() / 8) % 2 --For walking frames
	end
end

That's not how onTickEndNPC works, it's a function that needs to registered to the npc library using npcManager.registerEvent.

Code: Select all

function sampleNPC.onInitAPI()
	npcManager.registerEvent(npcID, sampleNPC, "onTickEndNPC")
end

function sampleNPC.onTickEndNPC(v)
	local extraFrames = 0 -- For walking frames

	if status == 0 then
		extraFrames = 2 -- For the looking & standing frames
	end
		
	local currentFrame = (math.floor(lunatime.tick() / 8) % 2) + extraFrames
	v.animationFrame = npcutils.getFrameByFramestyle(v, {frame = currentFrame, frames = DummehSettings.frames})
end
Okay so I made the code this after initializing the onTickEndNPC(v) in the code further above

Code: Select all

function Dummeh.onTickEndNPC(v)
	local extraFrames = 0 -- For walking frames
	if status == 0 then
		extraFrames = 2 -- For the looking & standing frames
	end
	local currentFrame = (math.floor(lunatime.tick() / 8) % 2) + extraFrames
	v.animationFrame = npcutils.getFrameByFramestyle(v, {frame = currentFrame, frames = DummehSettings.frames})
end
And this is what happens with the npc, it works fine going to the left but frame breaks going right. I have a gif showing what happens but when going to the RIGHT moving, it alternates between frames 8 and 9, which do not exist so the npc is invisible.

Added in 4 hours 45 minutes 21 seconds:
AH HA! I fixed it! I just had to set the frames number to 4 to limit the frame loop to one direction.

Thanks so much for the help everyone!


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 4 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari