Want to listen for animation actions via :GetMarkerReachedSignal(), but also want to listen for attack types

okay i know the title is probably like, extremely confusing
but;

basically, my enemies have animations (Who would’ve guessed?), and those animations include attack animations

some enemies have different attack types (direct, area of effect), while some enemies have both of those, just at different “attacks”

i figured i could use :GetMarkerReachedSignal() but this would kind of “overlap” each other

track:GetMarkerReachedSignal("attack") . . . 

track:GetMarkerReachedSignal(attackTypeInHere) 
-- now i have 2 listeners, very bad!

i’m not really sure how to work around this, i don’t think i can use :GetMarkers(), as that cannot be used dynamically (according to gpt, atleast), so my last stop is here

You can use a single listener for :GetMarkerReachedSignal() and dynamically determine the attack type based on the marker name

local track = animationTrack -- La tua animazione

track:GetMarkerReachedSignal("attack"):Connect(function()
    local markers = track:GetMarkers()
    for _, marker in pairs(markers) do
        local attackType = marker.Name:match("^attack_(%w+)")
        if attackType then
            print("Attack triggered: " .. attackType)
        end
    end
end)

I didn’t test it tho, so let me know if this works!

Or just use different markers for the different attack/effect types?

okay, what on earth does this do?

^ ensures the match starts at the beginning of the string "attack_" is the literal string being searched for %w+ matches one or more alphanumeric characters and the parentheses () capture and return that part after "attack_" if found

also if no match is found it returns nil

2 Likes

This code (which I assume is AI generated??) uses an AnimationTrack:GetMarkers() function, which does not exist…

1 Like

Yeah it has to be: one time I was curious and used the AI assistant thing to generate code and it seemed that 75% of the time I’d have to rewrite it because it came up with a completely nonexistent function

There is no real functional solution here, assuming you simply want to avoid writing two inline functions, separately when the (main part) of their functionality is the same, you can simply pass the same function as callback for multiple events, like so:

function callback(attackType)
  print(attackType)
  -- do something
end

track:GetMarkerReachedSignal("attack1"):Connect(callback)
track:GetMarkerReachedSignal("attack2"):Connect(callback)
1 Like

I dont know if i remember correctly, but i think you can put parameters into AnimationEvents

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.