Nyra 8 Direction
Nyra 8 Direction is a behavior that moves a character seen from above, in 8 directions, in an online game where the server decides the positions. Your character moves straight away when you press the keys, and the behavior keeps it in line with the server without visible jumps. The other players move smoothly.
Note
The behavior only works if the server is in 8 directions mode. You choose it on the Configuration page of the panel, in the Movement mode.
Information
| Type | Behavior (you add it to a Sprite object) |
| Name in Construct | Nyra 8 Direction (General category) |
| Version | 0.5.0.0 |
| Construct version | r495 or newer |
| Needs | The NyraServer object in the project (version 0.5.0.0 or later) |
Adding it to an object
- Install the addon with the Addon manager (see Installing the addons).
- Select your Sprite. In the properties bar open the Behaviors tab and press Add / Edit.
- Choose Nyra 8 Direction.
The Sprite must have the origin at the centre (in Image points, the Origin point). The X and Y position of the object is the centre of the character. The behavior does not rotate the Sprite.
Note
The walls come from the server level, not from Construct objects. Construct's Solid behavior is not needed here.
Properties
| PROPERTY | WHAT IT DOES |
|---|---|
| Default controls | If it is on (the default), the arrow keys move the character and Shift makes it run, without writing any events. The behavior sends the keys to NyraServer by itself: for the same keys do not also use the Set control action of NyraServer. If it is off, use the Simulate control action. |
| Facing directions | How many directions your animations have: 8 (the default), 4 or 2. With 4, on the diagonals the horizontal side wins. With 2 only right and left count: going straight up or down keeps the last side. |
| Enabled | If it is off the character stands still, the keys are released and nothing is sent. The link to the player stays. On by default. |
| Correction smoothing | How much of the difference with the server is corrected at each frame (at 60 fps), from 0 to 1. Default 0.2. It also applies to the smooth movement of the other players. |
| Correction dead zone (px) | For your character: a difference smaller than this value is ignored. Default 5. |
| Snap threshold (px) | A difference equal to or greater than this value makes the character jump to the server position. Default 100. |
There are no speed properties: the speed, the size of the collision area and whether players block each other are decided by the server, and you set them on the Configuration page.
Conditions
Link
| CONDITION | WHAT IT DOES |
|---|---|
| Is linked | True while the instance is linked to a player. |
| Is local player | True if the linked player is yours (movement is predicted). False for another player. |
| On linked | Fires when the link is complete. |
| On unlinked | Fires when the link ends: with Unlink, for a disconnection, for a level change or because the player left. The instance has stopped. |
| On error | Fires on every error. See ErrorCode and ErrorMessage. |
Input
| CONDITION | WHAT IT DOES |
|---|---|
| Is enabled | True if the behavior is on. |
State
| CONDITION | WHAT IT DOES |
|---|---|
| Is moving | True while the character is moving. |
| Is running | True while the character is moving at running speed. |
| Is facing | True if the character is facing that direction. Parameter Direction: Right, Down-right, Down, Down-left, Left, Up-left, Up, Up-right. It compares the facing already reduced by Facing directions: with 4 or 2 directions only some entries can be true. |
| On started moving | Fires when the character starts moving. |
| On stopped moving | Fires when the character stops, even if the link ends while it was moving. |
| On facing changed | Fires when the facing direction changes, already reduced by Facing directions. |
Actions
Link
| ACTION | WHAT IT DOES |
|---|---|
| Link to player | Links this instance to a player. Parameter Player id: the player identifier, for example NyraServer.MyPlayerId for your character or NyraServer.PlayerId for another one. The instance jumps to the player position and from then on follows it: your character is predicted and lined up with the server, the others are moved smoothly. If the player is not known yet, the link waits and an orange unknown-player warning appears in the console, which does not fire On error; if the sign-in ends without the player appearing, the link is discarded. |
| Unlink | Breaks the link (On unlinked fires) and the instance stops. If it was not linked it gives the not-linked error. |
| Set server target (manual) | For those who do not want to use Link to player. Parameters X, Y (centre of the character), Facing (degrees, a multiple of 45; 0 right, 90 down), Moving and Running (1 yes, 0 no). The instance behaves like another player: smooth movement only, no prediction. It cannot be used while the instance is linked. |
Input
| ACTION | WHAT IT DOES |
|---|---|
| Simulate control | Presses or releases a key from an event. Parameters Control (Left, Right, Up, Down, Jump, Run) and State (1 pressed, 0 released). You use it when Default controls is off. Jump is accepted but in 8 directions it does nothing. |
| Set enabled | Turns the behavior on (1) or off (0). Off: the character stands still, the keys are released and nothing is sent. |
Expressions
You write them with the name of the object and of the behavior, for example Avatar.Nyra8Direction.Facing. None of them has parameters.
| EXPRESSION | WHAT IT RETURNS |
|---|---|
PlayerId | The identifier of the linked player (the last one, even after Unlink). |
ErrorCode | The code of the last error. |
ErrorMessage | The message of the last error. |
Facing | The facing angle in degrees (0 right, 90 down), already reduced by Facing directions. |
FacingName | The name of the facing: right, down-right, down, down-left, left, up-left, up, up-right. |
VelocityX | The horizontal speed, in pixels per second. |
VelocityY | The vertical speed, in pixels per second. |
How to use it
The behavior does not know by itself which player the object belongs to: you have to call Link to player.
- For your character: Link to player with
NyraServer.MyPlayerId. - For every other player you need another instance of the same object, linked to that player's identifier. The behavior moves it smoothly and tells you its facing, movement and running.
- You have to link again for the players who were already there (with For each other player inside On welcome), for the ones who arrive (On player joined) and after a level change: it does not link itself again.
The behavior does not animate and does not rotate the Sprite. You choose the animations with the triggers: On started moving for walking, On stopped moving for standing and On facing changed to choose the animation based on Facing or FacingName.
With Default controls on you use the arrow keys and Shift. The Space key does nothing, because in this mode you do not jump.
An example
You need a Sprite Avatar with the Nyra 8 Direction behavior. These are the events:
- NyraServer: On welcome → create
AvataratNyraServer.MyStartX,NyraServer.MyStartYand use Link to player withNyraServer.MyPlayerId.- Sub-event NyraServer: For each other player → create
AvataratNyraServer.PlayerX,NyraServer.PlayerYand use Link to player withNyraServer.PlayerId.
- Sub-event NyraServer: For each other player → create
- NyraServer: On player joined → create
Avatarand use Link to player withNyraServer.PlayerId. - NyraServer: On player left → for each
AvatarwithAvatar.Nyra8Direction.PlayerIdequal toNyraServer.PlayerId→ Destroy. - Avatar: On started moving → walking animation. Avatar: On stopped moving → standing animation.
- Avatar: On facing changed → animation according to
Avatar.Nyra8Direction.FacingName.
Note
The link with the server (Connect, Login) is done as explained in Connecting the game to the server.
Note
When the server disagrees with the predicted position (for example at a corner of a wall), the behavior corrects it: below 5 pixels it does not move, beyond that it lines up gently, from 100 pixels up it jumps. It is not an error. The other players stop with about a quarter of a second of delay, which depends on your network (measured with 100 ms each way).
Errors
Every error fires On error and writes a red line in the browser console (F12), in the form Nyra8Direction code: message, with the name of the add-on between square brackets. It does not stop the game. The messages are in English.
| CODE | WHAT IT MEANS | WHAT TO DO |
|---|---|---|
no-nyraserver | The NyraServer object is not in the project, or it is a version without the channel for the behaviors (earlier than 0.5.0.0). | Add NyraServer to the project. |
nyraserver-version | The NyraServer interface version is not the one the behavior knows (1). | Use NyraServer 0.5.0.0 or later. |
wrong-movement-mode | The server is not in 8 directions mode. | Choose 8 directions on the Configuration page of the panel. |
invalid-argument | A parameter is not valid: for example an empty Player id, or Set server target on a linked instance or with values that are not numbers. | Fix the parameter. |
not-linked | Unlink on an instance that was not linked. | Check with Is linked first. |
no-keyboard | Default controls is on but the behavior finds no source to read the keyboard from. | Turn Default controls off and use Simulate control. |
The NyraServer codes can arrive too (for example not-logged-in or not-connected) if the keys cannot be forwarded: you see them only once, not at every frame.
Note
If a player is not known yet, the link waits and an orange warning unknown-player appears in the console: it is not an error and On error does not fire. If the sign-in ends without the player appearing, the link is discarded.
Note
If wrong-movement-mode does not appear, check that you have done Login: the error only arrives when there is a sign-in and a link. If the character does not move and there is no error, look at the console: at startup the line Nyra8Direction default controls: keyboard events from … should appear, with the name of the add-on between square brackets.
Compatibility
The behavior needs NyraServer 0.5.0.0 or later. It works with the server in 8 directions mode. With an older server that does not announce when players stop, it works, but the other players stop about a seventh of a second later.