System Key Timers

Status
Not open for further replies.

Jesus4Lyf

Good Idea™
Reaction score
397
Key Timers 2​
Version 1.0​

Requirements:
- Jass NewGen

JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ KT ~~ Key Timers 2 ~~ By Jesus4Lyf ~~ Version 1.0 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//  What is Key Timers?
//		 - Key Timers attaches structs to timers, or more to the point timed
//		   effects.
//		 - Key Timers only uses one timer per period to keep things efficient.
//		 - You can specify different periods. If you don't want to specify more
//		   than one period, I recommend using Timer Ticker instead.
//
//	=Pros=
//		 - Easy to use.
//		 - Fastest attaching system for loading the struct each time the
//		   timer expires (faster than Timer Ticker).
//
//	=Cons=
//		 - Should not be used with struct types that have more than 300 or so in
//		   existance at once. If lag happens when calling Add, this is why.
//		 - Periods should only be taken to 0.005 of a second, not 0.007 for example.
//		 - Limit of 20 different periods.
//		 - Limit of 400 instances of each period.
//
//	Functions:
//		 - KT_Add(userFunc, period, struct)
//		 - GetTriggerExecCount(GetTriggeringTrigger()) returns the struct
//
//		 - func is to be a user function that takes nothing and return boolean.
//		   It will be executed by the system every period until it returns true.
//
//		 - GetTriggerExecCount(GetTriggeringTrigger()) is to be used inside func,
//		   it will return struct passed to Add function. If you don't like this
//		   syntax, use KT_GetData() instead, but it isn't quite as efficient.
//
//  Details:
//		 - All user functions that should be run with the same period are stored
//		   in a timer that will call all those functions each period.
//
//		 - While func returns false the timer will continue to call it each period
//		   Once func returns true it will be detached from system.
//
//		 - When there is nothing attached, the timer for that period will pause itself.
//
//  Thanks:
//		 - Cohadar: He actually told me to make Key Timers without a textmacro
//		   interface. So this is it. Thanks to him for helping me with the original
//		   Key Timers system too. Also, I'd like to thank him for his work on
//		   Timer Ticker (TT) which demonstrated how to use triggers/conditions
//		   in this sort of system, and it would have been much harder to create
//		   Key Timers 2 without his excellent innovation.
//
//  How to import:
//		 - Create a trigger named KT.
//		 - Convert it to custom text and replace the whole trigger text with this.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library KT
	globals
		private trigger array trig
		private integer array max
		
		private integer array timerid
		private timer array KeyTimer
		private integer KeyTimerMax=0
		
		private trigger Carry
	endglobals
	
	private function KeyTimerLoop takes nothing returns nothing
		local integer id=timerid[R2I(TimerGetTimeout(GetExpiredTimer())*200)]
		local integer min=(id-1)*400
		local integer i=max[id]
		
		loop
			exitwhen i==min
			if TriggerEvaluate(trig<i>) then
				set Carry=trig<i>
				call TriggerClearConditions(Carry)
				set trig<i>=trig[max[id]]
				set trig[max[id]]=Carry
				set max[id]=max[id]-1
			endif
			set i=i-1
		endloop
		if max[id]==min then
			call PauseTimer(KeyTimer[id])
		endif
	endfunction
	
	public function Add takes code func, integer attachment, real period returns nothing
		local integer i=R2I(period*200)
		local trigger t
		
		if timerid<i>&lt;1 then
			set KeyTimerMax=KeyTimerMax+1
			set timerid<i>=KeyTimerMax
			set KeyTimer[KeyTimerMax]=CreateTimer()
			set max[KeyTimerMax]=(KeyTimerMax-1)*400
		endif
		set i=timerid<i>
		
		set max<i>=max<i>+1
		if trig[max<i>]==null then
			set trig[max<i>]=CreateTrigger()
		endif
		set t=trig[max<i>]
		if GetTriggerExecCount(t)&gt;attachment then
			call ResetTrigger(t)
		else
			set attachment=attachment-GetTriggerExecCount(t)
		endif
		call TriggerAddCondition(t,Condition(func))
		loop
			exitwhen attachment==0
			call TriggerExecute(t)
			set attachment=attachment-1
		endloop
		set t=null
		
		if max<i>==(i-1)*400+1 then
			call TimerStart(KeyTimer<i>,period,true,function KeyTimerLoop)
		endif
	endfunction
	
	// This function is just here for those who don&#039;t feel confident using
	// GetTriggerExecCount(GetTriggeringTrigger()) directly.
	public function GetData takes nothing returns integer
		return GetTriggerExecCount(GetTriggeringTrigger())
	endfunction
endlibrary

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	End of Key Timers 2
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// The below is for simple implementation.
function InitTrig_KT takes nothing returns nothing
endfunction</i></i></i></i></i></i></i></i></i></i></i></i></i>


About a week ago Cohadar was telling me to make Key Timers not use a textmacro interface. Done.

This turns out to be similar to Timer Ticker, but you can use multiple periods, and it uses a more efficient load struct method.

This comes with a few other minor limits, but try it and see. If you only need one fixed period you may as well use Timer Ticker instead... It should be a tiny bit faster. However, the load struct method in this is actually faster than Ticker Timer. I tested it. It's about 3x or 4x as fast according to my test.

Below is a simple example map.
 

Attachments

  • KeyTimers2.w3x
    18 KB · Views: 223

Cohadar

master of fugue
Reaction score
209
More than 80% of code here is taken from TT,
so I suggest you replace the credit section by saying that you created this by modifying TT (+ the link to the original system) or I blame you for forgery.

// - Key Timers only uses one timer per period to keep things efficient.
...
private timer array KeyTimer // One timer?

faster than Timer Ticker
??? Do you even believe your own words?

Same advice as for Key Triggers:
Give up on using TriggerExecCount as attaching method it simply does not work ffs.

More advice:
My systems may look simple in design but they are by no means simple in construction.
It takes time and careful testing to make something good.
Patience is a virtue some people say...
 

Jesus4Lyf

Good Idea™
Reaction score
397
Cohadar,

You told me to do it... o_O

Here I have combined Key Timers and Key Triggers to produce this. I daresay 80% of your code was taken from Key Timers in the first place, if you want to look at it that way.

// - Key Timers only uses one timer per period to keep things efficient.
...
private timer array KeyTimer // One timer?

One timer per period. As stated.

I didn't rip this from TT, but I really liked your documentation method. As I said, I merged what I made in Key Timers and Key Triggers to produce this. I also hand coded a system similar to structs, but in a manner that is more efficient. I used it to make this multi-period and still efficient.

Yes, I read TT, but I coded this from scratch, based on my systems. Notice that I had to attach an integer ID to my timers, and I did so by attaching via the period. Don't you think that's kind of clever and needless to say completely different to your system?

faster than Timer Ticker

You quote me out of context? It says the load struct system is faster. And it is. I tested it and it's about 4x faster. I acknowledge TT as the faster system in the post, but it can't deal with multi-period issues.

Anyway, I've just followed through by using some more of my work to make Key Timers more like TT in syntax... Like you said I should do. PM me if you want to talk further if you feel something has been stolen and we can sort it out. I don't want this thread to go off topic.

I actually put this together very carefully. This is the system I had in mind when I said it would be timeconsuming to make, and not quite as efficient.

Edit: Oh, I admit I took the clear conditions idea. :) That was clever.
 

Cohadar

master of fugue
Reaction score
209
Cohadar,

You told me to do it... o_O

Here I have combined Key Timers and Key Triggers to produce this. I daresay 80% of your code was taken from Key Timers in the first place, if you want to look at it that way.

One timer per period. As stated.

I didn't rip this from TT....

Yes I told you to do it, but you saw how to do it by using TT.
If TT was never published you could not have made this could you?
The large amount of code is basically the same as in TT,
it does not matter whether you used copy/paste or you typed all the letters yourself it is still taken code.

Now I don't ask for credit when people use my stuff in their spells/maps,
but modifying other peoples code and without stating so is a completely different business.

Don't get me wrong here, I am not accusing you of stealing,
(you did enough additional work to make it a separate system)
and I am not trying to make your system "forbidden" or something stupid like that.
I am just saying that you used much more of my work than you stated in documentation, and all I asked you was to update your documentation,
not to change your code.

Stuff like this are common courtesy in free software world.
So I am basically asking you to be polite.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Well said then.

>If TT was never published you could not have made this could you?
I honestly don't believe that I've edited TT to make KT2 (I haven't and even the code structure is different and based off Key Timers), but I see that what you did with conditions was a make/break demonstration for KT2 to occur. So I'll give credit to you for making TT which demonstrated it.

>Don't get me wrong here, I am not accusing you of stealing,
>(you did enough additional work to make it a separate system)
>and I am not trying to make your system "forbidden" or something stupid like that.
Thankyou for being someone with their head screwed on. Really :). This could've dragged on for ages and turned out nasty. I'm happy to give credit for whatever you innovated.

Though it would've been nice if you PMed it to help keep this thread on topic. Let's hope the following posts can be about using the system... :)

Edit:
>Give up on using TriggerExecCount as attaching method it simply does not work ffs.
Be realistic. It's fine. If people ever complain because it lags when they use it in application, I'll reconsider. In this system however I consider it a perfect, practical application of the method, as it is MUCH more likely to reduce lag than create it. Furthermore, I optimized the exec increment so that it doesn't always have to loop the full number of times.
Don't say it doesn't work when you should know perfectly well that it does, and tucked away in this system it's noob friendly (unlike on it's own).
If you want, I'll take up a bet with you that no one will ever complain of lag due to exec count, unless it's someone trying to proove me wrong.
For everyone else wondering what we're talking about... KT2 attaches the struct in a way that is insanely efficient for loading and looping and stuff, but in extreme cases (600 existances of one struct type, or intialization of 50 timers at once) can cause a tiny tiny jump in the game when calling Add. It's a good tradeoff, imo, especially because that negative risk simply won't happen in general.
 

Sim

Forum Administrator
Staff member
Reaction score
534
New version does not mean new thread.
 
Status
Not open for further replies.
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top