Skip to content

IGameObjectNetworkEvents are executed too early (prior to Syncvar initializations) #1265

Description

@dimmerly

Branch

staging

Describe the bug

When I have a gameobject that spawns a prefab with a component which has logic inside a NetworkOwnerChanged that interacts with a syncvar, it throws Object reference not set to an instance of an object. when it shouldn't if the ordering was correct.

e.g.

public sealed class NastyEventListener : Component, IGameObjectNetworkEvents
{
	[Sync]
	public bool FlipFlop { get; set; }
	
	public void NetworkOwnerChanged( Connection newOwner, Connection previousOwner )
	{
		Log.Info( "Owner Change" );
		FlipFlop = !FlipFlop;
	}
}
public sealed class Spawner : Component
{
	
	[Property]
	public GameObject toSpawn { get; set; }

	protected override void OnStart()
	{
		var test = toSpawn.Clone();

		test.NetworkSpawn();
	}
}

Exception when setting NastyEventListener.FlipFlop - Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at Sandbox.Component.__sync_SetValue[T](WrappedPropertySet`1& p) in C:\build_work\sbox\sbox\engine\Sandbox.Engine\Scene\Components\Component.Network.cs:line 42

To Reproduce

Run project and observe

gameobjectneteventbug.zip

when running

Expected behavior

It should be after syncvars are initialized or atleast not throw an exception

Media/Files

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    Status
    To triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions