r/howdidtheycodeit • • Jan 08 '23

Question How does one code a defense mechanic like Paper Mario?

In Paper Mario, when an enemy attacks you, you have a window of time to press the block button to successfully block the attack. If you get it spot on, you block the max amount. If you get close to the proffered time, you still block, but a little less. Your attack is a similar situation. During your attack animation, there is a few seconds before the impact where you can press the action button to deal more damage.

Is this driven via the animation events per animation? Paper Mario Combat Tutorial - RIP Professor Frankly

28 Upvotes

10 comments sorted by

22

u/BisonST Jan 08 '23

Not a programmer, but I imagine there are a set number of frames to hit the button. Too late and the game ignores the input and the normal damage was already applied.

11

u/LazyIce487 Jan 08 '23

Haven’t seen it in action, but if you initiate the animation and you measure it in frames or time, you just measure the distance from “perfect” timing and the users input. And divide based on that, i.e., if the timing was within 100ms or X frames of “perfect” you award max, and you pick the scale/window from there outward

4

u/Asnyd421 Jan 08 '23

Yeah I'd start a timer when that bar appears, when the timer ends the attack proceeds with no bonus. Then make an event that receives user input, it'll want to grab the current remaining time and stop the timer. Then use that value to LERP between whatever the min damage modifier and the highest damage mod (maybe .5-1.5). Multiply damage dealt by that.

It'll be a little tricky mathematically because after that peak point the LERP will have to invert so you'll end up with an if( alpha > peakDamageSpot) { LERP from 1.5 to. 5} else { LERP from. 5 to 1.5}

Sorry for the formatting on mobile

5

u/MyPunsSuck Jan 08 '23

That'll certainly get the job done, but I think it might be a bit overengineered unless there's already a general-purpose event system in place.

Because there only one attack happening at a time, you could just have a single variable for the time until the next timed hit. It'd get updated alongside other variables that get updated as often as possible. In the game data, each attack has a value to set the timer to, on the frame it starts. When the player pushes a button, it checks how much time is left (Or how far into the negatives it went)

3

u/ThisIsHughYoung Jan 08 '23

Agreed, this whole thread is super cringe lol. I don't think most of the top level commenters understand that Paper Mario is a turn based JRPG with fixed animations.

2

u/[deleted] Jan 23 '23

Well you see, if I spend time working on convoluted systems for my game and situations that probably will never happen, I don’t actually have to work on my game, but I still feel like I am making progress. /s

3

u/[deleted] Jan 08 '23

During the attack animation you set a boolean that is being checked on button press, allowing the blocking logic to run.

1

u/snipercar123 Jan 11 '23 edited Jan 11 '23

This looks like something you can set up to work in a generic way using events and a timer.

I wrote some code from my phone, C# Unity.

//Use this event somewhere to get results
Public static event EventHandler<bool> OnAnyActionHandled;

Private float timeWindow;
Private float passedTime;

Private bool isActive;

Private InputKey keyInputToTrack;

//Runs every frame
void Update()
{

if(!isActive){
return;
}

if(GetKeyPressUp() == keyInputToTrack)
{
//Success!
OnAnyActionHandled?.Invoke(this, true);
ResetValues();
return;
}

timePassed += Time.DeltaTime * 1f;

if(timePassed >= timeWindow)
{
//Failed :(
OnAnyActionHandled?.Invoke(this, false);
ResetValues();
return;
}

}

//Call this to start tracking input
Public void StartTracking(int timeWindow, Input keyPressToTrack)
{
this.timeWindow = timeWindow;
this.keyPressToTrack = keyPressToTrack;

isActive = true;
}

Private void ResetValues ()
{
//Reset all values to track and isActive to false
}

1

u/InfComplex Jan 16 '23

Right now I’m working in Unreal Engine, so my solution makes use of timelines.

When the enemy’s attack begins, a 0.0-1.0 timeline begins from the start. When the player is intended to block, a bool is set to true. That bool blocks an event that stops the timeline. Read value, apply to damage scale.