Coder Social home page Coder Social logo

Comments (18)

SoftwareGuy avatar SoftwareGuy commented on May 14, 2024 2

@Abdel-Fattah-Radwan Please don't necrobump old tickets, this ticket was from 2018/2019.

Please open a new ticket and reference this one. Thanks.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024 1

This is what it should look like in the end.
image

Might need a helper function so we don't have to do all of that with the weaver.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

Some more findings: PreStartClient() is called in NetworkIdentity.OnStartClient().

So this is not reliable enough. if Player is spawned, then 10s later Pet is spawned (=both in separate spawn packets) then OnStartClient of Player won't know Pet. No way around it except wrapping [SyncVar] NetIdentity with netIds.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

just for my notes. the plan:

  1. NetworkVAR.get: return clientscene.findlocalobject
  2. remove PreStartClient? not needed anymore then?
  3. replace 'VAR' reads with 'NetworkVAR.get'

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

More findings: OnDeserialize reads the netId if initialstate, and ReadGameObject() otherwise. Not 100% sure about the reason yet, but looks like we can remove Read/WriteGameObject() from NetworkReader/Writer if this is fixed too.

image

from mirror.

paulpach avatar paulpach commented on May 14, 2024

Be careful with removing Read/Write game object. I believe this is used when you pass a gameobject in a Cmd or Rpc.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

Some more research.. the above method only works if the GameObject was spawned already. This will result in NullReferenceExceptions if we try to access it before it was spawned, for example:
image

So this might not be the best solution yet after all.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

Will test this solution in uMMORPG for a while now. This should work:
image
There is one _ownerNetId which is still persistent, and the result is now cached in _owner which also makes it work before Spawn is called, because it's assigned in the setter too.

from mirror.

paulpach avatar paulpach commented on May 14, 2024

Cache is not good. What happens if you reassign the object? the client would keep using the old object.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

Cache is not good. What happens if you reassign the object? the client would keep using the old object.

Good point.
It seems like 'return NetworkIdentity.all[netId]' would be the easiest solution here. Right now NetworkIdentities are only stored in a dict after OnStartServer or ApplySpawnPayload. Not sure why yet.

from mirror.

paulpach avatar paulpach commented on May 14, 2024

@vis2k any progress on this? I believe this affects me too

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

still working on some edge cases, like assigning player.pet to summoned pet before networkserver.spawned was called, in which case netid is still 0.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

considering to simply show a warning in that case.
[SyncVar] GameObject/NetworkIdentity is really just a wrapper around netid then, so using null values or values with netid=0 will set it to 0, that's it

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

this works in uMMORPG so far:
image

image

porting it to Weaver now.

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

Test setup in case it's needed again later:

using UnityEngine;
using Mirror;

public class Test : NetworkBehaviour
{
    // the syncvar
    [SyncVar] GameObject test;

    // a function that uses it
    void Update()
    {
        Debug.Log(test.name);

        // read and write once
        GameObject read = test;
        test = gameObject;
    }
}

Currently generated:

using Mirror;
using System.Runtime.InteropServices;
using UnityEngine;

public class Test : NetworkBehaviour
{
  [SyncVar]
  private GameObject test;
  private uint ___testNetId;

  private void Update()
  {
    Debug.Log((object) ((Object) this.test).get_name());
    GameObject test = this.test;
    this.Networktest = ((Component) this).get_gameObject();
  }

  private void MirrorProcessed()
  {
  }

  public GameObject Networktest
  {
    get
    {
      return this.test;
    }
    [param: In] set
    {
      this.SetSyncVarGameObject(value, ref this.test, 1UL, ref this.___testNetId);
    }
  }

  public override bool OnSerialize(NetworkWriter writer, bool forceAll)
  {
    bool flag = base.OnSerialize(writer, forceAll);
    if (forceAll)
    {
      writer.Write(this.test);
      return true;
    }
    writer.WritePackedUInt64(this.syncVarDirtyBits);
    if (((long) this.syncVarDirtyBits & 1L) != 0L)
    {
      writer.Write(this.test);
      flag = true;
    }
    return flag;
  }

  public override void OnDeserialize(NetworkReader reader, bool initialState)
  {
    base.OnDeserialize(reader, initialState);
    if (initialState)
    {
      this.___testNetId = reader.ReadPackedUInt32();
    }
    else
    {
      if (((long) reader.ReadPackedUInt64() & 1L) == 0L)
        return;
      this.test = reader.ReadGameObject();
    }
  }

  public override void PreStartClient()
  {
    if (this.___testNetId == 0U)
      return;
    this.Networktest = ClientScene.FindLocalObject(this.___testNetId);
  }
}

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

progress so far:
+removed prestartclient
+Networktest.get returns GameObject based on netId
+NetworkTest.set SetSyncVarGameObject assigns netId internally
+onserialize/ondeserialize use netIds for GameObjects
-all read access to 'test' still need to be replaced with Networktest.get

using Mirror;
using System.Runtime.InteropServices;
using UnityEngine;

public class Test : NetworkBehaviour
{
	[SyncVar]
	private GameObject test;

	private uint ___testNetId;

	public GameObject Networktest
	{
		get
		{
			return NetworkBehaviour.GetSyncVarGameObject(___testNetId);
		}
		[param: In]
		set
		{
			this.SetSyncVarGameObject(value, ref test, 1uL, ref ___testNetId);
		}
	}

	public Test()
		: this()
	{
	}

	private void Update()
	{
		Debug.Log((object)test.get_name());
		GameObject val = test;
		Networktest = this.get_gameObject();
	}

	private void MirrorProcessed()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = this.OnSerialize(writer, forceAll);
		if (forceAll)
		{
			writer.WritePackedUInt32(___testNetId);
			return true;
		}
		writer.WritePackedUInt64(this.get_syncVarDirtyBits());
		if ((this.get_syncVarDirtyBits() & 1L) != 0L)
		{
			writer.WritePackedUInt32(___testNetId);
			result = true;
		}
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		this.OnDeserialize(reader, initialState);
		if (initialState)
		{
			___testNetId = reader.ReadPackedUInt32();
			return;
		}
		long num = (long)reader.ReadPackedUInt64();
		if ((num & 1L) != 0L)
		{
			___testNetId = reader.ReadPackedUInt32();
		}
	}
}

from mirror.

miwarnec avatar miwarnec commented on May 14, 2024

🎉 This issue has been resolved in version 1.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

from mirror.

abdelfattahradwan avatar abdelfattahradwan commented on May 14, 2024

@vis2k with all due respect, are you sure this has been fixed?! because this still happens even in the latest version as of writing this message.

from mirror.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.