Timer Array and tick event

stefanoa

Active Member
Licensed User
Longtime User
I need an array of timer, which set a timer for each element of an array, consisting of 20 elements.

example:
objects (0) activates the timer (0) and the event "timer (0) _Tick"
objects (1) activates the timer (1) and the event "timer (1) _Tick"
....
objects (19) activates the timer (19) and the event "timer (19) _Tick"

is it possible?

Code: Copy
Dim timerEating(20) As Timer   
timerEating(actorID).initialize("timerEating", 3000) 
timerEating(actorID).Enabled = True
Code: Copy
Sub timerEating_Tick

'>> how to recognize the timer which has triggered the tick event?

end sub

How to recognize the timer which has triggered the tick event?

thanks
 
I know it's not the best idea, but you could do something like this:
Code: Copy
timerEating(actorID).initialize("timerEating" & actorID, 3000)
...
Sub timerEating0_Tick
end sub

Sub timerEating1_Tick
end sub
...
Sub timerEating19_Tick
end sub
If then you run similar code for all these cases, you could do for e.g.
Code: Copy
Sub timerEating0_Tick
handleActor(0)
end sub

sub handleActor(ActorID as int)
'do what you want here
end sub
 
Upvote 0
thanks..
is a good solution for a fixed array, but I might have 'n' (variables) elements..20 or 30 or 40 elements...
 
Upvote 0
Here:
Code: Copy
Sub Process_Globals
   Private timers As List
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      timers.Initialize
      For i = 1 To 10
         Dim t As Timer
         t.Initialize("timer", Rnd(500, 5000))
         timers.Add(t)
         t.Enabled = True
      Next
   End If
End Sub

Sub Timer_Tick
   Log("I'm timer number: " & timers.IndexOf(Sender))
End Sub
 
Upvote 0
very good!

The problem is that i can't activate it at the same time..
I have several objects and each could set a timer, also at the same time. (eg 1' object activates a timer and, before it finishes, it can also start the 2' timer). I do not know if it is possible...

This code is with your solution:
Code: Copy
Dim timersEating As List
Code: Copy
Sub Activity_Create(FirstTime As Boolean)
   
If FirstTime Then
  timersEating.Initialize
  For i = 1 To 20
    Dim t As Timer
    t.Initialize("timer", 1000)
    timersEating.Add(t)
  Next
End If

Code: Copy
Dim t As Timer
t.Initialize("timersEating", 3000)
timersEating.InsertAt(actorID, t)   '--- punt in correct position based on actorID
t.Enabled = True
Code: Copy
Sub timersEating_Tick
   Dim t As Timer
   t = Sender
   
   Dim actorID = timersEating.IndexOf(Sender)
   
   If actorID < 0 Then Return
   
   ........
   
   timersEating.RemoveAt(actorID)
   t.Enabled=False
      
End Sub
 
Upvote 0
i think not.. because the problem is the Tick event, that is unique for all timers, with this solution..so, if i start a second timer before it finishes the first, the first stops before it expires.

is a bit difficult to explain, but think I have 10 objects (for ex.) on the screen, I click on an object and I have to show for 20 seconds, over the object, a label (first timer) with is value.
Before it disappears, I click on a second object and I have to show for 20 seconds, over the object, a label with his value (second timer).
and so I could do to other objects on the screen, thus activating multiple timers simultaneously...
 
Upvote 0
How critical is the timing? You could look at having one timer that ticks every second, in that code it could check a start time recorded for each object when clicked, and update the count in the related label.
 
Upvote 0
Strange, I thought I had given a similar answer to stevel05's one, probably I didn't hit enter?
Anyway, I write it again:
From what I saw in your code, you want to perform a task and then disable the timer for each one of your actors. If that's the case, I would choose to have just one timer, and an array (or list If I need to remove them) of flags raised every time I want to perform something with an actor (I also guess that the actions are similar. For e.g.
Code: Copy
dim startActor() as boolean
...
'start it based on your needs
startActor(ActorID)=true
...
sub yourTimer_tick
for i=0 to startActor.length-1 
   if startActor(i) then
          startActor(i)=false
          'do what you want with this actor, here
   end if
next
end sub
 
Upvote 0
cool.. your examples seems to works correctly!
now i've to try how apply it to my code...

i'm developing my first 2d game and it's not simple :D

thanks for now @mc73 @stevel05
 
Last edited:
Upvote 0
Back
Top