Casual Player State Change [4.5.9] [Intermediate]

crazygrouptrio

Active member
Following this tutorial you will be able to make your player "casually" change states, as in the player can continue to move but will change state, animations, movement animations, or how the game behaves in these different states (neat right?). Here's an example of what can be done:
jvovMDQ.gif

In this example the player "casually" changes its state just by moving to each side of the screen making a 3D perspective effect AND the player's movement animations reflect what state the player is in as the player's movement animations are different for the left, right, and center of the screen. This could do ALL KINDS of things that I hope is useful for others (changing the player to a "hiding" state, casually changing from walking to swimming, standing in water and making splashes, just a few examples that I can think of doing with this method). It's also worth noting that this method DOES NOT USE UP ANY ADDITIONAL ACTION STATES. Which I consider a pretty huge bonus.



REQUIREMENTS:
1. A minimum of 2 tiles
2. Unused player directions (if your player uses all 8 face directions this will not work as-is. The point of this is to utilize potentially unused face directions)

This script does not use a lot of code (which is nice) but it does require that we bounce around 4 or 5 different scripts and make them work in tandem. So make backups and whatnot.



STEP 1
Add this User Variable
playerState
In the comments you may want to add which number corresponds to which state. 0 = Normal, 1 = Hiding, 2 = Swimming etc. Whatever you plan to do with it. This single variable controls the states and how the game behaves, we'll talk about this later.

STEP 2
Select an open tile and use this script for that tile.
Code:
CPX player1_object
    BNE +skip
    LDA #$00
    STA playerState
    +skip:
Select a second tile and use this script for that tile.
Code:
CPX player1_object
    BNE +skip
    LDA #$01
    STA playerState
    +skip:
Each of these tiles will change the player's state upon touching them. We'll talk about placement of these tiles later.

STEP 3
Go to script settings, track down doUpdateSpriteTimer.asm and at the very end of the script after "animationTimerNotFinished:" add this script:
Code:
    TXA
    STA temp
    LDA playerState
    CMP #$00
    BEQ +stateZero ; we're in state 0
    CMP #$01
    BEQ +stateOne ; we're in state 1
    ; add more state checks here
    JMP doneStating
    +stateZero:
    ;ChangeFacingDirection temp, #FACE_UP ;whichever unused direction you want to use
    JMP doneStating
    +stateOne:
    ChangeFacingDirection temp, #FACE_UPLEFT ; like I said, another unused direction
    JMP doneStating
    doneStating:
What this script does is check the direction variable and changes the player's direction (in this case, they're state) if the player passes those tiles. Here's where things are going to start getting harder to pertain to each person's game, as people will be using different directions. I'm using #FACE_UPLEFT in my example and I've commented out #FACE_UP so that it just defaults to normal (I use UP as a default in the example above), just make sure you're using the directions you want to use. It doesn't matter as long as these directions aren't being used by your player's main action (i.e. if you're using platformer module you're probably using left and right, so up and up left would be fine. if you're in an adventure you're probably using up, down, left, and right, so instead you could use up left and up right. You get the idea.)

STEP 4
Now go to your input scripts. If you aren't lost yet you may get lost here. Right now the player will change if they pass over those tiles, BUT we need the player's movement scripts to also reflect those changes, and by default your player will change to state 0 if they stop moving and start moving again. You'll want to put code like this wherever your face direction is handled (either start/stop moving, or change action to moving/stopping, etc). Here is my StopMoving script for an example:
Code:
;;;;
    TXA
    STA temp ;; assumes the object we want to move is in x.
   GetActionStep temp
    CMP #$07
    BNE +notHurt
        RTS
    +notHurt
    StopMoving temp, #$FF, #$00
    ChangeActionStep #$00, #$00
    LDA playerState
    CMP #$00
    BEQ +stateZero ; stop in state 0
    CMP #$01
    BEQ +stateOne ; stop in state 1
    JMP doneStopping
    +stateZero:
    ;ChangeFacingDirection temp, #FACE_UP
    JMP doneStopping
    +stateOne:
    ChangeFacingDirection temp, #FACE_UPLEFT
    JMP doneStopping
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    doneStopping:
    RTS
Here in State 0 the player goes back into their idle state if they stop, unless playerState is set to 1, then it stops the player facing #UPLEFT which will be a different animation/state. I commented out the #FACE_UP line because in this case we're just using State 0 to return to idle. But I left it in to show how to add multiple states. I hope you aren't lost yet.
NOW here is an example of the movement script, which is pretty much the same thing:
Code:
    TXA
    STA temp ;; assumes the object we want to move is in x.
    
        StartMoving temp, #LEFT
        ChangeActionStep #$00, #$01
          TXA
        STA temp ;; assumes the object we want to move is in x.
         LDA playerState
    CMP #$00
    BEQ +stateZero
    CMP #$01
    BEQ +stateOne
    JMP doneLefting
    +stateZero:
    ;ChangeFacingDirection temp, #FACE_UP
    JMP doneLefting
    +stateOne:
    ChangeFacingDirection temp, #FACE_UPLEFT
    JMP doneLefting
    doneLefting:

    RTS
In the examples I used, I did not use ChangeActionToMoving/Stopping scripts because I didn't need them in my project. If you do, just adapt the above scripts to fit your needs. (basically LDA playerState and below) and apply this to whatever movement scripts you use. (Like I said above, if its a platformer probably just Left and Right, something with Jumping/landing. If its adventure up, down, left and right, etc. )

STEP 5
You still with me? We're almost there! Now go ahead and make your animations you want to use in these states. In this tutorial we're just using UPLEFT for State 1, so draw something to reflect the player NOT MOVING in this state and the player MOVING in this state (i.e. player swimming idly, the player swimming moving) And just assign those animations to UPLEFT in animation info for both Idle and Moving Animation Types for your player.

It's worth noting here that this does not at all mean that one state only applies to one animation (as in UPLEFT can only be state 1) this is not at all the case. I'm just doing it this way for simplicity's sake. For example if you have a 4 direction game and you want your player to behave differently in state 1 and move 4 directions, you can use state 1 in UPLEFT, UPRIGHT, DOWNLEFT, and DOWNRIGHT to represent the new UP, DOWN, LEFT, and RIGHT. Just make sure your movement scripts reflect this. It's a little confusing, but the point is those diagonal direction facings are not hardwired to move your character in those directions. In the gif above I used UPLEFT and DOWNLEFT to represent the ship moving up and down on the left side of the screen for example.

STEP 6
Now for the tiles! The tiles only need to be drawn in a line that separates in the areas where you want the player to behave differently.
lst8IF8.jpg

As you can see here, 0A tile is my State 0 and 0B tile is my State 1. When the player passes to the left, the last tile they touch will assign the playerState variable to 1 so the player will remain in state 1 until they pass the 0A tile going right and will stay until they pass back.. Make sense? (i.e. if you want your player to make water splashies in state 1, the outside of your water area would need to be your State 1 tile, and surrounding THOSE tiles would be your State 0 tile to make sure your player switches back to being a land walker.) I also like this method as it frees up the "inside" tiles for other things like warp, etc.

STEP 7
GET CREATIVE. This is the basic setup that puts this system in place. If you set it all up correctly your player should shift to State 1 upon touching the tile (which in this case means the player is actually facing UPLEFT) and leaving the area should switch them back. Neat. Now what?
First of all you can add more states (my gif at the top uses 3 states for left, middle, and right side of screen). And makes this script actually pretty powerful is that you can just use our playerState variable to change how your game behaves and plays. For example: say we want the player to be "in hiding" when they enter State 1. At the beginning of your ShootAtPlayer script you can add this:
Code:
LDA playerState
CMP #$01
BNE + ; its state 0 to continue as normal
RTS ; its state 1 so we're done here
+
Since the player is in State 1 and "in hiding" the enemy would stop shooting at the player. Here is where I hope you all get creative! You could do things like slow the player down while in water, change background colors, alter physics; all just by checking the playerState variable. Which is why I say it's a "casual" way of changing states in mid game. Good luck everyone!
 
Last edited:
Top Bottom