Nyra Studios
Manual under construction. Some parts are incomplete or may change.

Nyra 8 Direction

Nyra 0.5.4.28

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

TypeBehavior (you add it to a Sprite object)
Name in ConstructNyra 8 Direction (General category)
Version0.5.0.0
Construct versionr495 or newer
NeedsThe NyraServer object in the project (version 0.5.0.0 or later)

Adding it to an object

  1. Install the addon with the Addon manager (see Installing the addons).
  2. Select your Sprite. In the properties bar open the Behaviors tab and press Add / Edit.
  3. 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

PROPERTYWHAT IT DOES
Default controlsIf 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 directionsHow 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.
EnabledIf 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 smoothingHow 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

CONDITIONWHAT IT DOES
Is linkedTrue while the instance is linked to a player.
Is local playerTrue if the linked player is yours (movement is predicted). False for another player.
On linkedFires when the link is complete.
On unlinkedFires when the link ends: with Unlink, for a disconnection, for a level change or because the player left. The instance has stopped.
On errorFires on every error. See ErrorCode and ErrorMessage.

Input

CONDITIONWHAT IT DOES
Is enabledTrue if the behavior is on.

State

CONDITIONWHAT IT DOES
Is movingTrue while the character is moving.
Is runningTrue while the character is moving at running speed.
Is facingTrue 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 movingFires when the character starts moving.
On stopped movingFires when the character stops, even if the link ends while it was moving.
On facing changedFires when the facing direction changes, already reduced by Facing directions.

Actions

ACTIONWHAT IT DOES
Link to playerLinks 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.
UnlinkBreaks 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

ACTIONWHAT IT DOES
Simulate controlPresses 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 enabledTurns 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.

EXPRESSIONWHAT IT RETURNS
PlayerIdThe identifier of the linked player (the last one, even after Unlink).
ErrorCodeThe code of the last error.
ErrorMessageThe message of the last error.
FacingThe facing angle in degrees (0 right, 90 down), already reduced by Facing directions.
FacingNameThe name of the facing: right, down-right, down, down-left, left, up-left, up, up-right.
VelocityXThe horizontal speed, in pixels per second.
VelocityYThe 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:

  1. NyraServer: On welcome → create Avatar at NyraServer.MyStartX, NyraServer.MyStartY and use Link to player with NyraServer.MyPlayerId.
    • Sub-event NyraServer: For each other player → create Avatar at NyraServer.PlayerX, NyraServer.PlayerY and use Link to player with NyraServer.PlayerId.
  2. NyraServer: On player joined → create Avatar and use Link to player with NyraServer.PlayerId.
  3. NyraServer: On player left → for each Avatar with Avatar.Nyra8Direction.PlayerId equal to NyraServer.PlayerId → Destroy.
  4. Avatar: On started moving → walking animation. Avatar: On stopped moving → standing animation.
  5. 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.

CODEWHAT IT MEANSWHAT TO DO
no-nyraserverThe 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-versionThe NyraServer interface version is not the one the behavior knows (1).Use NyraServer 0.5.0.0 or later.
wrong-movement-modeThe server is not in 8 directions mode.Choose 8 directions on the Configuration page of the panel.
invalid-argumentA 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-linkedUnlink on an instance that was not linked.Check with Is linked first.
no-keyboardDefault 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.