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 8224655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:08:49+00:00 2026-06-07T15:08:49+00:00

Function PS.Tick() is called every 100 milliseconds and its job is to call AI

  • 0

Function PS.Tick() is called every 100 milliseconds and its job is to call AI function of NPCs so they can move:

PS.Tick = function ()
{
"use strict";
for (NPCid = 0; NPCid < NPCnumber; NPCid++)
{
    NPCAI(NPCid);
};
}; 

But I want the NPCs to not move simultaneously every 100 millisecond, but do it at their own frequency, so I tried this code:

PS.Tick = function ()
{
"use strict";
for (NPCid = 0; NPCid < NPCnumber; NPCid++)
{
    var timeout = 0;
    timeout = PS.Random (1000);
    setTimeout("NPCAI(NPCid)",timeout);
};
};

Now, they don’t move at all. Why? How do I make them move at different time intervals?

  • 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-06-07T15:08:52+00:00Added an answer on June 7, 2026 at 3:08 pm

    Now, they don’t move at all. Why?

    A couple of possible reasons:

    • If NPCid isn’t declared anywhere: Your code is throwing a ReferenceError.

    • If NPCid is declared somewhere but it’s not a global: When you pass a string into setTimeout, it doesn’t get evaluated in the current execution context and doesn’t have access to NPCid. In general, don’t pass strings into setTimeout.

    • If NPCid is a global: When the delayed code is executed, they’ll all see the same value for NPCid, which is its value at the end of the loop.

    Instead: If you’re doing this on NodeJS (I’m just inferring this from what you’re doing), you can do this):

    PS.Tick = function ()
    {
        "use strict";
    
        // (I'm assuming NPCid is defined somewhere; if not, add `var NPCid;` here)
    
        for (NPCid = 0; NPCid < NPCnumber; NPCid++)
        {
            var timeout = 0;
            timeout = PS.Random (1000);
            setTimeout(NPCAI, timeout, NPCid); // NodeJS (and Firefox) ONLY!!
        }
    };
    

    That works because on NodeJS (and Firefox), setTimeout can accept arguments to pass to the function to call.

    If you’re not using NodeJS or Firefox, but you do have access to ES5’s Function#bind, you can do this:

    PS.Tick = function ()
    {
        "use strict";
    
        // (I'm assuming NPCid is defined somewhere; if not, add `var NPCid;` here)
    
        for (NPCid = 0; NPCid < NPCnumber; NPCid++)
        {
            var timeout = 0;
            timeout = PS.Random (1000);
            setTimeout(NPCAI.bind(undefined, NPCid), timeout);
        }
    };
    

    Function#bind returns a function that, when called, will call the original function with a specific this value and the arguments you give it.

    If not, you can write your own bind, or do it like this:

    PS.Tick = function ()
    {
        "use strict";
    
        // (I'm assuming NPCid is defined somewhere; if not, add `var NPCid;` here)
    
        for (NPCid = 0; NPCid < NPCnumber; NPCid++)
        {
            var timeout = 0;
            timeout = PS.Random (1000);
            setTimeout(makeHandler(NPCid), timeout);
        }
    
        function makeHandler(id) {
            return function() {
                NPCAI(id);
            };
        }
    };
    

    That works by creating a function that, when called, turns around and calls NPCAI with the value we pass into it.

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

Sidebar

Related Questions

How can I use call with setInterval to get an object literal to invoke
I've the following method which allows me to protect MySQL entities: public function Tick($string)
This is my 'tick' function: - (void) tick: (ccTime) dt { NSLog(@%d,ticker); if(fbut.Adown ==
i have used a mail function. after inserting every record simultaneously from multiple PC.
`I have a function called lets say calculate; what i want to do is
IN SHORT: I have a method being called at 44,100 times per second. I
I would like to specify a function to be called in my VB.Net 2005
I got this timer tick function: Private Sub controlTick(ByVal sender As Object, ByVal e
I'm thinking about how to find from where any function was called. The problem
I am creating a game in javascript and my gameloop is called every 30ms,

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.