fix: Implement lazy initialization for Constants to prevent null reference errors
This commit is contained in:
15
Constants.cs
15
Constants.cs
@@ -15,19 +15,20 @@ namespace KCM
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Constants
|
public static class Constants
|
||||||
{
|
{
|
||||||
public static readonly MainMenuMode MainMenuMode = GameState.inst.mainMenuMode;
|
// Use lazy initialization to avoid null reference when GameState isn't ready yet
|
||||||
public static readonly PlayingMode PlayingMode = GameState.inst.playingMode;
|
public static MainMenuMode MainMenuMode => GameState.inst?.mainMenuMode;
|
||||||
public static readonly World World = GameState.inst.world;
|
public static PlayingMode PlayingMode => GameState.inst?.playingMode;
|
||||||
|
public static World World => GameState.inst?.world;
|
||||||
|
|
||||||
#region "UI"
|
#region "UI"
|
||||||
public static readonly Transform MainMenuUI_T = MainMenuMode.mainMenuUI.transform;
|
public static Transform MainMenuUI_T => MainMenuMode?.mainMenuUI?.transform;
|
||||||
public static readonly GameObject MainMenuUI_O = MainMenuMode.mainMenuUI;
|
public static GameObject MainMenuUI_O => MainMenuMode?.mainMenuUI;
|
||||||
|
|
||||||
/* public static readonly Transform TopLevelUI_T = MainMenuUI_T.parent;
|
/* public static readonly Transform TopLevelUI_T = MainMenuUI_T.parent;
|
||||||
public static readonly GameObject TopLevelUI_O = MainMenuUI_T.parent.gameObject;*/
|
public static readonly GameObject TopLevelUI_O = MainMenuUI_T.parent.gameObject;*/
|
||||||
|
|
||||||
public static readonly Transform ChooseModeUI_T = MainMenuMode.chooseModeUI.transform;
|
public static Transform ChooseModeUI_T => MainMenuMode?.chooseModeUI?.transform;
|
||||||
public static readonly GameObject ChooseModeUI_O = MainMenuMode.chooseModeUI;
|
public static GameObject ChooseModeUI_O => MainMenuMode?.chooseModeUI;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
Main.cs
23
Main.cs
@@ -189,7 +189,21 @@ namespace KCM
|
|||||||
|
|
||||||
Main.helper.Log(JsonConvert.SerializeObject(World.inst.mapSizeDefs, Formatting.Indented));
|
Main.helper.Log(JsonConvert.SerializeObject(World.inst.mapSizeDefs, Formatting.Indented));
|
||||||
|
|
||||||
KaC_Button serverBrowser = new KaC_Button(Constants.MainMenuUI_T.Find("TopLevelUICanvas/TopLevel/Body/ButtonContainer/New").parent)
|
// Check if MainMenuUI_T is available
|
||||||
|
if (Constants.MainMenuUI_T == null)
|
||||||
|
{
|
||||||
|
Main.helper.Log("MainMenuUI_T is null, cannot create Multiplayer button");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var buttonContainer = Constants.MainMenuUI_T.Find("TopLevelUICanvas/TopLevel/Body/ButtonContainer/New");
|
||||||
|
if (buttonContainer == null)
|
||||||
|
{
|
||||||
|
Main.helper.Log("Button container not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
KaC_Button serverBrowser = new KaC_Button(buttonContainer.parent)
|
||||||
{
|
{
|
||||||
Name = "Multiplayer",
|
Name = "Multiplayer",
|
||||||
Text = "Multiplayer",
|
Text = "Multiplayer",
|
||||||
@@ -202,8 +216,11 @@ namespace KCM
|
|||||||
};
|
};
|
||||||
serverBrowser.Transform.SetSiblingIndex(2);
|
serverBrowser.Transform.SetSiblingIndex(2);
|
||||||
|
|
||||||
|
var kingdomShare = Constants.MainMenuUI_T.Find("TopLevelUICanvas/TopLevel/Body/ButtonContainer/Kingdom Share");
|
||||||
Destroy(Constants.MainMenuUI_T.Find("TopLevelUICanvas/TopLevel/Body/ButtonContainer/Kingdom Share").gameObject);
|
if (kingdomShare != null)
|
||||||
|
{
|
||||||
|
Destroy(kingdomShare.gameObject);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user