- Home /
Calling a function only once in Update
I know I should probably be able to figure how to do this by myself, but for some reason I can't wrap my brain around it.
I have a function that is in Update() that checks for dropping health percentage. Every time health drops a significant percentage, I want the camera to shake.
My problem here is that it continually gets called, and therefore doesn't stop shaking after the first time.
How should I fix this?
The function:
function CheckHealthPercentage( healthPercent : float ){
if(healthPercent == 1){
damageLevel = "No";
damageColor = Color(0,1,0);
}
else if(healthPercent < 1 && healthPercent >= 0.8 ){
damageLevel = "Minimal";
damageColor = Color(1,1,0);
cameraController.ShakeCamera( 0.5, 1 );
}
else if(healthPercent < 0.8 && healthPercent >= 0.5 ){
damageLevel = "Moderate";
damageColor = Color(1,0.6,0);
cameraController.ShakeCamera( 1.0, 1 );
}
else if(healthPercent < 0.5 && healthPercent >= 0.2 ){
damageLevel = "Heavy";
damageColor = Color(1,0.2,0);
cameraController.ShakeCamera( 1.5, 1 );
}
else if(healthPercent < 0.2 ){
damageLevel = "Intense";
damageColor = Color(0.6,0.1,0.1);
cameraController.ShakeCamera( 2, 1 );
}
}
The problem here is that it needs to be called only once, but also EVERY TIME a damage level shifts downward. I can't reset it because it is called on update and will just reset itself every frame :/
Answer by Dave-Carlile · Feb 08, 2013 at 06:52 PM
The concept here is that you want to execute some code only when the state changes. In this case when the damage level changes. Your current code is always executing the code when you're in the state, not when you change to the state.
In order to check state changes you need to keep the previous state, and the current state. Then, when you want to check for a state change you compare the current state to the previous state. If it's the same then you know you didn't change state. If it's different then you did change state, so you can execute your "state change" code.
Here's what I would do with your code (with most of it left out, just showing this as an example)...
function CheckHealthPercentage(healthPercent : float)
{
// save current damage level
var previousDamageLevel : string = damageLevel;
if (healthPercent == 1)
{
damageLevel = "No";
}
else if (healthPercent < 1 && healthPercent >= 0.8)
{
damageLevel = "Minimal";
}
// did the damage level change?
if (damageLevel != previousDamageLevel)
{
// TODO : shake it
}
}
My Javascript sucks, so no guarantees of syntax correctness :)
Gotcha. This looks good to me. I'll try it out and post results.
You can modify this to only execute the state change code when "the damage level shifts downwards" by changing damageLevel
to an int, and doing the compare as damageLevel > previousDamageLevel
.
Ah thank you. I hadn't thought of that! Works like a charm by the way. Thank you.
Answer by Kergal · Feb 08, 2013 at 06:24 PM
easy - create a bool only call the function when bool is true. at the end of the function - set the bool to false.
example :
void Update() {
if(randombool){
SomeRandomFunction();
}
}
void SomeRandomFunction() {
// insert code here
randombool = false;
}
This would work except for the fact that it needs to repeat
well.. still easy :)
either call the function again. or create a loop how many times does it have to repeat that shaky thing? if each function call is one "iteration" do something like
if(somebool) {
while(actualShakes < numberofShakes){
Call the function ();
actualShakes ++;
}
if(actualShakes == numberofShakes){
somebool = false;
}
}
Answer by GC1983 · Feb 08, 2013 at 06:34 PM
Hmm, you could place a timer in there. Call the boolean when collision is made, and then let the timer run out. Make the timer variable public so you can set for how long you want it to shake. For Example:
float shakeTime;
bool cameraShake;
else if(healthPercent < 1 && healthPercent >= 0.8 ){
damageLevel = "Minimal";
damageColor = Color(1,1,0);
if(cameraShake)
{
shakeTime -= Time.deltaTime
cameraController.ShakeCamera( 0.5, 1 );
if(shakeTime <= 0)
cameraShake = false;
}
This, too, would work, but it needs to reset itself when damage levels change.
Heh ok, so then add the reset code in the if statement turning the shake off.
Your answer
Follow this Question
Related Questions
Calling A Function Once 0 Answers
Efficient way for calling a function once 3 Answers
Return multiple parameter 3 Answers
Call a function multiple times 2 Answers