Title
GB Studio 3.1+ Tutorial.
Easy sword attacks in Adventure mode.
Last updated 2/2/2023.
Easy sword attacks in Adventure mode.
Last updated 2/2/2023.
Introduction
This tutorial was written for GB Studio 3.1.0 and is subject to change! A video version of this tutorial is available here:
The addition of persistent projectiles in 3.1 made melee attacks much simpler to set up. This tutorial guides you through setting up a simple sword attack for Adventure mode, and will also work for Top-down mode. It involves four main steps:
The addition of persistent projectiles in 3.1 made melee attacks much simpler to set up. This tutorial guides you through setting up a simple sword attack for Adventure mode, and will also work for Top-down mode. It involves four main steps:
- Setting up the Player and Sword sprites
- Setting up the Player script
- Setting up the Enemy script
- Adding polish
Setting up the Player and Sword sprites
Set up the Player like a regular Top-down or Adventure mode player by setting the Animation Type to 'Four Directions + Movement' and adding the sprite tiles as desired.
For this tutorial, the Player's canvas size should be 16px*16px, and the collision box size should be 16px*16px or less.
Set up the Sword sprite by setting the Animation Type to 'Four Directions' and add the sprite tiles as desired
The canvas size should be 16px*16px, and the collision box size should be 14px*14px or less.
Center the collision box on the sprite, in this example by setting both its X and Y to 1.
Giving the Sword a collision box of 14px*14px or less ensures that there is a 1px gap between the Player's collision box and Sword's collision box.
This prevents some collision inconsistencies caused by the Sword overlapping the Player in certain circumstances.
Setting up the Player script
Prepare a new Top-down or Adventure type scene with the Player sprite sheet that you set up previously.
Add the below script to the scene's On Init script, ensuring that the checkboxes and projectile options match the image.
A simple explanation of the script is as follows:
- When the B button is pressed
- Override the Dpad with a blank script so the player cannot move
- Fire a projectile with the Sword sprite
- Wait 0.5 seconds
- Restore Dpad function so the player can move again
- Animation Speed- Change this to affect how fast the Sword animation plays.
- Life Time- Change this to affect how long the Sword stays out. Also change the length of the Wait event to match.
- Loop Animation- Change this if you want a looping animation for the Sword (might be good for another type of melee attack).
- Collision Group- Changes the Collision Group of the Sword, allowing it to be distinguished from other projectiles in other Collision Groups.
- Collide With- Changes the Collision Groups that the Sword is able to hit. Should match the Collision Group of enemies or other things that can be hit with the Sword.
Setting up the Enemy script
Add an actor to the scene that can be used for the Enemy. Set the Collision Group of the Enemy to 1.
The Collision Group of the Enemy should match one of the Collision Groups specified in the Collide With option of the Sword projectile event.
In other words, the Sword projectile should Collide With the Enemy's Collision Group.
Add the below script to the Enemy's On Hit script under the Collision Group 3 tab. This should match the Collision Group of the Sword projectile event.
A simple explanation of the script is as follows:
- Play sound effect
- Deactivate the Enemy actor until the scene is reloaded
Adding polish
Finally, here are a few options to improve on the basic sword attack.
You could add a swing animation to the Sword sprite in the sprite editor.
You could add an attack animation state to the Player sprite. Add 'Set Actor Animation State' events to the Player script to change to it when attacking.
You could add health and invincibility frames to the Enemy. Below are examples of two scripts that achieve this when added to the Enemy actor. The first should be added to the Enemy's On Init script. A simple explanation of the On Init script is as follows:
You could add a swing animation to the Sword sprite in the sprite editor.
You could add an attack animation state to the Player sprite. Add 'Set Actor Animation State' events to the Player script to change to it when attacking.
You could add health and invincibility frames to the Enemy. Below are examples of two scripts that achieve this when added to the Enemy actor. The first should be added to the Enemy's On Init script. A simple explanation of the On Init script is as follows:
- Set a local variable representing health of the Enemy to 4
- Set a local variable representing whether the Enemy is invincible or not to 0
- If the Enemy is invincible
- Stop this script, so the Enemy will not take damage
- Else
- Play sound effect
- Decrement health by 1
- If the Enemy is invincible
- Deactivate the Enemy actor until the scene is reloaded
- Else
- Make the Enemy invincible
- Wait 0.5 seconds, or the duration of the invicibility frames you want
- Remove the Enemy's invicibility
More resources
Now you can make a simple sword attack in GB Studio, but you will need more than that to make a game.
The following tutorial will show you how to make better enemies in Adventure mode:
If you have any queries about the tutorial contents (NOT general questions about GB Studio) contact me via my Twitter. Constructive feedback is appreciated.
If you have any queries about the tutorial contents (NOT general questions about GB Studio) contact me via my Twitter. Constructive feedback is appreciated.