Just plain help - Timer

redundant post. this is basically the same thing posted at the link below.

Sorry for the bad photo quality idk why that happens?’
But anyway, i’m trying to create a timer and thinking about it dong it by an event would be easier but I just want to know why the script does not work. I had to use a gameinstance because I want the timer to continue over two+ levels. IT worked before I added the game instant but now when I added it, it stays at 0:0 and idk why.
Any help?

What’s going in there?! Tick + Cast to GI + Delay.

Why do folks insist on adding seconds together instead of using an actual timer that does all that and more? Is there a tutorial everyone bumps into and tries to follow it?

wdym by an actual timer - using an event?

wdym by an actual timer - using an
event?

Just to keep it simple, in the Game Instance:

In a widget:


Game Instance does not tick so it cannot dispatch anything on Tick ;p Ignore what I said about dispatchers before - my bad.

The above works fine, though!

Thanks that makes it much smoother, another thing - I want one timer going throughout, then a timer for how long each level takes (which goes up during that level then stops at the end), each level ends with a trigger box. Any ideas?
I’m picturing like a list which does
Level 1 -X:XX:XX
Level 2 -X:XX:XX
etc.

The game instance has a timer and each level can have its own timer. That’d be the easiest I guess. Once the level ends, cast to game instance and save the resulting timer from Lvl1 there; add it to an array perhaps.

if you need the time of the level your in then you could use something like the get game time node or one of the many other time nodes. the get game time will return the time the level has been open and there are options so that it can be affected by pausing the game and to compensate for time dilation aka slomo.

How can my game tell when a new level starts (would I need to cast to level blueprint)?

How can my game tell when a new level
starts (would I need to cast to level
blueprint)?

Each level has Begin Play - you can start the timer there.

Or, when you load another level, have the Game Instance start another timer. So you have one timer that lasts for the duration of the game and another timer for the current level only.

When you load a new level, tell the GI to restart the 2nd timer. This way you can have it all in one place rather than scattered across levels. Up to you.

For some reason I am really struggling, all I have got so far is a mess of blueprints and loads of thing of got to clean-up, i’m not sure why but nothing is working, sorry to be a pain but can you help me some more?

heres a little example that may be of some help. in the below picture the top section is the game instance and the lower section is the game mode (separated by red line).

ok so for the game instance. when the game instance is initialized we begin the timer. in reality you would probably want this as a custom event that begins once the player hits the start button or when the first level loads. this timer should run at all times, every one second, in a never ending loop. ok so each time the timer is called we increment the total game time. then we need some script to tell us if we have a level loaded, no need to increment if no level and this is where you control if you want to increment the level time for example if you are still in the completed level and loading the next then you wouldnt want the level time to still be incrementing. after that we need a way to tell which level it is we are in, for this i decided to use the array index to represent the current level number. so we use the current level variable to track what level we are currently in, get that index from the level time array and increment the value.

in the game mode i simply made a script that increments the current level variable. the game mode is created upon each new level so i used the begin play here to run the script. basically anytime you open a level a new game mode instance is made to the begin play will be called.

theres many other ways you could track the level for example you could use the level name and parse the name to figure out which level you are in.

Edit: the increment i was using for the level time was only working some of the time so i would switch that out with a set array element node with size to fit checked so you dont need to worry about the array size.

And here’s a very different approach that keeps the track of the time played throughout any number of levels.

You need the game instance and a single actor, called Next Level here (you will also need some levels, of course!)


The Game Instance:

  • Event Init: starts the Game Timer that it will run throughout the game
  • Start Level Timer: starts the timer for the current level (equal to the above if we’re in lvl1)
  • Save Timer: saves the result of the above to a string array, adding the name of the current level

The Next Level actor has 3 roles:

  • open the next level defined in the Next Level Name variable
  • show the timers running in the Game Instance
  • show the results of any previous time scores

This actor is placed in each level and has a trigger box:

From the top:

  • create a reference to the GI and Start a timer for this level
  • Tick prints the timers from the GI and then prints any old results
  • entering the trigger box, tells the GI to Save the Timer and Open next level

And a quick demo:

Image from Gyazo

I’ll just add that you will need widgets to display it properly, Print String is just for debug. I think it would be reasonable to create the widgets in the Next Level actor since they need to be recreated anyway. Do tell if you have issues implementing that.

I made a bunch of tutorials on these custom timer/stopwatch actor components you can add to any game actor creating a master clock, timed progress bars, dates etc that you might find useful. See videos 4, 6, 8 and 9. All involve timers of some Sort :slight_smile:

I will attach a little gif later but don’t rlly have the time now, basically, so I haven’t attached it to widgets yet but the string prints infinite game time: … then current level time:… then game time: … and on, which isn’t the same but wouldn’t matter in widgets, when I go to Level 2 it says Level 1:(My time) then just stops and see nothing else printed. Any ideas what i did wrong.
Also in the actor is game instance a variable in the actor because I just did a pure cast node to my game instance then got my game instance variable from there.
Thanks for your help.
(Might also see the first guys blueprint as well cus I completely forgot he replied as well)

string prints infinite game time

set the Print String duration to 0 - it will make it more managable

Any ideas what i did wrong.

Not without seeing what you have, show us how you wired it up and I’ll try to poke holes in it. Ideally, the GI and the actor graphs.

Also in the actor is game instance a
variable in the actor because I just
did a pure cast node to my game
instance then got my game instance
variable from there.

Should be absolutely fine with a pure cast. But perhaps try to replicate what I demoed first and only than adapt it to your needs.

That is the thing I couldn’t replicate it for some reason.
Anyway I attached what I have done.

In order to create variables of the correct type quickly, you can promote pins:

This works with every pin in the engine, super useful, no need to look for the types on the list.

Are you sure the GameInstance is assigned; at the very bottom:

Perhaps you forgot to place the NextLevel actor in the levels?

Is it printing anything? What values are you seeing?

yeah I realized that I didn’t add the actor the second I got into bed, I’m loading up Unreal to add it now - sorry for being so dumb.