Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3679194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:25:51+00:00 2026-05-19T03:25:51+00:00

I have a simple timer class in a game, intended to allow me to

  • 0

I have a simple timer class in a game, intended to allow me to make an entity do something every so many seconds. It has two functions:

void Timer::GameTimer() // Gets time between frames
{
    ::ElapsedTime = Clock.GetElapsedTime();
    Clock.Reset();
}

This just updates the ElapsedTime Global so that it can be grabbed by anything that needs it. It’s placed inside the gameloop.

Then the important one:

bool Timer::tCounter(int total) // Returns true every 'total' seconds
{
    static float ftime; // Variable to hold the mounting elapsed time
    static int n; // Counter variable for true trigger

    ftime += ElapsedTime;

    int itime = (int)ftime; //Truncate

    if(!(itime%total) && itime != 0)
    {
        n++;
    }
    else
    {
        n = 0;
    }

    if (n == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Timer is instantiated inside the Entity class as Timer::Timer timer

The function is then called inside Entity::Update once per loop for every instance of the entity class in the game:

if (timer.tCounter(2))
    {
        Do stuff
    }

The problem is it doesn’t seem to count uniquely for each instance of Entity, resulting in only one doing stuff at a time and other weird stuff occasionally happening. The problem seems to be the static variables being shared. How do I ensure that this tCounter function holds unique variables for each instance that calls it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:25:51+00:00Added an answer on May 19, 2026 at 3:25 am

    static in this context means only one instance of something exists at a time and is initialized once.

    The easiest fix to me would be to (not necessarily the best fix):
    – Remove the static qualifier
    – make ftime and n member variables of Timer
    – Create an instance of Timer for each entity that you want to react to a timer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For quite awhile I have been trying to make a simple game in Java
Ok, i have simple scenario: have two pages: login and welcome pages. im using
I develop tools in Autodesk Maya. Many of the tools I build have simple
I have a simple webform that will allow unauthenticated users to input their information,
In my game engine I have a State class that represents the game world
I have simple regex \.*\ for me its says select everything between and ,
In general, is it a best practice to have simple POJO Java classes implement
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple 2-column layout with a footer that clears both the right
I have a simple page with my ScriptManager and my UpdatePanel , and my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.