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?

B4X:
Dim timerEating(20) As Timer   
timerEating(actorID).initialize("timerEating", 3000) 
timerEating(actorID).Enabled = True
B4X:
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
 

mc73

Well-Known Member
Licensed User
Longtime User
I know it's not the best idea, but you could do something like this:
B4X:
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.
B4X:
Sub timerEating0_Tick
handleActor(0)
end sub

sub handleActor(ActorID as int)
'do what you want here
end sub
 
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
thanks..
is a good solution for a fixed array, but I might have 'n' (variables) elements..20 or 30 or 40 elements...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
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

stefanoa

Active Member
Licensed User
Longtime User
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:
B4X:
Dim timersEating As List
B4X:
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

B4X:
Dim t As Timer
t.Initialize("timersEating", 3000)
timersEating.InsertAt(actorID, t)   '--- punt in correct position based on actorID
t.Enabled = True
B4X:
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

stefanoa

Active Member
Licensed User
Longtime User
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

stevel05

Expert
Licensed User
Longtime User
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

mc73

Well-Known Member
Licensed User
Longtime User
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.
B4X:
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

stevel05

Expert
Licensed User
Longtime User
I was thinking similar, along the lines of the attached
 

Attachments

  • ttest.zip
    7.4 KB · Views: 307
Last edited:
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
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
Top