Jump to content

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


TheConceptBoy
 Share

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.

Link to comment
Share on other sites

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

Coding for Christ.

Link to comment
Share on other sites

I believe if you use an Actor class, there is an EndAnimation function you can extend:
https://www.leadwerks.com/learn?page=Tutorials_CPP_Actors

virtual void EndAnimation(const int sequence);

It's not in the docs because it is a sort of obscure thing I think was added in the last update.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

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.

https://gist.github.com/rioki/1290004d7505380f2b1d

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;
		Leadwerks::Time::Update();

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

		controller->update(dt);
		
		world->Update();
		world->Render();

		//dt = Leadwerks::Time::GetCurrent() - dt;
		context->SetBlendMode(Leadwerks::Blend::Alpha);
		context->SetColor(1.0, 1.0, 1.0);
		context->DrawStats(2, 2);
		context->Sync(false);
	}
	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

Coding for Christ.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
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.

 Share

×
×
  • Create New...