Forum rules - please read before posting.

Displaying Speedrun Timer question

edited December 2024 in Technical Q&A

Hi, I've managed to put together a game I'm pretty proud of, still WIP and the writer is going to take a pass:

https://mikdog.itch.io/bru-boegie-get-the-milk
password: kiff

One of his ideas, which I think is kind of funny, is when the 5-10 min experience is complete, is showing a cutscene of the character walking through a portal and the game starting again, with the player's finish time visible to the player, and then giving the player the chance to beat their own time in a kind of 'speedrun' challenge, that I guess players could compare their time to other players' time in the comments or something.

I see there's an AC Global Timer which is great, but wondering how best to display the timer on the screen please? I'd also like to be able to display the current time or player's finishing time in a character's dialogue, I'm not super au fait with code but I'm guessing it would be a [timer] string or something I'd weave into the dialogue?

And I think if possible, some help please with grabbing the timer's time at a certain time and displaying it, stopping and starting a timer, and resetting a timer (I've perused the forums and it looks like there was some fiddliness with resetting a timer, maybe that's been updated?)

Thank you.

«1

Comments

  • There's currently no [timer] tag, but that's an excellent suggestion - I'll see this gets included in v1.82.2.

    Timers will be reset when the game is restarted (via the Engine: End game Action), and also if the Variable: Set Timer Action is used to Play a Timer that has previously been completed. A dedicated "Reset" command would be a fair addition as well, however.

  • Hi Chris, thanks for the reply. I think I'll maybe leave out the 'reset and speed run' section of the game to when it's more easily includable.
    Another related question - if I wanted to reset the game for a player to try again in a kind of simulated time-warp back to the beginning akin to reloading the game (ie I might just leave out the timer for now), is there an (easy) way to reset all local and global variables back to their original settings? Or maybe easier to just reset the game?
    I had ChatGPT make me a custom script to reset all variables but I haven't had a huge amount of success using it to get things right all the time.

  • Resetting the game via the Action is the easiest way. You can reset through scripts, but when it comes to scene data it's all or nothing - you can't reset the local variables of a given scene, rather you reset all data within the scene, Remember components included.

    To reset the data of a given scene, use the ClearLevelData function:

    AC.KickStarter.levelStorage.ClearLevelData (mySceneName);
    
  • Hi Chris, thanks for the reply.
    My apologies, I really am a n00b - so, when the game is finished and I wanted to restart it, how would I trigger that function please? Would it be in a custom script attached to a blank GameObject that's triggered by an ActionList with 'Object - Send Message' or similar?
    I had a look at the linked ClearLevelData page - it seems I'd create a 'custom event' that would allow AC to interact with custom scripts, but I'm still not sure where the bridge part between AC and custom functions & scripts happens, or where to attach the script to the custom event, as the event options don't seem to have an external script option. Eg I can see in the Events Editor there's an option to have a new event for what happens when 'OnBeginGame', but the option here is only to make an ActionList, and I'm not sure where in an ActionList the script is placed?

    And in that code example I'm guessing I could add all my scenes there - ClearLevelData (mySceneName, lounge, bathroom etc...)

    Thanks

  • What is it exactly you're looking to do that makes the provided "Restart Game" command unusable in this case? That would essentially perform the same operation - clearing the data of each scene.

  • Aaaaaaah lol, apologies, I was trying to over-engineer something that already existed, have done that a lot and then later realised an action was designed specifically to easily do what I'm trying to do. :s I knew of the 'Engine: End Game' action, but didn't notice I got the option to restart the game along with a scene number to choose. Thanks, I think that's pretty much perfect. :)

    Am looking forward to the speedun timer. Was looking into the included timer and I think it may be more for mix/max values for eg countdown timers to make a decision in a given time. I could get a timer running, but wasn't sure if there was currently a way to grab the ongoing timer's current time and display it, or maybe that's what the possibly future [timer] tag will do.

    In any event, thanks!!

  • Yes, the [timer] tag will let you do this with a time-formatted display. You can currently use the [var] tag, if you've linked the Timer to a variable, but it'll show the raw value, not one formatted to appear like a time.

  • edited December 2024

    Hi Chris, hope all well, in lieu of the new timer to be included with a possible upcoming update of AC, I've been trying to get another timer plugin 'TimersMadeEasyLite' to work with AC but am a little stumped. I can get the timer showing on screen ok by attached a canvas to the GameEngine and then the Timer prefab to it, but even when attached to the GameEngine, it's not permanent between scene switches, ie the timer resets when leaving a room. I'd also not be too sure how to get that timer's time info to be public so I can make a variable in AC of that string / code to show the time and then get the variable to show up in a character's dialogue. It's a bit beyond me though I understand how it probably should work.

    So, just wondering if possible for the current AC timer, if/how that works, and if the timer will keep running between scene changes? From what I can read, it sounds like I would be able to get the [timer] tag to work in a character's dialogue but it wouldn't be formatted for time?

    While sleuthing the forums I came across this:
    https://adventurecreator.org/forum/discussion/5019/global-timer

    Would you still recommend this method? It still sounds a little beyond me but I can imagine how a variable that gets increased by 1 second could work and then just calling that [variable] during a dialogue could work, but while I'll have the timer running in the background invisibly for the first playthrough, ideally I'd like to have the time formatted like a proper time (00:03:25) show up in the corner of the screen after the player first finishes the game so that they now see the timer working in the background.

    Edit: Actually just want to have the player's finishing time displayed in a character's line of dialogue to let them know their finish time I think, and then the game ends.

  • That's an old thread, I wouldn't pay attention to that.

    it sounds like I would be able to get the [timer] tag to work in a character's dialogue but it wouldn't be formatted for time?

    There's currently no [timer] tag, but when there is you'll have the option to control its formatting.

    For now, you can link a Timer to a global Float variable, and then use a simple script to convert it to a formatted String variable, which you can then display in speech text using the [var] tag:

    using UnityEngine;
    using AC;
    
    public class FormatTimer : MonoBehaviour
    {
    
        void Update ()
        {
            float time = GlobalVariabes.GetVariable ("Timer").FloatValue
    
            int totalSeconds = Mathf.FloorToInt (time);
    
            int minutes = (totalSeconds % 3600) / 60;
            int seconds = totalSeconds % 60;
    
            string formatted = $"{minutes:D2}:{seconds:D2}";
    
            GlobalVariables.GetVariable ("TimerFormatted").TextValue = formatted;
        }
    
    }
    
  • Hi Chris, thank you for the reply. So, I've got my Global Float variable [cool] and I've got an AC Timer 'linked to global variable' [cool].
    I see there's an option in the variable to 'Link to custom script' but no field to point it to. May I ask where I'd place it please?

  • You don't need to point it to anything - as the script above only reads its value, not sets it. It can be placed on a GameObject in any scene you wish to display a time-formatted value.

  • New timer in 1.82.2 seems to work great - exactly what I was looking for, thank you!

  • Hi Chris - really enjoying the ease of the new timer, thnx. A question please - is there any way to get the timer to not be affected by time scaling that happens in ActionLists? I have a little minigame as part of my main game, where I have a timer that's currently affected when I use slowmo 'bullet time' and was wondering if I could avoid that? Thnx

  • As in, with the Engine: Change Timescale Action?

    Yes, that should be possible to make optional.

  • edited February 13

    Yes, exactly re with 'Engine: Change Timescale'.
    Not a major necessity but would be nice to have as an option where the seconds timer ignores changes to timescale, thank you.

  • Hi Chris, hope all well. I think I discovered something - when saving and loading a game, the timer I think (that was started in an earlier scene) I don't think continues running. May I ask if there's a way to keep timers running after loading a game, please?

  • What's your AC version, and can you share images of the Timer's properties and Action that runs it?

    View the save file in the Save-game File Manager (top of the Settings Manager), and scroll down to the bottom of the "Main data" section. There should be a "Timers" entry - what does it show?

  • Hi Chris, thanks.
    It's AC 1.82.2
    For some more clarity, the timers work fine when someone plays the game from start to finish in one go, but when loading a saved game, seems like the timer resets and plays from zero.

    Here's the timer info I found in the Saved Game #2, just before the end of the game:

    I start 2 timers at the beginning of the game when someone clicks the 'New Game' button (I display them in 2 different places in 2 different formats):

    And this is what the 2 timers look like in the Editor:

  • seems like the timer resets and plays from zero.

    How are you determining this? From a label that displays its value?

    Check that this occurs in the latest release, with a backup/duplicate project. If so, what is the result of linking the value to a Global Variable? Does the variable's value reset? You can see its live value in the Variables Manager if "Show realtime values?" is checked.

  • Hi Chris, thanks. Yes, I'm displaying the timers as labels in two locations, once during character dialogue, and later during the credits in an empty NPC's dialogue:

    I linked the timer to a global variable and yes, when I load the saved game the counter starts from zero. This is testing/saving/loading in the Unity Editor btw, so maybe that's affecting the timer's saved values.

    Naive question - to test on a backup project, simply a matter of duplicating the Unity project's folder, adding it via Unity Hub and working off that? I'm right at the end of the project so I may leave this for now, but if I'm feeling brave I may give it a go. Thanks for including the cinematic black bars in the latest build btw, I tested that in a separate new demo project, very nice.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.