Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (797)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How Do I time events?  (Read 4330 times)
0 Members and 1 Guest are viewing this topic.
Offline ndnwarrior15
« Posted 2016-08-14 10:44:18 »

Let's say I want some event to happen 2 seconds after the clock starts, then have  a second event occur 2 more seconds (4 total) after that.

How can this be accomplished in a game?
Offline VaTTeRGeR
« Reply #1 - Posted 2016-08-14 11:49:05 »

You keep these variables:
time = 0f, threshold = 2f,
count = 0, event[] = {event0, event1}

Every frame you do:
1  
2  
3  
4  
5  
time = time + delta
if time >= threshold and count < event.length
    execute event[count]
    time = time - threshold
    count = count + 1

delta is the time in seconds between update/render loop calls (60fps ~> 0.016s).
 Kiss
Offline ShadedVertex
« Reply #2 - Posted 2016-08-14 11:57:11 »

...Or you could just use the java.util.Timer class, which allows you schedule delayed events. However, this happens on a different thread, so I don't know if the Timer class is what you want...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VaTTeRGeR
« Reply #3 - Posted 2016-08-14 12:06:58 »

He's talking about having this in a game, his event is probably modifying game data, it would be suicide (or at least too much effort and also inconsistent with game time) to use an asynchronous threaded timer.
Offline ShadedVertex
« Reply #4 - Posted 2016-08-14 12:30:45 »

delta is the time in seconds between update/render loop calls (60fps ~> 0.016s).
 Kiss

Don't you mean 0.016 mins?

He's talking about having this in a game, his event is probably modifying game data, it would be suicide (or at least too much effort and also inconsistent with game time) to use an asynchronous threaded timer.

Well, in that case, I guess he should just do it on the main thread.

Offline Catharsis

JGO Ninja


Medals: 76
Projects: 1
Exp: 21 years


TyphonRT rocks!


« Reply #5 - Posted 2016-08-14 12:39:13 »

`search` is your friend...

http://www.java-gaming.org/index.php?topic=24220.0

Check out the TyphonRT Video Suite:
http://www.typhonvideo.com/

Founder & Principal Architect; TyphonRT, Inc.
http://www.typhonrt.org/
http://www.egrsoftware.com/
https://plus.google.com/u/0/+MichaelLeahy/
Offline VaTTeRGeR
« Reply #6 - Posted 2016-08-14 12:41:39 »

http://lmgtfy.com/?q=0.016s+to+ms
Offline SHC
« Reply #7 - Posted 2016-08-14 12:54:14 »

I keep a custom timer which is updated automatically at the same time as game update events. Here is the class I'm using.

https://github.com/sriharshachilakapati/SilenceEngine/blob/silenceengine-1.0.1/silenceengine/src/main/java/com/shc/silenceengine/utils/GameTimer.java

It is just implemented based on keeping track of elapsed time since last update. These should be updated each frame, and the callback is called once the elapsed time is greater than the scheduled time. And the usage, how I use it.

Run once timer

1  
2  
3  
4  
5  
6  
GameTimer timer = new GameTimer(2, TimeUtils.Unit.SECONDS);
timer.setCallback(() ->
{
    // Do the timer task here.
});
timer.start();

Repeating timer

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
GameTimer timer = new GameTimer(2, TimeUtils.Unit.SECONDS);
timer.setCallback(() ->
{
    // Do the timer task here.

    // Reschedule the timer
    timer.setTime(newTime, newUnit);
    timer.start();
});
timer.start();

In case you didn't set the new time, it will just use the old values. Hope this helps.

Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #8 - Posted 2016-08-14 16:31:41 »

Just keep track of game time. You actions are scheduled to take place at some point in the future. When the time passes a certain point, do the action.

Offline philfrei
« Reply #9 - Posted 2016-08-14 19:15:30 »

...Or you could just use the java.util.Timer class, which allows you schedule delayed events. However, this happens on a different thread, so I don't know if the Timer class is what you want...

When triggering events from another thread, which I do a lot (e.g., between the GUI and my audio-processing thread, or the audio-processing and the game loop thread), a good technique is to have one thread throw a flag on the other, often via a volatile boolean. The other thread spots the flag and acts accordingly.

I'm not sure if this is strictly the design pattern named "loose coupling" or just one form of it. But it is good to understand patterns for messaging between threads. If one is worried about opening up a can of worms by using another thread, maybe the real concern should be that one is holding a can of worms, and that looser-coupling/better messaging would improve the structure of the code.

music and music apps: http://adonax.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ndnwarrior15
« Reply #10 - Posted 2016-08-16 00:52:35 »

@philfrei

Any chance you could provide a simple example?
I feel this would be the best way going about things since I wish to sync to an audio clip.
Offline philfrei
« Reply #11 - Posted 2016-08-16 04:10:07 »

@philfrei

Any chance you could provide a simple example?
I feel this would be the best way going about things since I wish to sync to an audio clip.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;

public class Avatar {

   public enum EmotionalState { GLOWING, DEPRESSED, ENVIOUS, EMBARRASSED };
   
   private EmotionalState currentEmo;
   private Color avatarColor;
   private volatile boolean changeEmotions;
   
   private Color golden = new Color( 248, 248, 0 );
   
   // This next method is the "loosely coupled" hook.
   // We don't DO anything here except flip a switch.
   // The thread with the game loop updates and renders in its own time.
   public void setChangeEmotions(boolean bool)
   {
      changeEmotions = bool;
   }  
   
   public void update()
   {  
      if (changeEmotions)
      {
         // clear flag
         changeEmotions = false;

         int random = (int)(Math.random() * 4);
         switch (random)
         {
         case 0: currentEmo = EmotionalState.GLOWING; break;
         case 1: currentEmo = EmotionalState.DEPRESSED; break;
         case 2: currentEmo = EmotionalState.ENVIOUS; break;
         case 3: currentEmo = EmotionalState.EMBARRASSED ;
         }        
      }
   }
   
   public void render()
   {
      switch (currentEmo)
      {
      case GLOWING:
         avatarColor = golden;
         break;
      case DEPRESSED:
         avatarColor = Color.BLUE;
         break;
      case ENVIOUS:
         avatarColor = Color.GREEN;
         break;
      case EMBARRASSED :
         avatarColor = Color.RED;        
      }
     
      drawAvatar(avatarColor);
   }
   
   private void drawAvatar(Color color)
   {
      // draws avatar based on current emotional state
   }
}
   
   
class GameLoopThread
{
   private boolean running;
   private Avatar avatar;
   
   public GameLoopThread(Avatar avatar)
   {
      this.avatar = avatar;
   }
   
   public void run()
   {
      while (running)
      {
         // ...code to regulate frame rate to 60 fps...
         
         avatar.update();
         avatar.render();
      }
   }
}


class LaunchTimerTask
{
   Timer timer = new Timer();
   
   public void launchTask(Avatar avatar)
   {
      EmotionTrigger triggerTask = new EmotionTrigger();
      triggerTask.avatar = avatar;
     
      timer.schedule(triggerTask, 1000 * 60, 1000 * 60);
   }
   
   
   class EmotionTrigger extends TimerTask
   {
      public Avatar avatar;
     
      @Override
      public void run()
      {
         avatar.setChangeEmotions(true);
      }
   }
}


The main tools for triggering from audio are the Listeners that monitor when a Clip or SourceDataLine opens/starts/ends/closes, that sort of thing. The design pattern, I think, is called "Observer". Check the method:
javax.sound.sampled.line.addLineListener

Also this:
http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/LineEvent.html

For more granularity, I count sound frames and have rolled my own "Listener" equivalent.
For the following link, see the last section "Manipulating the Audio Data Directly"
http://docs.oracle.com/javase/tutorial/sound/controls.html

music and music apps: http://adonax.com
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (3676 views)
2018-11-16 13:46:03

hadezbladez (1318 views)
2018-11-16 13:41:33

hadezbladez (3681 views)
2018-11-16 13:35:35

hadezbladez (712 views)
2018-11-16 13:32:03

EgonOlsen (4024 views)
2018-06-10 19:43:48

EgonOlsen (4548 views)
2018-06-10 19:43:44

EgonOlsen (2708 views)
2018-06-10 19:43:20

DesertCoockie (3576 views)
2018-05-13 18:23:11

nelsongames (3723 views)
2018-04-24 18:15:36

nelsongames (4755 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!