Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I am writing some code sections here and there for a game I'm trying to devellope and I have written a piece to cause an action when a condition is met, however the action loops infinitely once the condition is met. I need it to only trigger once per condition met or to ask it to only run once?
Sorry I am a supreme novice and still learning, only started a couple weeks ago with Java script.

More in depth..
The condition is met when IE: target A "Hits" target B.
Then the below piece executes and prompts target A to use skill X, but this also counts as a "Hit" and causes the condition to be met again and thus infinite loop.
Please how do I stop this.. How is it written? Can this be explain in novice terms?

This is what I have.

var skill = 49;
var target = -2;
BattleManager.queueForceAction(user, skill, target);

NOTES: "user" is defined elsewhere
Did I explain this correctly?

What I have tried:

I tried creating and storing a variable to make an if condition of (times <= 1) but may not have written that correctly so it may not have worked right..
This is the block I tried

var times = 0;
var skill = 49;
var target = -2;
if (times < 1) {
BattleManager.queueForceAction(user, skill, target);
times = +1;
}
Posted
Updated 27-Nov-18 4:04am

1 solution

In JavaScript, like pretty much any language (I can think of no exceptions, at any rate), a function is invoked only once for each call. If you have an infinite loop, then you have a different issue.

Nothing you posted includes a loop of any sort (for, while, foreach) so either the code you posted is contained in a loop, there code you call (queueForceAction) contains a loop that is getting locked, or you have a recursive call tucked in there somewhere.

One good troubleshooting techniques in JS is to tuck console.log() statements into sections of code to help you diagnose where issues exist.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900