Skip to content

Get started

Dmytro edited this page Aug 22, 2024 · 1 revision
  1. Install StrideNet NuGet package to your Stride project.

  2. In Stride Editor create empty entity on scene and attach Network->NetworkManager and NetworkSpawner components to it.

image

  1. Create your player entity on scene and attach Network->NetworkEntity component to it. Create prefab and save it.

image

Player script sample
namespace Sample
{
    public partial class Player : NetworkScript
    {
        public float Speed { get; init; }

        [DataMemberRange(0,1, 0.01, 0.1, 5)]
        public float Interpolation { get; init; } = 0.5f;

        [NetworkVariable(SendMode = MessageSendMode.Unreliable)]
        private Vector3 _position;

        public override void NetworkStart()
        {
            Log.Info($"NetworkId: {OwnerId}, IsLocalPlayer: {IsOwner}");
        }

        public override void Update()
        {
            if(IsClient)
                Entity.Transform.Position = Vector3.Lerp(Entity.Transform.Position, Position, Interpolation);

            if (!IsOwner)
                return;

            float horizontal = (Input.IsKeyDown(Keys.A) ? -1f : 0) + (Input.IsKeyDown(Keys.D) ? 1f : 0);
            float vertical = (Input.IsKeyDown(Keys.S) ? -1f : 0) + (Input.IsKeyDown(Keys.W) ? 1f : 0);

            Vector2 input = new(horizontal, vertical);
            UpdatePositionRpc(input);
        }

        [NetworkRpc]
        private void UpdatePosition(Vector2 input)
        {
            float deltaTime = (float)Game.UpdateTime.Elapsed.TotalSeconds;
            Entity.Transform.Position += Entity.Transform.WorldMatrix.Up * Speed * input.Y * deltaTime;
            Entity.Transform.Position += Entity.Transform.WorldMatrix.Right * Speed * input.X * deltaTime;
            Position = Entity.Transform.Position;
        }
    }
}
  1. Add prefab to spawnable prefabs list in NetworkSpawner component.

image

  1. Now create new script that will start NetworkManager and spawn players.
Code sample
namespace Sample
{
    public class GameStartup : SyncScript
    {
        public NetworkManager NetworkManager { get; init; }
        public NetworkSpawner Spawner { get; init; }
        public Prefab PlayerPrefab { get; init; }

        public override void Start()
        {
            NetworkManager.ClientConnectedToServer += NetworkManager_ClientConnectedToServer;
            NetworkManager.ServerStarted += NetworkManager_ServerStarted;
        }

        private void NetworkManager_ServerStarted(object sender, System.EventArgs e)
        {
            if(NetworkManager.IsHost)
                Spawner.SpawnEntities(PlayerPrefab, ownerId: 0);
        }

        private void NetworkManager_ClientConnectedToServer(object sender, ServerConnectedEventArgs e)
        {
            Spawner.SpawnEntity(PlayerPrefab, e.Client.Id);
        }

        public override void Update()
        {
            if (Input.IsKeyDown(Stride.Input.Keys.D1))
                NetworkManager.StartHost();

            if (Input.IsKeyDown(Stride.Input.Keys.D2))
                NetworkManager.StartClient();
        }
    }
}
  1. Attach this component to a entity on a scene and assign properties in editor.

image

  1. Build game and see result.

For this sample, to start server press 1, to start client press 2.

image

Clone this wiki locally