S2ENGINE HD  1.4.6
Official manual about S2Engine HD editor and programming
Weapons

Just as items, weapon are components of player invenory.
They are a little different from items because items are used one time and just when selected from inventory, while weapons remains in player hands after selected and can be shot any time it is needed

Weapon and WeaponTAG

WeaponTAGs are the corresponding itemTAGs but for weapons.
They are the weapon representation inside the scene and they are needed for letting player pick up the related weapon.

Weapons are corresponding itemObjects but for weapons.
Differently from ItemObjects, Weapons must not be in form of prefabs to be instanced when selected, but they are placed inside the scene, precisely Weapons are always on the player's hands.
This means that the Weapon object must be attached to playerCharacter hands node.
PlayerCharacter hides all weapons at start, a weapon is showed only if it is taken and when it is selected from inventory.

To be 100% convincing a weapon must owns an sh2 model containing 4 nodes, BUT you can omit then if you want:

  • MuzzleNode
  • ProjectileNode
  • BulletBoxNode
  • LoaderNode: it is used for showing/hiding weapon loader during ammo re-loading.

Usually loader node is used in this way:

  1. A loader model is attached to an hand node of the player, a loader mesh is on a node of the weapon object model.
  2. When using the weapon the loader node in the players hand is hidden, the loader mesh in weapon object is showed
  3. when reloading the weapon the loader node in player hand is showed while the loader mesh in weapon object is hidden.
weapon.jpg
Note
usually muzzle and projectile nodes are the same

Weapon AmmoTAG

WeaponAmmoTAG are special kind of TAG used for weapon ammo.
They are implemented in a very simple way: when taken, automatically a new loader is added to the corresponding weapon, even if player haven't that weapon yet. This is achieved making the weaponAmmoTAG to send a SUPPLY_AMMO message to the player when it receives an ACTION message from the player. A weaponAmmoTAG has a reference to the weapon object it must supply loaders.

Adding a new weapon

Weapon Logic

Weapons are the most complex part of the DLC. This is due to the fact that weapons are composed by many parts (models, animations, logic, spawing objects such as projectiles, particle systems, etc..) and all these must interface with GameMachine for letting user to have the maximum control.

Weapon Pick up

When player takes the weapon ( i.e. it sends the ACTION message to the aimed weaponTAG ), weaponTAG replies with a ARM message specifying the related weapon object (attached to players hand) name and weapon caption into the message content.

Use weapon

Before using the weapon, of course, it must be selected and grabbed from inventory. There are 2 ways for grabbing weapons from inventory:

  • By showing the inventory, selecting the weapon item slot and press the use command
  • By using the ChangeWeaponAxis command (that can be set in the player character parameters).

When a weapon is grabbed playerCharacter sends a CHANGE_WEAPON event to its FSM (see Player Sending Events) for notifying the FSM that a new weapon has been choosed, this is useful, for example, in the case you want player to perform an animation for changing weapon.
When the FSM receives the event CHANGE_WEAPON it can send the selectWeapon message to the player for making it to hide previous weapon and showing the current selected weapon, also when playerCharacter receives the selectWeapon message it replies to FSM with an event named weapon_weaponName where weaponName is the name of the current selected weapon (see Player Sending Events), this is needed to inform FSM what is the current selected weapon, so FSM can change state according to the new weapon.

The image below shows the flow of this mechanism, taken from the playerCharacter asset provided into the EasyGame sample project:

change_weapon.jpg
  • 1. A CHANGE_WEAPON event is processed
  • 2. From previous state a transition to a WEAPON_CHANGE state is performed. In this state it is executed an animation for changing weapon (for example a pose with hands low).
  • 3. An ANIMATION_FINISHED event from WEAPON_CHANGE state is fired and a transition to a new state WEAPON_SELECT is performed.
  • 4. In the WEAPON_SELECT state a selectWeapon message is sent to playerCharacter (FSM owner).
  • 5. The playerCharacter replies with a new event called weapon_weapon02, so from WEAPON_SELECT state there is a transition to WEAPON_MITRA_STAND that is the state that manages the weapon02 use.

Weapon Firing

To shoot projectiles weapon must receive a message called shoot. When receiving this message a weapon spawn various prefabs:

  • Projectile: specified into the ProjectilePrefab parameter of the Weapon object class.
  • Bullet Box: specified into the BulletBoxPrefab parameter of the Weapon object class.
  • Muzzle: usually a particle system. Specified into the MuzzlePrefab parameter of the Weapon object class.

How to make weapon receiving shoot message?
Usually the projectile is shot just at the beginning of the shooting animation so you could proceed in this way:

shoot.jpg
  1. produce a shoot event, for example using InputAction checking, i.e. when user clicks on fire button.
  2. create Shoot state that receives the shoot event
  3. the Shoot state sends the shoot message to the weapon

Melee weapon

Weapon can also receive a message called Throw. When receiving this message the weapon sends an HIT event to hit objects in 2 ways, basign on the parameter MeleeFromCamera:

  1. MeleeFromCamera is FALSE: in this case all object that are inside a range from player (defined by MeleeRange parameter) and are in front of player receive the HIT event. It is useful for 3dr person character games
  2. MeleeFromCamera is TRUE: in this case a raycast from camera node is done and an HIT event is sent to collided object. This mode is useful in fist person games.
Note
in the case 1. the meleeNode parameter must be specified. It is the node from which are generated the effects of the hit (for example particle systems). It must be on the weapon. The following image is an example, taken from the mace.sh2 file used into the demo project game, about how melee node must be made:
melee.jpg