Jump to content

How to use the PlayAnimation endhook feature [C++]

Recommended Posts

Good day, everyone.

I'm trying to get a hang of the animation function set and I'm trying to figure out how to use the endhooks in C++

From what I understand, it's a function that gets triggered once the animation has ended, however there are no example codes in the docs that actually show how that is written.


Thank you.

Share this post

Link to post

Ow Jesus. I was afraid of that.... 

Can we get something similar for firing up C++ functions perhaps? @Josh or that 0.0 - 1.0 animation solution starting to sound pretty suitable?


Share this post

Link to post

Well, I cannot speak for him of course, but that was 2 years ago and still nothing.

It seems people just measure it themselves.  I'll be doing this soon myself since animation is my next milestone......  Not a big fan of lua for core stuff like player controlling, so yeah.

If I write something of use before you have a solution, i will gladly share it.

  • Like 1

Share this post

Link to post

I was bored.  I wrote something in a few minutes.  It's a mockup, I dunno?  It works anyhow.  I'm using EventEmitter I posted the other night in that other thread.


Or just ignore my ->on and ->emit stuff in the player class.  It's just a callback function anyway.

main loop:

	long currentTime;
	long dt;
	long lt;

	while (true) {
		if (window->Closed() || window->KeyDown(Leadwerks::Key::Escape)) return false;

		currentTime = Leadwerks::Time::GetCurrent();
		dt = currentTime - lt;
		lt = currentTime;


		//dt = Leadwerks::Time::GetCurrent() - dt;
		context->SetColor(1.0, 1.0, 1.0);
		context->DrawStats(2, 2);
	return 0;

player class update function:

// constructor ..
this->on(EVENT_ANIMATION_END, function<void (const char*)>([this](const char* sequence) {
	std::cout << "Animation sequence '" << sequence << "' ended!" << std::endl;

void update(long dt) {
		animationFrame += dt / ANIMATION_WALK_SPEED;
		if (animationFrame > model->GetAnimationLength("walk")) {
			animationFrame = 0; // this doesnt have to reset to 0 but rather whatever your state system dictates?
			this->emit(EVENT_ANIMATION_END, "walk");
		if (animationFrame < 0) {
			animationFrame = 0;
		model->SetAnimationFrame(animationFrame, 0.8, "walk");
		std::cout << "animationFrame: " << animationFrame << endl;

EDIT: I guess it should be noted (at least in my model) it's 0 based animation sequences, so GetAnimationLength would return, for example 100 and your frames are 0-99; so offset appropriately.

Also, I'm not blending or anything here (it's a hack come on!), so you'd take the remainder from the DT calculation and probably want to mix that into your wrap around to avoid hitching or smooth the interpolation =D

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...