Please give some tips about C++ code for 2D game

I am beginner for UE4 program, now I am trying to learning how to use C++ to implement a simple 2D action side-scrolling game. The first step for that is try to set up a main character. I used 2D sprite and flipbook, then try to code some base function such as idle, walk, jump, and so on. I had a simple concept:

I also wrote some code for that but it didn’t work well, for example:
in character class. I used some bool to control the animation status:

// Custom Params
IsLanding = true;
IsWalking = false;
IsRunning = false;

IsStartJump = false;
IsRising = false;
IsStartFalling = false;
IsInAir = false;
IsFalling = false;

in Tick() function, I checked these variables.
The I used my custom function “UpdateAnimation” to set Flipbook. just like follow code:

// Standing
if (IsLanding && !IsRising && !IsRunning && !IsWalking)
{
	GetSprite()->SetFlipbook(IdleAnimation);
}
// Walking
if (IsLanding && IsWalking && !IsRising && !IsRunning)
{
	GetSprite()->SetFlipbook(WalkingAnimation);
}

// Running
if (IsRunning && IsLanding) 
{
	GetSprite()->SetFlipbook(RunningAnimation);
}

// Jump start
if (IsStartJump && !IsRising && !IsStartFalling)
{
	GetSprite()->SetFlipbook(JumpStartAnimation);
}
// Jump rising
if (IsRising && !IsStartFalling && !IsStartJump)
{
	GetSprite()->SetFlipbook(JumpRisingAnimation);
}
// Jump in air and start falling
if(IsStartFalling && !IsFalling && !IsStartJump)
{
	GetSprite()->SetFlipbook(JumpInAirAnimation);
}
// Falling
if(IsFalling)
{
	GetSprite()->SetFlipbook(JumpFallingAnimation);
}

Did I have mistakes or logic problem? Can somebody give me some tips for coding. Thx first.

By the way, the resource I used were downloaded from website, these were extracted from other games, I just used it to learn and find some fun. :slight_smile:

Hi XTijang,

what you’re trying to do is a State Machine. You can see how to do that in Blueprints in the 8th video of this great Zack Parish series.

In your case I would first consider refactoring animation statuses into an enum (short for enumeration). Your custom params would become something like:

UENUM()
enum AnimationType
{
  Landing,
  Walking,
  Running,
  StartJump,
  Rising,
  StartFalling,
  InAir,
  Falling,
  Idle
};

This goes in your character header file. Now your can add a variable to your character header file, that will hold an enumeration value:

UPROPERTY()
AnimationType ActiveAnimation = AnimationType::Idle;  // set default to Idle (standing)

Now you can use a switch statement, and use the ActiveAnimation variable to set the right animation flipbook.

switch(ActiveAnimation ) {
    case AnimationType::Walking : 
			GetSprite()->SetFlipbook(WalkingAnimation);
            break;
	case AnimationType::Running :
			GetSprite()->SetFlipbook(RunningAnimation);
            break;
	case AnimationType::StartJump :
			GetSprite()->SetFlipbook(JumpStartAnimation);
            break;
	case AnimationType::Rising :
			GetSprite()->SetFlipbook(JumpRisingAnimation);
            break;
	case AnimationType::InAir :
			GetSprite()->SetFlipbook(JumpInAirAnimation);
            break;
	case AnimationType::Falling :
			GetSprite()->SetFlipbook(JumpFallingAnimation);
            break;
	default: 
			GetSprite()->SetFlipbook(IdleAnimation);
            break; 
}

Making these changes will require you to change your logic about setting the IsLanding, IsWalking,IsRunning, … variables. You will have to change the characters ActiveAnimation variable now. For example, if you wanted to set your character to falling, you would set it something like this:

ActiveAnimation = AnimationType::Falling;

There are other ways to code this logic, of course :slight_smile:

But before you make any big changes, make sure you have a copy of your work, or consider using a versioning system like Git or Subversion. (It’s like having a save game, but for your C++ code :D)

Happy coding!

Thank you very much! I am beginner for writing code, your tips are very important for me, I will try it today. Cool~~~~~
Thx, Hawba :slight_smile: