Trying to set some variables

Started by Hobo Joe, Fri 05/09/2008 20:37:58

Previous topic - Next topic

Hobo Joe

Basically, what I'm trying to do is set it up so that if 2 particular graphical variables are less than or equal to 5, a third variable will be set to greater than or equal to 5. Having troubles. Here's my script:

SetGraphicalVariable("happy", Random(10));
SetGraphicalVariable("sad", Random(10));
SetGraphicalVariable("angry", Random(10));
if (GetGraphicalVariable("happy")<=6)
if (GetGraphicalVariable("sad")<=6){
SetGraphicalVariable("angry", (>=(5)));
}

I'm getting this error:

GlobalScript.asc(61): Error (line 61): Parse error: unexpected operator '>='

Obviously it doesn't like the >=, but I don't know what better way to put it in context or what else to try if it doesn't work. Help would be much appreciated.


EDIT: GAH! Wrong forum. ::)
Yay, I'm Hobo..... Joe...

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Hobo Joe

#2
Okay... now it says

GlobalScript.asc(61): Error (line 61): read/write ax called with non-variable parameter ('=')


The script looks like this now

  SetGraphicalVariable("happy", Random(10));
  SetGraphicalVariable("sad", Random(10));
  SetGraphicalVariable("angry", Random(10));
  if (GetGraphicalVariable("happy")<=5)
  if (GetGraphicalVariable("sad")<=5){
  SetGraphicalVariable("angry", =>(5));
}

Also, is that the proper way to set it up so that if BOTH happy and sad <=5 then it changes, or will only one of them affect it?
Yay, I'm Hobo..... Joe...

Khris

First of all, it's indeed ">=", not "=>".
Secondly, this is for testing a condition (is A greater than or equal to B), not setting a variable.

You'd use
Code: ags
  if (GetGraphicalVariable("happy")<=5 && GetGraphicalVariable("sad")<=5) {
    SetGraphicalVariable("angry", 5);
  }


Note the AND operator (&&) connecting the two conditions into one.
What exactly are you trying to set "angry" to? Some random value greater than 5?
Use
Code: ags
  SetGraphicalVariable("angry", Random(5)+5);

to set it to 5-10.

Hobo Joe

Okay. That works, thanks. However, one more problem, then I'll be out of your hair.

When I run it now, it starts to boot, crashes, then says interaction variable "happy" not found. How do I create variables in script? Never could figure that out.
Yay, I'm Hobo..... Joe...

Dualnames

put this
int happy;//by default it gets 0 value if you want it to have another value do this
//int happy=5;
above the function

or add a variable at global variables section above scripts
select integer, name it happy and add a default value.

If you add a variable at the global variables, you can access it at any script of your game(modules,global,room).

Also Khris, I edited my response when I realised i was actually not helping..but for a reason it didn't change. And i noticed i gave wrong info. Just letting you know.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Hobo Joe

I couldn't figure out where the global variables above the script you were talking about were, so I've set it up like so:

  int happy;
  int sad;
  int angry;
  SetGlobalInt (happy, Random(10));
  SetGlobalInt (sad, Random(10));
  SetGlobalInt (angry, Random(10));
  if (GetGlobalInt (happy) <=5 && GetGlobalInt(sad)<=5){
  SetGlobalInt (angry, Random(5)+5);
  }
  if (GetGlobalInt (sad) <=5 && GetGlobalInt(angry)<=5){
  SetGlobalInt (happy, Random(5)+5);
  }
  if (GetGlobalInt (angry) <=5 && GetGlobalInt(happy)<=5){
  SetGlobalInt (sad, Random(5)+5);
  }
  if (GetGlobalInt (angry) ==0){
  SetGlobalInt (angry, (1));
  }
  if (GetGlobalInt (happy) ==0){
  SetGlobalInt (happy, (1));
  }
  if (GetGlobalInt (sad) ==0){
  SetGlobalInt (sad, (1));
  }

I added a thing to try to prevent them from being zero at the end because I don't know of a more efficient way to do it just yet. Once I set up a whole crapload of these, we'll see if any of this affects any gameplay! Thanks for the help, guys.
Yay, I'm Hobo..... Joe...

Hobo Joe

Okay, new problemish thing. Here's my script: (it's kind of long)

  int thappy;
  int tsad;
  int tangry;
  SetGlobalInt (thappy, Random(10));
  SetGlobalInt (tsad, Random(10));
  SetGlobalInt (tangry, Random(10));
  if (GetGlobalInt (thappy) <=5 && GetGlobalInt(tsad)<=5){
  SetGlobalInt (tangry, Random(5)+5);
  }
  if (GetGlobalInt (tsad) <=5 && GetGlobalInt(tangry)<=5){
  SetGlobalInt (thappy, Random(5)+5);
  }
  if (GetGlobalInt (tangry) <=5 && GetGlobalInt(thappy)<=5){
  SetGlobalInt (tsad, Random(5)+5);
  }
  if (GetGlobalInt (tangry) ==0){
  SetGlobalInt (tangry, Random(10));
  }
  if (GetGlobalInt (thappy) ==0){
  SetGlobalInt (thappy, Random(10));
  }
  if (GetGlobalInt (tsad) ==0){
  SetGlobalInt (tsad, Random(10));
  }
 
    if (GetGlobalInt (thappy)==1){
  Display("Happiness equals one.");
  }
  if (GetGlobalInt (thappy)==2){
  Display("Happiness equals two.");
  }
  if (GetGlobalInt (thappy)==3){
  Display("Happiness equals three.");
  }
  if (GetGlobalInt (thappy)==4){
  Display("Happiness equals four.");
  }
  if (GetGlobalInt (thappy)==5){
  Display("Happiness equals five.");
  }
  if (GetGlobalInt (thappy)==6){
  Display("Happiness equals six.");
  }
  if (GetGlobalInt (thappy)==7){
  Display("Happiness equals seven.");
  }
  if (GetGlobalInt (thappy)==8){
  Display("Happiness equals eight.");
  }
  if (GetGlobalInt (thappy)==9){
  Display("Happiness equals nine.");
  }
  if (GetGlobalInt (thappy)==10){
  Display("Happiness equals ten.");
  }
 
  if (GetGlobalInt (tsad)==1){
  Display("Sadness equals one.");
  }
  if (GetGlobalInt (tsad)==2){
  Display("Sadness equals two.");
  }
  if (GetGlobalInt (tsad)==3){
  Display("Sadness equals three.");
  }
  if (GetGlobalInt (tsad)==4){
  Display("Sadness equals four.");
  }
  if (GetGlobalInt (tsad)==5){
  Display("Sadness equals five.");
  }
  if (GetGlobalInt (tsad)==6){
  Display("Sadness equals six.");
  }
  if (GetGlobalInt (tsad)==7){
  Display("Sadness equals seven.");
  }
  if (GetGlobalInt (tsad)==8){
  Display("Sadness equals eight.");
  }
  if (GetGlobalInt (tsad)==9){
  Display("Sadness equals nine.");
  }
  if (GetGlobalInt (tsad)==10){
  Display("Sadness equals ten.");
  }


The problem is that every time both numbers are the same. If "Happiness" is 10, so is sadness, and so forth. What am I doing wrong?
Yay, I'm Hobo..... Joe...

monkey0506

#8
The problem is that thappy, tsad, and tangry all hold the same value: 0 (zero). So you are only accessing GlobalInt 0.

You have some alternatives here:

1. Leaving your scripts in-tact and as-is, all you would have to do is assign different values to your different variables:
int thappy = 1;
int tsad = 2;
int tangry = 3;
2. *RECOMMENDED* Use the Global Variables pane. As noted by Dualnames, this should appear directly above Scripts in your main game pane (General Settings, Colours, Sprites, Text Parser, etc.). If you don't see this, you're using an older version of AGS which doesn't support this method. Either select a different alternative, or update your version of AGS. To use this method, simply read the manual entry on "Global variables" which will direct you on how to properly create and use global variables using the Global Variables editor.

3. *Most scripting-oriented* This method is the most scripting-oriented with AGS's current scripting methods. To use this method you must export your variables from the script, and import them into the header:

Code: ags
// global script (GlobalScript.asc)
int thappy;
int tsad;
int tangry;
export thappy;
export tsad;
export tangry;

// global script header (GlobalScript.ash)
import int thappy;
import int tsad;
import int tangry;

// from the global or any room script (must be within a function)
thappy = 15; // set thappy's value
if (thappy == 27) { // get thappy's value
  // blah
}


If you want to be able to access these variables from scripts other than the global and room scripts, simply place the variable definitions and exports (the lines such as "int thappy;" and "export thappy;") in the proper script file and the imports ("import int thappy;") in the appropriate script header of a script prior to (above) the script you wish to use the variables in.

The second alternative is recommended because the Global Variables pane has been introduced as a method of deprecating the GlobalInt functions, so it is better to use the newer method. The third alternative is still valid, but is slightly more complex than the second. There's no difference between the second and third except preference really, aside from the fact that variables defined in the Global Variables pane will always be accessible from all of your scripts whereas if you define the variables yourself in the script, they must be in a script prior to the script you wish to use them in (or in the same script).

Edit: Also, I know you're using at least AGS 3.0 due to the error message (GlobalScript.asc), but IIRC the Global Variables pane wasn't implemented until 3.0.1 or 3.0.2...just FYI. ;)

Pumaman

Yes, the important point here is that you should NOT be using SetGlobalInt or SetGraphicalVariable.

Create variables called "thappy", "tsad" and "tangry" in the Global Variables editor (assuming you have AGS 3.0.2), and then in the script you just do:

thappy = Random(10);
tsad = Random(10);
tangry = Random(10);

if (thappy <=5 && tsad<=5){
  tangry = Random(5)+5;
}

Just remember that Set/GetGlobalInt and Set/GetGraphicalVariable are obsolete and provided only for backwards compatibility with older versions of AGS.

Dualnames

There's no reason at least if you've used 3.00 and later why not upgrade to any future versions.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Hobo Joe

I guess that's my problem, then. I have 3.0.1. Time to upgrade!
Yay, I'm Hobo..... Joe...

Dualnames

Quote from: Hobo Joe on Sun 07/09/2008 17:06:06
I guess that's my problem, then. I have 3.0.1. Time to upgrade!

You can stick to 3.0.1 we will still help you, but Chris is putting more user-friendly features in every version that's there;s really no reason at all to upgrade.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Hobo Joe

I fixed the whole thing. It all sets up properly, but now I have this problem:

function repeatedly_execute() {
  // put anything you want to happen every game cycle here

  if (hungry > pee){
    cEgo.Walk(74, 199, eBlock, eWalkableAreas);
    cEgo.FaceLocation(74, 160, eBlock);
  }
  else if (pee > hungry){
    cEgo.Walk(301, 199, eBlock, eWalkableAreas);
  }
 
}


I want it to be eNoBlock, but that just makes him twitch in place. The problem is that I want this all to happen sort of randomly. I'm trying to set up a series of statistics that the guy will act on, on his own, but the problem is that other than putting it in repeatedly_execute, I don't know how to do this, so he just tries to do it endlessly.

Is there a way to make it check the stats, decide an action, do it, then check again when he's done and so forth?
Yay, I'm Hobo..... Joe...

monkey0506

#14
In the first instance it's understandable why he might twitch without blocking. FaceLocation is presumably forcing Ego to stop before making him turn to the specified location.

For something like this you might want to do something such as:

Code: ags
bool haswalked = false;

function repeatedly_execute() {
  // put anything you want to happen every game cycle here

  if (hungy > pee) {
    if (!haswalked) { // this is equivalent to "haswalked == false"
      cEgo.Walk(74, 199, eNoBlock, eWalkableAreas);
      haswalked = true;
    }
    else { // haswalked == true
      if (!cEgo.Moving) { // Ego has stopped
        cEgo.FaceLocation(74, 160, eNoBlock);
        haswalked = false;
        // you should probably also reset "hungry" here
      }
      // else Ego hasn't gotten there yet, so we don't want to do anything
    }
  }
  else if (pee > hungry) {
    cEgo.Walk(301, 199, eNoBlock, eWalkableAreas);
    // reset "pee" here
  }
}


Actually, I've just read the end of your post...::)...and I think there might be a better solution for your problem. Do you need to store things like hungry and pee for use in other scripts? If you don't then there's no need to use global variables at all. The only time you should use global variables is if you need to access the variables from multiple scripts.

That said, if you don't need global variables, then something like this might interest you. You could use an enum to store the different possible actions, and then use the Random function to pick one...

Code: ags
enum Actions {
  eActionNone = 0,
  eActionHungry,
  eActionPee,
  eActionTelevision,
  // etc. you get the idea
  eActionMax // we can use this as a sort of "last action" counter to let us know how many actions we have; it won't actually be used
}; // make sure you put a semi-colon after this brace!

Actions EgoAction = eActionNone; // we'll use this to track Ego's current action
bool hasWalked = false;

function repeatedly_execute() {
  if (EgoAction == eActionNone) { // Ego's not doing anything!
    int ran = Random(5); // Random returns 0-X inclusive (0-5 in this case), so we've got a 1/6 chance of getting a specific value
    if (ran == 1) { // 1/6 chance he'll do something if he wasn't busy
      EgoAction = Random(eActionMax - 2) + 1;
      // eActionMax - 2 prevents using eActionNone or eActionMax as the action (offsetting it by 1 since eActionNone is 0)
    }
  }
  if (EgoAction == eActionHungry) {
    if (!hasWalked) { // this is equivalent to "haswalked == false"
      cEgo.Walk(74, 199, eNoBlock, eWalkableAreas);
      hasWalked = true;
    }
    else { // hasWalked == true
      if (!cEgo.Moving) { // Ego has stopped
        cEgo.FaceLocation(74, 160, eNoBlock);
        hasWalked = false;
        EgoAction = eActionNone;
      }
      // else Ego hasn't gotten there yet, so we don't want to do anything
    }
  }
  if (EgoAction == eActionPee) {
    if (!hasWalked) { // do this to make sure we don't send Ego somewhere else before he gets where he was going!
      cEgo.Walk(301, 199, eNoBlock, eWalkableAreas);
      hasWalked = true;
    }
    else {
      if (!cEgo.Moving) {
        EgoAction = eActionNone;
      }
    }
  }
}

Hobo Joe

Hey, that's great. I can expand off that and hopefully figure out how to incorporate it doing things that aren't completely random. I'm trying to set him up so he has a bunch of needs to fulfill, sort of a semi AIish thing, and chooses what to do based on his stats. This is a huge help. Thank you very much!
Yay, I'm Hobo..... Joe...

TerranRich

Also, what if the need to pee is the same level as the hunger (hunger == pee)? Nothing happens in that case. Is that intentional?
Status: Trying to come up with some ideas...

Hobo Joe

Looking over it, I'm not sure enum is what I need. I need to keep track of a number of hidden statistics and then use them as a basis for what the "AI" will do. I'll keep working on it.
Yay, I'm Hobo..... Joe...

SMF spam blocked by CleanTalk