(4.5) Trying to create a weapon that if player is in action it creates different weapon animation

Post Reply
User avatar
TolerantX
Posts: 57
Joined: Fri Jan 18, 2019 7:58 am

(4.5) Trying to create a weapon that if player is in action it creates different weapon animation

Post by TolerantX » Tue Jun 30, 2020 6:32 am

This code I know worked before I added more to it. I keep getting branch out of range errors for my +commands

I'm trying to make it so if the player is in action state 2, the attack state already when they attack, then it does a different player animation (*got that working) but then the weapon is suppose to be the other state of the weapon, and it's still only showing the first state of the weapon not the one called for.... This was what it was before I added more code in the beginning. now I have branch out of range errors...
Please help me fix these so I can add punch combos to my game.

Code: Select all

;;;  originally from mod_adventure as ChangeToAttack ;;;

;;;;;;;;;;;;;; Check ActionStep here and if player is in action step 2 then make step 3 combo_1 ;;;;;;;;;;;;;;;;;;;

	TXA
    STA temp ;; assumes the object that we want is in x.
	GetActionStep temp
	CMP #$02		;; is player action step 02 or what state you have set for regular punch? ;;
	BNE +doCreatePunch   ;;; if the player is not in action step 2 do the normal punch animation ;;;
		ChangeActionStep temp, #$03 ;;; assumes that "combo" is in action 2 ;;;
		;arg0 = what object?
		;arg1 = what behavior?
		StopMoving temp, #$FF, #$00

;;;;;;;;;;;;;;;;;;;;;;;;;   End of Check and change player ActionStep is here	 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;    Here we are setting up object to be created.   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  hi,x
                CLC
                ADC #WEAPON_POSITION_LEFT_X
                STA tempA
                LDA Object_y_hi,x
                CLC
                ADC #WEAPON_POSITION_LEFT_Y
                STA tempB
                
                CreateObject tempA, tempB, #WEAPON_OBJECT, #WEAPON_LEFT_STATE, currentNametable
                JMP +doneWithCreatingWeapon
            
;;;;;;;;;;;;;;;;;;;;;;;;;  	 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

+doCreatePunch:


    TXA
    STA temp ;; assumes the object that we want is in x.

    ChangeActionStep temp, #$02 ;; assumes that "attack" is in action 2
    ;arg0 = what object?
    ;arg1 = what behavior?
    StopMoving temp, #$FF, #$00
    
    ;;; some constants for where to create the punch.
    ;;; Constants do not take up any memory.
        WEAPON_POSITION_RIGHT_X = #$10
        WEAPON_POSITION_RIGHT_Y = #$10
        ;WEAPON_POSITION_UP_X = #$00
        ;WEAPON_POSITION_UP_Y = #$F0
        ;WEAPON_POSITION_DOWN_X = #$00
        ;WEAPON_POSITION_DOWN_Y = #$10
        WEAPON_POSITION_LEFT_X = #$F8
        WEAPON_POSITION_LEFT_Y = #$10
        WEAPON_OBJECT = #$03      			;;; This is the object number you are using from game objects ;;;
        WEAPON_RIGHT_STATE = #$00			;;; WEAPON_OBJECT action step number ;;;
        WEAPON_LEFT_STATE = #$01			;;; WEAPON_OBJECT action step number ;;;
        ;WEAPON_UP_STATE = #$00				;;; WEAPON_OBJECT action step number ;;;
        ;WEAPON_DOWN_STATE = #$00			;;; WEAPON_OBJECT action step number ;;;
        
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
;makePunchObjects:		
		
        ;; Now, we have to create the object.
        ;; We will need to determine the direction
        ;; of the player.
        LDX player1_object
        TXA
        STA temp
        GetObjectDirection temp ;; temp still observed from above.
            ;;; this object's direction is now loaded into the 
            ;;; accumulator for comparison after the macro.
            ;; 0 = down
            ;; 1 = downright
            ;; 2 = right
            ;; 3 = upright
            ;; 4 = up
            ;; 5 = upleft
            ;; 6 = left
            ;; 7 = downleft
             ;BNE +notDown ;; jump if not equal to zero.
                ;;; CREATE DOWN WEAPON
                ;;; create an object for the weapon.
                ;;; create it at the down positions.
                ;;; create it with the down state
            
                ;LDX player1_object
                ;LDA Object_x_hi,x
                ;CLC
                ;ADC #WEAPON_POSITION_DOWN_X
                ;STA tempA
                ;LDA Object_y_hi,x
                ;CLC
                ;ADC #WEAPON_POSITION_DOWN_Y
                ;STA tempB
                
                ;CreateObject tempA, tempB, #WEAPON_OBJECT, #WEAPON_DOWN_STATE, currentNametable
                ;JMP +doneWithCreatingWeapon
            ;+notDown
                CMP #$02     				;; direction ;;
                BNE +notRight
                ;;; CREATE RIGHT WEAPON
                LDX player1_object
                LDA Object_x_hi,x
                CLC
                ADC #WEAPON_POSITION_RIGHT_X
                STA tempA
                LDA Object_y_hi,x
                CLC
                ADC #WEAPON_POSITION_RIGHT_Y
                STA tempB
                
                CreateObject tempA, tempB, #WEAPON_OBJECT, #WEAPON_RIGHT_STATE, currentNametable
                JMP +doneWithCreatingWeapon
            +notRight
            ;CMP #$04						;; direction ;;
            ;BNE +notUp
                ;;; CREATE UP WEAPON
                ;LDX player1_object
                ;LDA Object_x_hi,x
                ;CLC
                ;ADC #WEAPON_POSITION_UP_X
                ;STA tempA
                ;LDA Object_y_hi,x
                ;CLC
                ;ADC #WEAPON_POSITION_UP_Y
                ;STA tempB
                
                ;CreateObject tempA, tempB, #WEAPON_OBJECT, #WEAPON_UP_STATE, currentNametable
                ;JMP +doneWithCreatingWeapon
            ;+notUp
            CMP #$06						;; direction ;;
            BNE +notLeft
                ;;; CREATE LEFT WEAPON
                LDX player1_object
                LDA Object_x_hi,x
                CLC
                ADC #WEAPON_POSITION_LEFT_X
                STA tempA
                LDA Object_y_hi,x
                CLC
                ADC #WEAPON_POSITION_LEFT_Y
                STA tempB
                
                CreateObject tempA, tempB, #WEAPON_OBJECT, #WEAPON_LEFT_STATE, currentNametable
                JMP +doneWithCreatingWeapon
            +notLeft
            
        +doneWithCreatingWeapon       
    
    RTS
Thank you for your help and consideration.
Last edited by TolerantX on Tue Jun 30, 2020 9:39 am, edited 1 time in total.
User avatar
jorotroid
Posts: 265
Joined: Wed Aug 08, 2018 7:48 pm
Location: California
Contact:

Re: (4.5) Trying to create a weapon that if player is in action it creates different weapon animation

Post by jorotroid » Tue Jun 30, 2020 7:11 am

Whenever you use a branch command like BNE or BEQ, there is a limit to how far away your label can be. About 127 addresses either up or down. When you get the error, either you have to find a way to optimize your code so the label is within the limit, or you can reverse your logic so the branch points to the code near by and then it uses a JMP to go to the code that is farther away because the JMP command doesn't have any distance limitations. So for example, If you have some code like

Code: Select all

	CMP someVariable
	BEQ codeThatIsFarAway
and that gives you an error, you can change the BEQ to a BNE and do this:

Code: Select all

	CMP someVariable
	BNE continueWithNearbyCode
	JMP codeThatIsFarAway
	
continueWithNearbyCode:
More specifically with your code at a glance, I would guess you are getting the error on the BNE +doCreatePunch line near the top. If so, I would change the code to look like this:

Code: Select all

	GetActionStep temp
	CMP #$02
	BEQ +continue
	JMP +doCreatePunch
+continue:


Slight disclaimer for something I'm not sure about: So, one thing I'm not sure about with my modified code above is the use of the + during a JMP command. I've just never done that, so I don't know if it works. I don't like to use +'s at the start of my labels because those kinds of labels can't be extracted to be used in the Mesen debugger. The point of the + labels are so you can reuse a convenient label name over and over in the code and it will only apply to area of the code that you use it in. I feel like that means it shouldn't work with a JMP command, but I don't know. You might have to get rid of the + on the doCreatePunch label.
User avatar
TolerantX
Posts: 57
Joined: Fri Jan 18, 2019 7:58 am

Re: (4.5) Trying to create a weapon that if player is in action it creates different weapon animation

Post by TolerantX » Tue Jun 30, 2020 9:40 am

Thank you man! I fixed it and got it to work. Man it's been wracking my brain. Thank you so much.
User avatar
dale_coop
Posts: 5386
Joined: Fri Feb 16, 2018 7:05 am
Location: France

Re: (4.5) Trying to create a weapon that if player is in action it creates different weapon animation

Post by dale_coop » Tue Jun 30, 2020 10:20 am

jorotroid wrote:
Tue Jun 30, 2020 7:11 am
Slight disclaimer for something I'm not sure about: So, one thing I'm not sure about with my modified code above is the use of the + during a JMP command. I've just never done that, so I don't know if it works. I don't like to use +'s at the start of my labels because those kinds of labels can't be extracted to be used in the Mesen debugger. The point of the + labels are so you can reuse a convenient label name over and over in the code and it will only apply to area of the code that you use it in. I feel like that means it shouldn't work with a JMP command, but I don't know. You might have to get rid of the + on the doCreatePunch label.
The + means the next label (if "JMP +theLabel" the code will jump to the next label "+theLabel" it finds in the code.... it's just just using "JMP +"... for small jumps for dealing with the branch errors) and you can reuse those +labels.
-----
Sorry about my poor english
All I need: A Damn Fine Cup of Coffee
My games: PRESS START GAME / UNDERGROUND ADVENTURE / UNDERGROUND ADVENTURE (Arcade version - Byte-Off-2019)
My son's game: KUBO 3
User avatar
jorotroid
Posts: 265
Joined: Wed Aug 08, 2018 7:48 pm
Location: California
Contact:

Re: (4.5) Trying to create a weapon that if player is in action it creates different weapon animation

Post by jorotroid » Wed Jul 01, 2020 10:42 am

dale_coop wrote:
Tue Jun 30, 2020 10:20 am
The + means the next label (if "JMP +theLabel" the code will jump to the next label "+theLabel" it finds in the code.... it's just just using "JMP +"... for small jumps for dealing with the branch errors) and you can reuse those +labels.
Ah, thanks for the clarification, Dale! I had assumed they worked for within the 255 address range of a branch command backwards or forward, not that they just applied to the next appearance of the label.
Post Reply