1. How do I get a variable from another plugin? I tried to make a function and return the variable in the other plugin andbut I getCode:bool isAFK = (bool)PlayTimeTracker.CallHook("returnisAFK", null);
Code:(NullReferenceException: Object reference not set to an instance of an object) [Oxide] 23:04 [Debug] at Oxide.Plugins.Interest.checkAFK (.BasePlayer player, System.String command, System.String[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.Interest.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hookname, System.Object[] args) [0x00000] in <filename unknown>:0
2. how do I get a timer that is called x second as long a player is still online? I was thinking do doing a while statement to test if the player is still in, and if he is, call timer.Once hook, but I can already see an error with that, and surely there is a better way.
Getting variables from another plugin?
Discussion in 'Rust Development' started by w4ssup, Mar 13, 2016.
-
Wulf Community Admin
You'd only be able to get something from another plugin if they have it in an accessible function/hook. Simply using the plugin name is not how you grab it, you'd need to either use a PluginReference for C# or plugins.Find to get the plugin instance first. There is some info on this in the Docs, and a few existing threads.
-
I did reference it.
can you give me an example code?Last edited by a moderator: Mar 13, 2016 -
Call a function from the plugin using the parameters it needs. This is just a example as I haven't looked at the plugin
Code:object isAFK = PlayTimeTracker?.Call("functionName", new object[]{param1, param2}); if (isAFK is bool) if ((bool) isAFK) //do stuff -
what did you put a question mark? and dont you mean 'as' instead of is?
and I'm looking for a way to call the class variable directly if that's possibleLast edited by a moderator: Mar 14, 2016 -
Wulf Community Admin
See Null-conditional Operators (C# and Visual Basic). Bascially a more modern way of doing:
Code:if (blah == null) return;
-
What Wulf said for the question mark and no I meant 'is'
That example was to call a function from another plugin which is what I thought you were trying to do based on what you originally posted.Code:object isAFK = PlayTimeTracker?.Call("functionName", new object[]{param1, param2}); if (isAFK is bool) // Check to make sure what has been returned is actually bool if ((bool) isAFK) // Check to see if the bool is true //do stuff
After looking at PlayTimeTracker there is no indication anywhere of a check to see whether a player is actually AFK. The AFK time is calculated in a function by checking the players position from a previously stored position, then it checks the time difference between then and now and the time is added to the time stamp in data.
I am unsure why you want to use another plugin to get this data anyway, you could easily create a function inside your own plugin to check whether your player is AFK. Here is something I quickly put together based roughly the same way as PlayTimeTracker does it, its pretty basic and untested but its a good start that you can improve on
Code:Dictionary<ulong, Vector3> AFKData = new Dictionary<ulong, Vector3>(); List<ulong> AFKPlayers = new List<ulong>(); void OnServerInitialized() { AFKCheck(); } void AFKCheck() { foreach (BasePlayer player in BasePlayer.activePlayerList) { if (AFKData.ContainsKey(player.userID)) // If afkdata already contains the player check their position { if (player.transform.position == AFKData[player.userID]) // If the players current position is the same as their previous position AFKPlayers.Add(player.userID); // Add them to afk list else { if (AFKPlayers.Contains(player.userID)) AFKPlayers.Remove(player.userID); // The players postion is not the same so check the afk players list and remove them if they are in there AFKData[player.userID] = player.transform.position; // Update the data with the new position } } else AFKData.Add(player.userID, player.transform.position); // No data currently for this player so add them in } timer.Once(300, () => AFKCheck()); // Repeat this function in 5 mins (continuous loop) } bool isAFK(BasePlayer player) { if (AFKPlayers.Contains(player.userID)) // Check if the player is in the afk list { if (AFKData.ContainsKey(player.userID)) // Check if the player is in data (they should already be here if they are in the afk list) if (AFKData[player.userID] == player.transform.position) // Check the position one last time to make sure they havent moved since the last check return true; } return false; }Last edited by a moderator: Mar 14, 2016 -
It didnt have that function (I added it myself), and I didnt make one because I was unsure of how to do a repeat effectively (which was my second question). Thanks for the sample script, Ill check it out
