Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58430 Members - Latest Member: Jesse Webb

April 26, 2024, 06:21:16 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Help with Round Timer Script
Pages: [1]
Print
Author Topic: Help with Round Timer Script  (Read 958 times)
Abel
TIGBaby
*


Professional derp


View Profile WWW
« on: July 16, 2016, 05:49:08 PM »

Hello.
I'm making a political satire strategy game, similar to that of Plants vs Zombies. For this I need a working round timer script, however the code I wrote doesn't seem to be working properly. I want to avoid coroutines, but if I must use them, then I will.

Code:
    
void StartRound(float RoundTime)
{
    RoundTime = 60;
    InvokeRepeating("GenerateMexican", 5, 5);
    Debug.Log("Round started");
    do
    {
        RoundTime -= Time.deltaTime;

        if (RoundTime <= 0)
        {
            CancelInvoke("GenerateMexican");
            Debug.Log("Round ended");
        }
    } while (RoundTime > 0);
}     
(The function is invoked by the click of a button)

The problem is, the round immediately starts and ends, the value, as I watch it in the editor, never changes either, even if I set it to something outside of the range of 0-60. I am unsure why this is happening.
Thanks in advance! Smiley

Sidenote: The function I'm invoking is GenerateMexican, not intended to be offensive, it's just a satire game about Mexicans revolting against Donald Trump.
Logged

I'm that kid who wants to be Peter Pan.
voidSkipper
Level 2
**


View Profile
« Reply #1 on: July 16, 2016, 08:54:18 PM »

I think you need to take a little bit of a refresher course on how while loops work.

Your do...while... structure currently endlessly decrements RoundTime until it goes past zero.

It does this all before it exits the function.

So, no matter what number you put in "RoundTime", as long as "Time.deltaTime" is greater than zero, RoundTime will be less than or equal to zero by the time StartRound completes. If Time.deltaTime is less than or equal to zero, your do...while... loop will loop endlessly and eventually crash your game.

I don't quite know what you were trying to achieve, but I get the feeling you actually want the contents of your loop to be inside a frame handler or timer.
Logged
Cunnah
Level 1
*



View Profile
« Reply #2 on: July 18, 2016, 01:33:33 AM »

Wow you have over complicated things for yourself. You need to call this function in the void Update() function.

You should read up on the order that Unity calls functions.

Your code should look something like this;

Code:



public float Roundtime //Store the length of the round.
float _currentRoundtime //store exactly where in the round the timer is from update to update.

void Update()
{
    StartRound();
}

void StartRound()
{
    _currentRoundTime -= Time.deltatime;
    
    if (_currentRoundTime <= 0)
    {
        EndTheRound();
        _currentRoundTime = RoundTime; // end the round and reset the timer.
    }
}

Sorry if this seems a little janky as I am typing on a work computer. You might want to put a check in the start round function such as a boolean to ensure that the round has started otherwise this code would just continuously count down every frame.

I hope this helps.
Logged
eerr
Level 0
***


View Profile
« Reply #3 on: July 18, 2016, 09:30:25 AM »

@Abel

Delta time doesn't give you the time since you last called delta time.

Nope. Never did. It's not even a function.

Your princess is in another castle.
« Last Edit: July 19, 2016, 03:43:09 AM by eerr » Logged
Cunnah
Level 1
*



View Profile
« Reply #4 on: July 19, 2016, 01:39:30 AM »

Delta time doesn't give you the time since you last called delta time.

Nope. Never did. It's not even a function.

Your princess is in another castle.

What you on mate? The update function is called every time the game updates and time.deltatime is the time between frames.
Logged
eerr
Level 0
***


View Profile
« Reply #5 on: July 19, 2016, 03:42:49 AM »

Ahhh I was talking to him, I will clairify this whoops.
Logged
Cunnah
Level 1
*



View Profile
« Reply #6 on: July 19, 2016, 07:01:31 AM »

Fair enough Smiley
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic