Hi, i cant find this information:
in Oxide, C# plugin, is it possible to pass input parameters to timers? If so, how can i pass parameters?
I want multiple independent timers and make sure they will execute the same function but using different input variables.
I really cant figure it out. I can't find any documentation for Oxide.Core.Libraries.Timer.TimerInstance
Code:private void DelayedUiCloseDo(Something s){ // here i need input variables s.testUI.Destroy(s.crafter); } private void DelayedUiClose(UIObject testUI, BasePlayer crafter) { // can i send somehow input variables from here to timer? var timerInstance = new Oxide.Core.Libraries.Timer.TimerInstance(1000, 1000, DelayedUiCloseDo, this); }private void OnItemCraft(ItemCraftTask task, BasePlayer crafter) { UIObject testUI= new UIObject(); UIColor color = new UIColor(0.5, 0.5, 0.5, 0.5); testUI.AddPanel("myUIpanel", 0, 0, 0.1, 0.1, color, false, "HUD/Overlay"); testUI.Draw(crafter); // input variables came from here originally DelayedUiClose( testUI, crafter); }
Solved Passing variables in a timer?
Discussion in 'Rust Development' started by raholl, Apr 21, 2016.
-
Wulf Community Admin
Everything you should need to know about timers is shown in the Docs link at the top of the page.
-
Thx a lot, google gives 10 results.
And search in Docs link at the top of the page gives me:
- No Results Found for "timer"
with no info about passing variables...
This thread is one of 10 google links to this topic.
Could you please respond here clearly, is there any way to pass input variables to function called by timer(s)?
If there is no way to pass variables just say no pls. If there is way to do it, simple example would be greatly appreciated.
Thx for your time Wulf -
Wulf Community Admin
-
Yes it is the same what i linked, but how to do it, pls.
Is this correct?
Code:str="Hello World!";timer.Once(3f, (str) => { Puts(str); });
But how do i make it function, how to take that part of code out to not be inline?
This is all-WRONG syntax of what i would like to do:
Code:sayText = function(str) { Puts(str); }str="Hello World!";timer.Once(3f, sayText(str) );
-
Wulf Community Admin
Code:timer.Once(3f, () => sayText(str));
-
Ye but that i was already trying like this:
Code:private void DelayedUiCloseDo(UIObject testUI, BasePlayer crafter){ testUI.Destroy(crafter); } private void DelayedUiClose(UIObject testUI, BasePlayer crafter) { var timerInstance = new Oxide.Core.Libraries.Timer.TimerInstance(1000, 1000, DelayedUiCloseDo(testUI, crafter), this); }
[Oxide] 07:46 [Error] myTest plugin failed to compile!
[Oxide] 07:46 [Error] myTest.cs(241,81): error CS1503: Argument `#3' cannot convert `void' expression to type `System.Action'
[DOUBLEPOST=1461217852][/DOUBLEPOST]So this compiles:
Code:private void DelayedUiCloseDo(UIObject testUI, BasePlayer crafter){ testUI.Destroy(crafter); } private void DelayedUiClose(UIObject testUI, BasePlayer crafter) { var timerInstance = new Oxide.Core.Libraries.Timer.TimerInstance(1000, 1000, () => DelayedUiCloseDo(testUI, crafter), this); }
instead of
DelayedUiCloseDo(testUI, crafter)
[DOUBLEPOST=1461218821][/DOUBLEPOST]But it actually does not work:
Code:private void DelayedUiCloseDo(UIObject testUI, BasePlayer crafter){ Log("DelayedUiCloseDo run..."); testUI.Destroy(crafter); } private void DelayedUiClose(UIObject testUI, BasePlayer crafter) { Log("DelayedUiClose run..."); var timerInstance = new Oxide.Core.Libraries.Timer.TimerInstance(1000, 1000, () => DelayedUiCloseDo(testUI, crafter), this); }
[DOUBLEPOST=1461219746][/DOUBLEPOST]Finally, this is working fine:
Code:private void OnItemCraft(ItemCraftTask task, BasePlayer crafter) { Puts("OnItemCraft hook"); UIObject testUI= new UIObject(); UIColor color = new UIColor(0.5, 0.5, 0.5, 0.5); testUI.AddPanel("myUIpanel", 0, 0, 0.1, 0.1, color, false, "HUD/Overlay"); testUI.Draw(crafter); timer.Once(5, () => { testUI.Destroy(crafter); }); }
-
Wulf Community Admin
-
-
Wulf Community Admin
-
Code:private void OnItemCraft(ItemCraftTask task, BasePlayer crafter) { Puts("OnItemCraft hook"); UIObject testUI= new UIObject(); UIColor color = new UIColor(0.5, 0.5, 0.5, 0.5); testUI.AddPanel("myUIpanel", 0, 0, 0.1, 0.1, color, false, "HUD/Overlay"); testUI.Draw(crafter); timer.Once(5, () => { testUI.Destroy(crafter); }); }
-
Wulf Community Admin
-
This has nothing to do with oxide.
Look up what first class functions, callbacks and closures are. -
-
-
thx Wulf again -
I named the concepts you need to understand to able to properly do what you wanted to do.
Just copying solutions and not understanding the language features that lead to the result will eventually result in you hitting another barrier the next time you have a slightly different but similar problem.