Help me with LUA aniamtion

Hi,

I’am working on addon, and I want add random animation for NPC model (when You can read quest text) or make delay between animation loop.

This is a code I have:

local frame = CreateFrame("Frame", "QCP" ,QCQuestTitleFrame)
frame:SetPoint("CENTER", -30, 60, "TOP");
frame:SetWidth(700);
frame:SetHeight(700);
frame:SetAlpha(1);
frame:SetFrameStrata("DIALOG");
local model = CreateFrame("PlayerModel", "QCP", QCQuestTitleFrame);
model:SetFrameStrata("DIALOG");
model:SetUnit("TARGET");
model:ClearModel();
model:SetPortraitZoom(0.3);
model:SetPosition(-1.6,0,0);
model:SetScale(0.8);
model:SetFacing(-(math.pi / -26))
model:SetAlpha(1);
model:SetScript("OnAnimStarted", function() model:SetAnimation(60); end);
model:SetScript("OnAnimFinished", function() model:SetAnimation(60); end);
model:SetAllPoints(frame);
model:RegisterEvent("PLAYER_TARGET_CHANGED");
model:RegisterEvent("UNIT_PORTRAIT_UPDATE");
model:RegisterEvent("UNIT_MODEL_CHANGED");
model:SetScript("OnEvent", TargetPortrait_EventHandler);

Something you might be able to work with (/fa starts and stops the animation (if it hasn’t finished)), the Animations table contains a list of sequences and the amount of time they should run (the run times are just random for the example):

local Start = false
local RunTime = 0
local Seqtime = 0
local Sequence = 0
local Animations = {
	{ sequence=5, run=5000 },
	{ sequence=60, run=5000 },
	{ sequence=67, run=10000 },
	{ sequence=96, run=7000 },
}
local function RunAnimation(self, elapsed)
	Seqtime = Seqtime + (elapsed * 1000)
	if Seqtime > RunTime then
		self:SetScript("OnUpdate", nil)
		Start = false
		return
	end 
	self:SetSequenceTime(Sequence, Seqtime)
end


local frame = CreateFrame("Frame", "QCP" ,QCQuestTitleFrame)
frame:SetPoint("CENTER", -30, 60, "TOP");
frame:SetWidth(700);
frame:SetHeight(700);
frame:SetAlpha(1);
frame:SetFrameStrata("DIALOG");
local model = CreateFrame("PlayerModel", "QCP", QCQuestTitleFrame);
model:SetFrameStrata("DIALOG");
model:SetUnit("TARGET");
model:ClearModel();
model:SetPortraitZoom(0.3);
model:SetPosition(-1.6,0,0);
model:SetScale(0.8);
model:SetFacing(-(math.pi / -26))
model:SetAlpha(1);
model:SetUnit("player")
--model:SetScript("OnAnimStarted", function() model:SetAnimation(60); end);
--model:SetScript("OnAnimFinished", function() model:SetAnimation(60); end);
model:SetAllPoints(frame);
model:RegisterEvent("PLAYER_TARGET_CHANGED");
model:RegisterEvent("UNIT_PORTRAIT_UPDATE");
model:RegisterEvent("UNIT_MODEL_CHANGED");
model:SetScript("OnEvent", TargetPortrait_EventHandler);

SLASH_FIZZLEANIMATE1 = "/fa"
SlashCmdList["FIZZLEANIMATE"] = function(msg)	
	Start = not Start
	if Start then
		seqtime = 0
		local s = random(1, #Animations)
		RunTime = Animations[s].run
		Sequence = Animations[s].sequence
		model:SetScript("OnUpdate", RunAnimation)
	else
		model:SetScript("OnUpdate", nil)
	end
end

1 Like

Ok… It’s working very nice. Thanks.

But problem is after animation it’s stop. How to make loop now? Can be random animations or… like animation talk -> wait 3 seconds ->talk -> wait…

And after i click on NPC again, animation never play again

Set Seqtime to 0. Each sequence runs a certain length of time and the update moves it along each step. You can start half way through if you like. To move on to another sequence, set Sequence to the new one and reset Seqtime again.

This example just uses /fa to pick a random animation and start it playing from 0. Random being random and a small sample, you might get the same sequence multiple times in a row.

1 Like

Thank of all help, I will try best i can :slight_smile:
LUA is new for me… and I’m learning LUA making this addon… so… long way :slight_smile:

Last question. is possible to only edit this part:

model:SetScript("OnAnimStarted", function() model:SetAnimation(60); end);
model:SetScript("OnAnimFinished", function() model:SetAnimation(60); end);

To only add delay between loop? ( talk -> 3sec delay -> talk 3sec delay)

I’m asking because that code You give me it’s great but very complicated for me and I need time to make with this code what i exactly need (but I can even slowdown animation with You’r code so is amazing!).

And for now it will be great to have only this delay - if is not too complicated.

I’m making this addon - just trying things, spending hours on google, looking how to do this, or I’m checking code other addons… But… Yeee… not easy job, but I’m super proud :stuck_out_tongue: :slight_smile:

Here You can find my addon: Queso + wow curse (sorry for no link - but i can’t add links, dunno why).

You could replace it with something like (would need refining for whatever you’re doing). Talk for 8ish seconds depending on sequence length, pause for 4, rinse and repeat:

local function Play(start)
	if start then
		model:SetAnimation(60)
		model:SetScript("OnAnimFinished", function() model:SetAnimation(60); end);
		C_Timer.After(8, function()
			model:SetScript("OnAnimFinished", nil)
			Play()
		end)
	else
		C_Timer.After(4, function()
			Play(true)
		end)
	end
end
Play(true)
1 Like

Thank You :slight_smile:

Don’t shotgun-blast all the available help channels at once, that’s like yelling and irritates people. Step through them softly.

http://www.catb.org/~esr/faqs/smart-questions.html#forum

Crosspost for reference

Funny how the person I helped in his previous thread now starts swearing at me just because I don’t like seeing the same thread cross-posted everywhere

It makes sense to not call every single doctor in the hospital and just go to your appointed doctor first. People reading your other threads will just waste their time

https://imgur.com/6BSzUgA

1 Like