Commands tied to a bar that fills over time

Hey everyone,

I’m trying to make a window with several buttons that appears once a bar has finished filling up over time. I’ve managed to get the bar working fine and the window to appear once the bar is filled and disappear when a button is pressed. The problem is that one of the button’s functions is being difficult to properly construct. It is an ability that will cause the character to regenerate health until the bar is finished filling up again.

here is a bit of the script where I think the problem occurs.

This part takes place inside OnGUI.

// a boolean to turn on/off the abilities window for the timer bar
var tAbilitieswinswitch : boolean = false;

// an if statement to make the abilities window appear.
		if (tAbilitieswinswitch == true){
			var tAbilitieswin = GUI.Window (1, Rect(110,250, 100, 100), tAbilities, "Abilities");
			}

}	


function tAbilities (windowID){
// the buttons that appear inside the abilities window and their functions	
	var taButton = GUI.Button(Rect(5, 25, 50,20), "Attack");
	if (taButton == true){
		
		tAttackCommand();
		}
	var tdButton = GUI.Button(Rect(5, 45, 50,20), "Defend");
	if (tdButton == true){
		
		tDefendCommand();
		}	
}

and later on the tDefendCommand function, which doesn’t want to behave properly…

function tDefendCommand(){
//Function for character to defend once defend ability is used	
	Debug.Log( "Defend button is defending");
	tAbilitieswinswitch = false;
	tTimer = 1;
	if (tTimer >= 1   tTimer < tSpeed){
		
		tHP += 1 * Time.deltaTime;
		
	}
	if (tTimer >= tSpeed){
		
		Debug.Log("Defend button is done defending");
		
		}

tTimer and tSpeed are variables used for the bar that fills over time. That function is called during Update and looks like this…

function tTimerFill(){
	
// timer bar and ability activation
	if (tTimer >= 1   tTimer < tSpeed){
		
		tTimer += 10 * Time.deltaTime;
		
		if (tTimer >= tSpeed){
		
			tAbilitieswinswitch = true;
		
		}
	}
}

I have a feeling the problem is related to how Update and OnGUI work based on frames but I’m not sure how to make the tDefendCommand work properly. I’m kinda new to programming so forgive me if I’ve done something horribly wrong.

desperation bump.

Can you simplify your question a bit? I’ve read your statement through a couple times and still don’t understand what you expect to happen
or what is actually happening.

Sorry. I’ll try to reiterate.

Basically, I have a bar on the GUI that fills over time(last script function sample), once it fills entirely a window appears beneath it with a set of buttons (first script sample). If a button is pressed the bar is reset and begins filling again. Once the defend button is pressed I want the tDefendCommand function to regenerate the character’s health until the bar is finished filling again(2nd script sample). The first script sample is within OnGUI while tTimerFill is within Update. I’m not getting any errors with what I currently have (shown) but when I watch the character’s health after I press the defend button it only goes up by a very marginal degree (like .001) so I think the tDefendCommand function only works for a single frame or something.

tDefendCommand(); is only run on the frame you press the button, It doesn’t sound like that is the behavior you want. Maybe you want something like this:

var bool tDoDefend = false;
...
OnGUI() {
   ...
    if (tdButton == true){
        tDoDefend = true;
        }
  ...
Update() {
 if (tDoDefend) tDefendCommand();
 ...

Thanks a bunch! the extra bool variable almost completely fixed it.

The working button looks like this;

var tdButton = GUI.Button(Rect(5, 45, 50,20), "Defend");
		if (tdButton == true){
		tDoDefend = true;
		tAbilitieswinswitch = false;
		tTimer = 1;
		}

The only problem now is that the nested if doesn’t work. Is it because I already have a function using tTimer and tSpeed similarly? Thanks for your help though.

function tDefendCommand(){

	if (tTimer >= 1   tTimer < tSpeed){
		Debug.Log( "Defend button is defending");        
		tHP += 1 * Time.deltaTime;
    
		if (tTimer >= tSpeed){
        
			Debug.Log("Defend button is done defending");
			
			tDoDefend = false;
        }	
	}
		
}

Check i tTimer is increasing on each frame. You may have broken it somehow. I find this kind of Debug line useful:
Debug.Log( "Defend button is defending at "+tTimer);

tTimer is definately increasing with each frame and stopping at tSpeed according to debug but tDoDefend never gets switched back to false.

Well I replaced the nested if with an else and that seemed to fix it. I don’t understand why though. If anybody out there understands the problem would you care to explain it please? thanks!

tTImer must be less than tSpeed to get here.

so then how could it ever be greater then tSpeed here?

The inner if checks for a condition which is the opposite of the outer if. It will never trigger.

I get it now. Thanks for the help guys! Much appreciated.