Hello,
I tried to edit the Balance class from InfoPanel for Rust | Oxide for it to display the player's RP amount from ServerRewards for Rust | Oxide
So here are the interesting parts of the Balance class:
The problem is that the "!ServerRewards" condition is passing, so the function returns 0.Code:[PluginReference] Plugin ServerRewards;public int GetBalance(string PlayerID) { var player = RustCore.FindPlayerByIdString(PlayerID); if (player == null || !ServerRewards) return 0; return (int)ServerRewards?.Call("CheckPoints", player.userID); }
If I remove the "!ServerRewards" condition, the Call is called and I get tons of errors.
I don't know what is wrong, any of you could help me please?
Solved External plugin call trouble
Discussion in 'Rust Development' started by Shytoos, Nov 3, 2016.
-
Wulf Community Admin
If !ServerRewards passes, that means ServerRewards is installed. What errors are you getting exactly? Is the above code from your plugin I assume? First thing I would say is that you don't need to find the player to get their ID, you already have it from the argument.
Code:[PluginReference] Plugin ServerRewards;public int GetBalance(string id) { if (!ServerRewards) return 0; return (int)ServerRewards.Call("CheckPoints", Convert.ToUInt64(id)); }
-
Here's the entire class, nothing has been edited outside of it:
Code:#region Balance private Balance Bala; private Timer BalanceUpdater; public class Balance { [PluginReference] Plugin ServerRewards; public int RefreshRate = 3; public Balance(int RefreshRate) { this.RefreshRate = RefreshRate; } public int GetBalance(string PlayerID) { var player = RustCore.FindPlayerByIdString(PlayerID); if (player == null || !ServerRewards) return 0; return (int)ServerRewards?.Call("CheckPoints", player.userID); } public void Refresh(StoredData storedData, Dictionary<string, Dictionary<string, IPanel>> panels) { if (!Settings.CheckPanelAvailability("Balance")) return; foreach (var panel in panels) { IPanel iPanel; if (!panel.Value.TryGetValue("BalanceText", out iPanel)) continue; var balance = GetBalance(panel.Key); var panelText = (IPanelText)iPanel; if (!balance.Equals(panelText.Content)) { panelText.Content = $"{balance}"; panelText.Refresh(); } } } } #endregion
Code:[Error] Failed to call hook 'OnServerInitialized' on plugin 'InfoPanel v0.9.5' (NullReferenceException: Object reference not set to an instance of an object)
-
Wulf Community Admin
-
I tried the code you gave to me, still returning 0 because of the line
Code:if (!ServerRewards) return 0;
Also, I'm not doing a new plugin, I'm editing the InfoPanel one. -
Wulf Community Admin
-
-
Wulf Community Admin
-
Code:[PluginReference] static Plugin ServerRewards;
-
Wulf Community Admin
-
Without the "static" I get:
Code:error CS0038: Cannot access a nonstatic member of outer type `Oxide.Plugins.InfoPanel' via nested type `Oxide.Plugins.InfoPanel.Balance'
-
You need the plugin reference in the main namespace. The create a static instance of your plugin. Then call ServerRewards via the instance
Code:[PluginReference] Plugin ServerRewards static InfoPanel instance; void OnServerInitialized() { instance = this; }// in your class instance.ServerRewards?.Call....
-
-
-
Static classes (even singletons!) are completly fine because the plugin class itself already acts as single namespace for all plugin code.
Don't just randomly parrot things. -
try to build architecture without Singleton, class is an object, which in this should only manage itself. -
Code:public double GetBalance(string id) { var player = RustCore.FindPlayerByIdString(id); if (player == null) return 0; return (double)instance.ServerRewards?.Call("CheckPoints", Convert.ToUInt64(id)); }
Code:Failed to call hook 'OnServerInitialized' on plugin 'InfoPanel v0.9.5' (InvalidCastException: Cannot cast from source type to destination type.)
Code:void OnServerInitialized() { instance = this; }
-
did you get this working im also attempting to do this.
-
Code:
public int GetBalance(string id) { var player = RustCore.FindPlayerByIdString(id); if (player == null) return 0; return (int)instance.ServerRewards?.Call("CheckPoints", Convert.ToUInt64(id)); }
-
[DOUBLEPOST=1484926029][/DOUBLEPOST]