Page 1 of 1

How to change sprite frames for an npc?

Posted: Wed Dec 27, 2023 4:48 pm
by LunarCatUSA
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?)

Re: How to change sprite frames for an npc?

Posted: Wed Dec 27, 2023 8:34 pm
by Mal8rk
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

Re: How to change sprite frames for an npc?

Posted: Sat Dec 30, 2023 10:46 pm
by LunarCatUSA
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.

Re: How to change sprite frames for an npc?

Posted: Sun Dec 31, 2023 1:09 am
by Mal8rk
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")

Re: How to change sprite frames for an npc?

Posted: Mon Jan 01, 2024 3:50 am
by LunarCatUSA
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

Re: How to change sprite frames for an npc?

Posted: Mon Jan 01, 2024 6:40 am
by Marioman2007
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

Re: How to change sprite frames for an npc?

Posted: Mon Jan 01, 2024 3:10 pm
by LunarCatUSA
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!