Closed cantorek closed 4 years ago
The initialization of a Bolt Entity is a frame-sensitive process, that is why we provide several callbacks to make sure it occurs at the right moment and you don't need to sync the flow yourself.
In your case, there is no way to properly know when the Start
method of your other class will execute, as this is controlled by Unity itself.
There are two ways you can have your desired behavior working:
Bolt.EntityBehaviour<IState>
with the right State and override the Attached()
callback. The Bolt Prefabs can have multiple of those.using System.Collections;
using Bolt.Samples.GettingStarted;
using UnityEngine;
public class BoltBugTets : Bolt.EntityBehaviour<IState>
{
void Start()
{
if (entity.IsOwner)
{
StartCoroutine(TrigerLog());
}
}
public override void Attached()
{
state.OnGameOver += Log;
}
public IEnumerator TrigerLog()
{
while (true)
{
state.GameOver();
yield return new WaitForSeconds(1);
}
}
public void Log()
{
BoltLog.Warn("Is owner or controller? {0}", entity.IsControllerOrOwner);
}
}
Bolt.GlobalEventListener
to include the callback on any Entity that was attached to the Bolt system:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[BoltGlobalBehaviour("GameScene")]
public class NetworkCallbacks : Bolt.GlobalEventListener
{
public override void EntityAttached(BoltEntity entity)
{
if (entity.StateIs<IState>())
{
var state = entity.GetState<IState>();
state.OnGameOver += () =>
{
BoltLog.Warn("Is owner or controller? {0}", entity.IsControllerOrOwner);
};
}
}
}
How to reproduce issue
GetComponent<BoltBugTest>().state.OnGameOver += Log;
Expected Behavior
Delegate function (Log) should fire on all proxies
Actual behavior
Delegate function (Log) is called only on the owner
Configuration
When using state.AddCallback in the same place in code on different property it works fine. I wasn't able to use state.AddCallback with triggers successfully.
In example below, delegate function - Log - is being fired only on the owner. When changing the subscription place to within Attached() it works fine. This is however unexpected as delegate can be set anywhere in the code - hence it's a delegate. Also, using State.AddCallback works just fine. Trigger state is being replicated, I can see frame counter update within inspector. According to my debug log output, BoltBugTest2.Start() is being run after BoltBugTest.Attached().