Customizable Attack (Multiple Attacks, Objects, Etc) [4.5.9]

crazygrouptrio

Active member
Nothing too exciting here, just thought I'd share my modified version of the attack script from the Brawler module, it can be used in Platformers as well. It could be used in other modules if you edit it enough.
I've set it up to be more customizable without using more space (as-is it may actually be smaller than the original script).
For example: You can add multiple objects, object states, spawn points, etc.

First of all just add this to your Zero Page Ram:
Code:
tempCGT
Here is the attack script. Just copy it and add it to your input scripts, and set it to the gamepad like normal.
Code:
    TXA
    STA temp ;; assumes the object that we want is in x.
    ; here we're going to get the player's direction and store it to determine states
    ; if your object doesn't use different states, you can comment out these and other tempCGT lines
        GetObjectDirection temp
        STA tempCGT
    ; set which action steps do not allow attacking, or what attacks need to be performed
    ; I'll just display 2 attacks, but you can add more and change them, as long as they
    ; are set up like the attacks below.
    GetActionStep temp
    CMP #$02 ; jumping
    BEQ setJumpAttack ; we have a jump attack separate from regular attack so we "jump" to that
    CMP #$03 ; attacking
    BEQ noAttack
    CMP #$05 ; crouching
    BEQ noAttack
            ; nothing else interrupted so normal attack state
        ChangeActionStep temp, #$03
            ; my attacks require a different Y coord, so we'll set it here for regular attack
        LDA #$09
        STA tempy
            ; set the action state here
            ; we're checking for left or right to determine which state to make
        LDA tempCGT
        CMP #$02 ; check for right
        BEQ +facingRight
        LDA #$01 ; left state weapon 1
        JMP setWeaponState
        +facingRight
        LDA #$00 ; right state weapon 1
        JMP setWeaponState
    
    noAttack:
    RTS
    
    setJumpAttack:
        ChangeActionStep temp, #$06
            ; my objects use a different Y coord, so we'll set it here
        LDA #$1F
        STA tempy
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            ; if this uses a different object, you can set that here, for example:
            ; LDA "your object #"
            ; STA "variable of your choice"
            ; then add your variable in the create object macro further down,
            ; replacing the "#$02", as long as all attacks store to that variable as well
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            ; set the action state here
            ; my jump attack uses the same blank action state,
            ; so we don't need to check direction for what state to draw
        LDA #$04 ; state for weapon 2
        JMP setWeaponState
    
    setWeaponState:
            ; this stores the weapon object for all attacks
        STA tempC
    
    continueAttack:
            ; if you want to stop moving uncomment this line,
            ; though I've never seen this actually work outside of the brawler module
            ; StopMoving temp, #$FF;, #$00
        
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;; Now, we have to create the object.
        ;; We will need to determine the direction
        ;; of the player.
        ;; We already have this loaded into tempCGT, so we can just check that
                LDA tempCGT
                CMP #$02
                BNE +notRight
                ;;; CREATE RIGHT WEAPON
                ;;; now that we know the direction we can set the X value here
                LDA #$10
                STA tempx
                
                LDX player1_object
                LDA Object_x_hi,x
                CLC
                ADC tempx
                STA tempA
                LDA Object_screen,x
                ADC #$00
                STA tempD
                LDA Object_y_hi,x
                CLC
                ADC tempy
                STA tempB
          
                    ;; we're actually creating the object now
                    ;; this is the macro defined to help keep track of what's what
              
                    ; CreateObjectOnScreen x, y, object, state, currentNametable
                  
                    ; all of these variables should be defined above
                    ; in my case all weapons are stored in object #$02, but you can
                    ; swap it out for your own weapon # or defined variable
                    
                   CreateObjectOnScreen tempA, tempB, #$02, tempC, tempD
                   LDA #%11000000
                   STA Object_direction,x
                   JMP +doneWithCreatingWeapon
            +notRight
            
                ;;; CREATE LEFT WEAPON
                ;;; now that we know the direction we can set the X value here
                LDA #$F9
                STA tempx
                    
                LDX player1_object
                LDA Object_x_hi,x
                CLC
                ADC tempx
                STA tempA
                LDA Object_screen,x
                SBC #$00
                STA tempD
                LDA Object_y_hi,x
                CLC
                ADC tempy
                STA tempB

                   ;; we're actually creating the object now
                   ;; this is the macro defined to help keep track of what's what
              
                   ; CreateObjectOnScreen x, y, object, state, currentNametable
                  
                   ; all of these variables should be defined above
                   ; in my case all weapons are stored in object #$02, but you can
                   ; swap it out for your own weapon # or defined variable
                  
                 CreateObjectOnScreen tempA, tempB, #$02, tempC, tempD
                 LDA #%10000000
                 STA Object_direction,x
                 JMP +doneWithCreatingWeapon
            +notLeft
            
        +doneWithCreatingWeapon
        ; use your attack sound here. swish, zap, pow!
        PlaySound #2
    
    RTS
The code has several comments that explain what does what where, and it will be up to you to set everything up to fit your game. Many of the values used in the script are just placeholder that I used for my game. You can add more attacks of different varieties as long as they follow the same setup I've provided.
This script DOES use many variables (tempx, tempy, tempA, etc). It's never caused problems for me, but if it does just create new variables and use those instead.
 
@crazygrouptrio hoping you can help me make sense of this, as I’m sure I’m overthinking it?
For my other button, I’m not trying to do anything crazy. Just an alternate punch. Same fist, same position as presets, just a different animation for the punch. (Punching with right fist instead of left fist)

I feel like the answer is somewhere between the answer above and the existing input. Thanks for any help on this.
 

crazygrouptrio

Active member
@crazygrouptrio hoping you can help me make sense of this, as I’m sure I’m overthinking it?
For my other button, I’m not trying to do anything crazy. Just an alternate punch. Same fist, same position as presets, just a different animation for the punch. (Punching with right fist instead of left fist)

I feel like the answer is somewhere between the answer above and the existing input. Thanks for any help on this.
Simple answer would be to just use a different action step for that animation (i.e. left fist is action step 2, right fist is action step 3). If you're wanting to use this same script for both buttons to save space you could add a check to see what button is being pressed in the script to use a different action step that corresponds with the button (LDA gamepad, AND $%whatever input) since that should be the only difference.
 
Got it! This was a huge help as I was definitely overthinking. For anyone trying to do this, what I did was:
- copy the attack script and rename (ie. altattack)
-where it mentions assuming that attack is action 2, I just set it at and changed that number to a 3.

that’s it. Thanks again. Now I can throw a solid right and left jab.
 
Ran into one issue. What should I do in the below code for changeactiontoStopMoving_brawler base? In the tutorial, I know the note "don't change to walk if you are punching" is what stops you from moving while punching. Now I need that to happen for my alternate punch.

I tried adding a line in there to include my Action 3/Alt Punch, but that only broke the script and caused me to keep moving regardless of which punch I used.

Code:
    TXA
    STA temp ;; assumes the object we want to move is in x.
 GetActionStep temp
    CMP #$07
    BNE +notHurt
        RTS
    +notHurt
    CMP #$02
    BEQ +skip ;; don't change to walk if we are punching.
    CMP #$01
    BEQ +alreadyInWalkState
        ChangeActionStep temp, #$01 ;; assumes that "walk" is in action 1
            ;arg0 = what object?
            ;arg1 = what behavior?
    +alreadyInWalkState
    +skip
    RTS
 

crazygrouptrio

Active member
Honestly I've never been sure what exactly stops the player while attacking, as the StopMoving macro in the default attack script doesn't seem to do anything whether it's there or not. Many others have had the issue and provided different solutions here in the forums.
 
Top Bottom