Can I use a GUI slider to display a global variable?

Started by Axelord1942, Fri 02/02/2018 16:51:53

Previous topic - Next topic

Axelord1942

Working on a project and I'm trying to implement a stress/sanity level, and I would like this to be reflected with a slider on the either on the action gui or elsewhere that would raise and lower respectively...is this as simple as I think it might be (just using a call//check/display function?) or am I off the mark?  First post, long-time lurker so please forgive me if this isn't in the right place after all.(laugh)
You have been eaten by a Gru

Cassiebsg

Hope this helps: http://www.adventuregamestudio.co.uk/forums/index.php?topic=51544.msg636504538#msg636504538

There are more threads about "health bars"... Principle is the same, just the name that changes. (laugh)
There are those who believe that life here began out there...

Axelord1942

Thank you!  I searched around the forums but I guess I wasn't using the right terms, I'll read through all of this right away!  Thanks again.
You have been eaten by a Gru

Khris

Let me just point out that a slider is for input, not output. :-D

Axelord1942

Yeah, I figured that out the hard way and switched to a button.  That project, since it really matters to me, is on hold until my scripting gets better.  Working on a less important one until i get more comfortable with it.
You have been eaten by a Gru

Egmundo Huevoz

#5
Hey, man. A couple of months ago, I was in your shoes. But the good people on this forums helped so much, that now I understand a lot of things (still a noob, but not so much anymore :-D ). Sooo I'm confortable enough to try and help you out.
What you want is simple enough, I think. You want a stress/sanity level. Okay, your character becomes crazier and crazier when he does some things, right? We can just track this with a variable. If you're gonna have more "stats" to your character/s (like, I don't know, luck, strength, or something you'd see in an RPG), you probably should use structs, arrays, and/or extender functions but, for now, if you don't need this, I'll skip them. If you DO need them, let me know and I'll explani that.
I'm gonna assume you use stress and sanity as 2 extremes of the same variable. First you create global variable, named "stress", or whatever you want. We'll leave the default value to 0. Then, whenever your character does something that makes him more or less stressful/sane/crazy you just do this:

Code: ags

//you can use these anywhere in your code.
stress ++; //this increases stress by 1
stress --; //this decreases stress by 1
stress += 10 //this increases stress by 10, you can replace that number with any number you want.
stress -= 10 //the same but decreasing stress


Also you'll want to set your stress limits. In my example I made them 0 and 100, but you can do whatever you want.

Code: ags

//in repeatedly_execute
if (stress<0){ //so you can't have negative stress.
    stress = 0;
}
if (stress>100){
    stress = 100;
    //or lose the game, die, or whatever you want.
}


Then, to change events depending on how much stress your character has, do something like:
Code: ags

cBoss.say ("You're fired.");
if (stress < 25){
player.say("It's okay, I'll find another job soon enough.");
}
else if (stress < 50){
player.say("Oh yeah? Up yours, old man!");
}
else{
Display ("You grab your chair and proceed to bludgeon your boss to death.");
}



You'll want to know how much stress your character has. You can do this in several ways:

1) By displaying it in certain circumstances (by clicking a button, for instance):
Code: ags

Display ("Your character has %d amount of stress!", stress); //%d displays the value of an int, in this case stress.


2) By displaying it on a GUI's label. Create a GUI for displaying you stress, and there create a label called "lblStress".
Code: ags

//in repeatedly_execute
String StressDisplay=String.Format("Stress: %d", stress);
lblStress.Text=StressDisplay;


3) Like you wanted, by using a "stress bar". I think you should combine this with method 2. I've made this a long time ago, only once, so I don't know if it's the easier method, but it works. Make a sprite of a rectangle, and make it 100 px wide (that would be our max, but you can make it whatever you want). Then make a button on your stress GUI, called btnStressBar. Use your rectangle sprite as your button's sprite. In the button's properties, make "clip image" true.
Code: ags

//in repeatedly_execute
btnStressBar.Width=stress;



If something doesn't work, you don't understand something, or need anything else, don't hesitate to ask.




Axelord1942

This is excellent.  This template is going to help me IMMENSELY in understanding how this actually works.  The examples given in the in-program help menu leave a little to be desired (roll)

Once I get the time today I'm gonna boot up my prototype and start plugging away and see what kind of lemonade I can make.

Yeah, you pretty much nailed it though.  All I really need is the stress/sanity variable (trying to keep the game mechanics as simple as possible).
Quote from: Egmundo Huevoz on Wed 07/02/2018 18:50:37
Hey, man. A couple of months ago, I was in your shoes. But the good people on this forums helped so much, that now I understand a lot of things (still a noob, but not so much anymore :-D ). Sooo I'm confortable enough to try and help you out.
What you want is simple enough, I think. You want a stress/sanity level. Okay, your character becomes crazier and crazier when he does some things, right? 

Coincidentally, have you played Darkest Dungeon? :-D
You have been eaten by a Gru

Khris

One can combine all that into a single function btw:

Code: ags
// global header
import void AddStress(int amount);
import int stress, max_stress;

// global script
int stress, max_stress = 100;
export stress, max_stress;

void AddStress(int amount) {
  stress += amount;
  if (stress < 0) stress = 0;
  if (stress > max_stress) stress = max_stress;
  lblStress.Text = String.Format("Stress: %d", stress);
  btnStress.Width = stress;
  Wait(1); // wait for screen repaint
  // if (stress == max_stress) Die();  // uncomment to use
}


That way we only update the label and button when stress actually changes.

Code: ags
  AddStress(10);

  // or
  AddStress(-5);
  Display("You calm down a bit.");

Axelord1942

I really appreciate this input!  This is extremely insightful, and helps me put this stuff in the proper context!  I'll make sure to post results!

;)
You have been eaten by a Gru

SMF spam blocked by CleanTalk