I’m trying to create a multiplayer game, but I want to use netcode instead of a third-party solution.
I’ve tried using the manual, but it seems it isn’t up to date?
I’ve downloaded the sample project but personally, it’s way to large and complicated.
I’d love to get my hands on a simple sample project, like just some players that can move.
If there is such a project, or a good tutorial I’d love to hear about it.
I’ve checked that everything is up to date, I used the latest editor and updated all the listed packages mentioned in the manual (Hybrid Renderer, Entities, Unity NetCode, Unity Transport + Burst + Jobs)
using System;
using UnityEngine;
using Unity.Burst;
using Unity.Entities;
using Unity.NetCode;
using Unity.Networking.Transport;
//update in default world
[UpdateInWorld(UpdateInWorld.TargetWorld.Default)]
public class Game : ComponentSystem
{
//singleton trigger connections once from control system
struct InitGameComponent : IComponentData
{
}
protected override void OnCreate()
{
RequireSingletonForUpdate<InitGameComponent>();
//run singleton once
EntityManager.CreateEntity(typeof(InitGameComponent));
}
protected override void OnUpdate()
{
//Destroy singleton, prevent it from running again
EntityManager.DestroyEntity(GetSingletonEntity<InitGameComponent>());
foreach (var world in World.AllWorlds)
{
var network = world.GetExistingSystem<NetworkStreamReceiveSystem>();
if (world.GetExistingSystem<ClientSimulationSystemGroup>() != null)
{
// Client worlds automaticlly connect to localhost
NetworkEndPoint ep = NetworkEndPoint.LoopbackIpv4;
ep.Port = 7979;
network.Connect(ep);
}
#if UNITY_EDITOR
else if (world.GetExistingSystem<ServerSimulationSystemGroup>() != null)
{
// Server worldd automatically listens for connections from any host
NetworkEndPoint ep = NetworkEndPoint.AnyIpv4;
ep.Port = 7979;
network.Listen(ep);
}
#endif
}
}
}
[BurstCompile]
public struct GoInGameRequest : IRpcCommand
{
public void Deserialize(DataStreamReader reader, ref DataStreamReader.Context ctx)
{
}
public void Serialize(DataStreamWriter writer)
{
}
[BurstCompile]
private static void InvokeExecute(ref RpcExecutor.Parameters parameters)
{
RpcExecutor.ExecuteCreateRequestComponent<GoInGameRequest>(ref parameters);
}
static PortableFunctionPointer<RpcExecutor.ExecuteDelegate> InvokeExecuteFunctionPointer = new PortableFunctionPointer<RpcExecutor.ExecuteDelegate>(InvokeExecute);
public PortableFunctionPointer<RpcExecutor.ExecuteDelegate> CompileExecute()
{
return InvokeExecuteFunctionPointer;
}
}
// The system that makes the RPC request component transfer
public class GoInGameRequestSystem : RpcCommandRequestSystem<GoInGameRequest>
{
}
I’ve seen this problem before, but I have no idea what to do.
Errors 2 and 3 are telling you exactly what the issue is. Implement the two methods and remove (if not in the interface) the others and you should be fine
You mainly need to update the code to the NetCode version you have.
For example, error 3 tells you that the reader is now passed by ref and that the context is not passed anymore. You can then assume that ReadInt does not take a context anymore either:
It does not know what you want to write (byte? int? …?). In general, the Serialize/Deserialize methods need to correspond internally to one another, since what one serializes, the other deserializes again on the other end. So if you write an int, you need to read an int, in the same order.
So here:
public void Serialize(ref DataStreamWriter writer)
{
writer.WriteInt(horizontal);
writer.WriteInt(vertical);
}
You’re missing a using Unity.Transforms;. I have to tell you that if you already have trouble with this error or how to handle it, you will most likely not get very far with the experimental package that is the current NetCode, since there are some hurdles hidden in there that require some knowledge on how to handle issues in code. Sorry to be so blunt
I figured, I’m not really far in programming. I’ve gotten so far as creating games and being able to write decent code with documentation. But this really is new territory for me, I’ve only been programming for 2 years so I really do have a lot to learn. I just thought if I tried my best I’d be able to get it working, but it’s a real struggle for without documentation.
However I really thank you for your help!
I want you to succeed!
But I also did not want to paint too rosy of a picture since you’ll be using an experimental package with changing code and (as you also found) not up to date documentation and very few examples, videos, etc. I just wanted to provide a bit of a warning, since figuring it all out on one’s own can be nerve-wracking. The next version will be similar, but will require code changes again, I assume, and the one version after that will contain even bigger changes, if I understood the team correctly.
Anyway, you’ll only get better by trying, so all the best on your path!
P.S: I can recommend an IDE such as Rider, which will automatically suggest fixes such as the one above with using.