Facepunch / sbox-issues

175 stars 12 forks source link

Hotload breaks games that use Network #4086

Closed PolSpock closed 9 months ago

PolSpock commented 11 months ago

Describe the bug

Hi,

Since the resolution of https://github.com/sboxgame/issues/issues/4066, the hotload for games that used [Net] var was fixed. However, as @MrBrax notices, hotload recently restarting creates errors.

Now it's generally random but I've been able to recreate a scenario for having the error frequently. See below.

To Reproduce

I've been able to bring you an easy way to reproduce : 1) Download & mount sbox-gunfight https://github.com/Facepunch/sbox-gunfight/ 2) Start "FFA - Small Town" image 3) Hotload the game by editing & saving a GameResource extended class. Like sbox-gunfight-main\code\WeaponSystem\WeaponDefinition.cs, just add a function to the class :

public void Hello()
{
    Log.Info( "I will break the game" );
}

4) The game starts to be crazy: you cannot play anymore with this spamming console error :

Could not proxy var 196671 as it does not exist in the network table
   at Sandbox.Internal.VarClass`1.Read(NetRead reader)
   at Sandbox.Internal.Var.Read()
   at Sandbox.NetworkTable.ReadAll(PredictionDataStore prediction)
   at Sandbox.Entity.UpdateFromNetwork()
   at Sandbox.Entity.PredictionStore(Int32 slot, Int32 command_num, Boolean network, Boolean variables)
   at Managed.SourceClient.Exports.Sandbox_Entity_PredictionStore(UInt32 self, Int32 slot, Int32 command, Int32 network, Int32 notnetworked)
   at NativeEngine.EngineGlobal.SourceEngineFrame(CMaterialSystem2AppSystemDict appDict, Double currentTime, Double previousTime)
   at Sandbox.EngineLoop.RunFrame(CMaterialSystem2AppSystemDict appDict, Boolean& wantsQuit)
   at Sandbox.SourceEngineApp.RunFrame() in C:\build\_work\sbox\sbox\engine\Launcher\SourceEngineApp.cs:line 46
   at Sandbox.SourceEngineApp.RunLoop() in C:\build\_work\sbox\sbox\engine\Launcher\SourceEngineApp.cs:line 37
   at Sandbox.Program.LaunchGame() in C:\build\_work\sbox\sbox\engine\Launcher\Launcher.cs:line 108
   at Sandbox.Program.Main() in C:\build\_work\sbox\sbox\engine\Launcher\Launcher.cs:line 58

And the HUD of GameMenu will be broken image

The sandbox game doesn't have this issue, so i conclude that network replication on hotload is currently broken

Expected behavior

Hotload must not breaking game

Media/Files

No response

Additional context

No response

MrBrax commented 11 months ago
1697138631591

Now it's even worse, completely breaking input and spamming the server console

PolSpock commented 11 months ago

image Something has changed since this update, is more stable for me

MrBrax commented 11 months ago

Same. Network vars seem to be fixed but menu still breaks.

Xartrick commented 10 months ago

Same. Network vars seem to be fixed but menu still breaks.

Menu breaking on hotload is something I experienced. Here is how I fixed it.

  1. OnAfterTreeRender(true) is not called when hotloading, something to keep in mind ;
  2. @ref might be null on reload, you must manually check them ;
  3. Check if your input variables are not null by using "guards".

Minimal example with a random component that expects a Player input :

@using Sandbox;
@using Sandbox.UI;

// guard
@if (Player == null) {
    return;
}

<root>
    <h1>@(Player.Name)</h1>
    <div @ref=ProgressBar></div>
</root>

@code
{
    // inputs
    public Player Player { get; set; }

    // references
    private Panel ProgressBar { get; set; }

    // properties
    private bool IsInitialized => ProgressBar != null;

    public override void Tick()
    {
        if (!IsInitialized) {
            return;
        }

        var progress = 0f; // calculate some progress
        ProgressBar.Style.Width = Length.Percent(progress);
    }
}