Detect when player attacks an enemy?

Fripouney

Member
May 13, 2024
4
1
110
Pronouns
He/Him
Hey everyone !

I've been working on my first mod for the past few days, and I want players to get points for performing specific actions (collecting coins, stars, killing enemies, etc.)
My problem here is that I don't understand how to give points for killing enemies since I don't know how to detect when a player attacks them.

I've read in the docs that gMarioState.usedObj could be used when a player stomps a goomba, but again, I'm not too sure how to use this nor if it is the correct way to solve my problem.

Any help would be greatly appreciated !
 
Coins and stars are simple, you can use the HOOK_ON_INTERACT hook. This hook takes in the mario state that interacted, the object mario interacted with, and the interaction type. With this, you can check for coins if the interaction type is set to INTERACT_COIN, you can do the same with INTERACT_STAR_OR_KEY for stars and keys (if you want to detect if it's a key or a star, check the behavior with "if o.behavior == get_behavior_from_id(id_bhvBowserKey)"). For detecting enemies, that's a bit more complicated.
INTERACT_BOUNCE_TOP and INTERACT_BOUNCE_TOP2 are the main interact stats enemies use. Enemies like chuckya use INTERACT_GRABBABLE also, so you should check the behaviors for those ones if you don't want breaking boxes to count. INTERACT_HIT_FROM_BELOW are used by snufit's and other enemies. Those are pretty much all the interact types enemies use, so hopefully this helps with what you're trying to accomplish :]
 
Coins and stars are simple, you can use the HOOK_ON_INTERACT hook. This hook takes in the mario state that interacted, the object mario interacted with, and the interaction type. With this, you can check for coins if the interaction type is set to INTERACT_COIN, you can do the same with INTERACT_STAR_OR_KEY for stars and keys (if you want to detect if it's a key or a star, check the behavior with "if o.behavior == get_behavior_from_id(id_bhvBowserKey)"). For detecting enemies, that's a bit more complicated.
INTERACT_BOUNCE_TOP and INTERACT_BOUNCE_TOP2 are the main interact stats enemies use. Enemies like chuckya use INTERACT_GRABBABLE also, so you should check the behaviors for those ones if you don't want breaking boxes to count. INTERACT_HIT_FROM_BELOW are used by snufit's and other enemies. Those are pretty much all the interact types enemies use, so hopefully this helps with what you're trying to accomplish :]
I'm currently testing all of this and it seems to work very well, thanks ! I've just got one more question though, what's the difference between INTERACT_BOUNCE_TOPand INTERACT_BOUNCE_TOP2?
Don't double post! Use that edit button! Post automatically merged:

Update : after testing I realized that these interactions actually happen when you are close to an enemy, not necessarily when you kill it. If you stay close to an enemy for some time, it results in the interaction happening every frame which is problematic
 
Last edited:
what's the difference between INTERACT_BOUNCE_TOPand INTERACT_BOUNCE_TOP2?
Good question, they're the exact same. INTERACT_BOUNCE_TOP2 isn't used in the vanilla game though, it's unused, so I shouldn't have mentioned it
Update : after testing I realized that these interactions actually happen when you are close to an enemy, not necessarily when you kill it. If you stay close to an enemy for some time, it results in the interaction happening every frame which is problematic
Well you see, this gets complicated extremely fast. A simple(r) way for INTERACT_BOUNCE_TOP would be to get the interaction via local interaction = determine_interaction(m, o), then checking if that interaction is an attack not from below, like this: if interaction & INT_ATTACK_NOT_FROM_BELOW ~= 0 then, which should do the trick for that one. For INTERACT_HIT_FROM_BELOW, you can just check if it was any attack via if interaction & INT_ANY_ATTACK ~= 0 then. INTERACT_GRABBABLE get's a lot more complicated, I'd recommend looking into m.heldObj, keeping track of the last held object, and seeing if that object dies, and was never held again.
 
Good question, they're the exact same. INTERACT_BOUNCE_TOP2 isn't used in the vanilla game though, it's unused, so I shouldn't have mentioned it

Well you see, this gets complicated extremely fast. A simple(r) way for INTERACT_BOUNCE_TOP would be to get the interaction via local interaction = determine_interaction(m, o), then checking if that interaction is an attack not from below, like this: if interaction & INT_ATTACK_NOT_FROM_BELOW ~= 0 then, which should do the trick for that one. For INTERACT_HIT_FROM_BELOW, you can just check if it was any attack via if interaction & INT_ANY_ATTACK ~= 0 then. INTERACT_GRABBABLE get's a lot more complicated, I'd recommend looking into m.heldObj, keeping track of the last held object, and seeing if that object dies, and was never held again.
Thanks again for all the help, I managed to make it work correctly for attacks like punching, diving or kicking. For some reason, bouncing on an enemy doesn't seem to be considered as an attack at all, since the value of interactionis still 0. I'll need to do some more research to get this case to work properly.
 
Interesting. I wouldn't know then, if you want to know what I'm looking at, I'm looking at the interact_bounce_top function in line 1842 in src/game/interaction.c in the sm64ex-coop developer repo (coopdx is the same for this stuff here). You can go and take a lookt at it, and maybe you can find something that helps you out. I'll be able to help you a bit more later when I can actually test stuff :].
 
Interesting. I wouldn't know then, if you want to know what I'm looking at, I'm looking at the interact_bounce_top function in line 1842 in src/game/interaction.c in the sm64ex-coop developer repo (coopdx is the same for this stuff here). You can go and take a lookt at it, and maybe you can find something that helps you out. I'll be able to help you a bit more later when I can actually test stuff :].
I did some research in the interaction.c file and found the INT_HIT_FROM_ABOVEflag, which seems to be giving some very promising results. I'll do some more testing to make sure everything works as intended !

EDIT : Well, nevermind I thought it did something, but after more testing it ends up messing up everything
 
Last edited:
  • Like
Reactions: EmeraldLockdown

Users who are viewing this thread