Help using variables

Started by matteo, Thu 22/11/2007 14:24:05

Previous topic - Next topic

matteo

first of all.sorry if my english is a little rusty,i'm from italy.
i have start with ags only 3 day ago..be patient please.
this is my problem: i create a item,is a box
when the main cha interact with it,the box open up.
i want that the game display a message only when the box is open.
i tried to use conditional with a variable,but i guess i had to use script and even if i check the manual,i had big problems.
hope you can help me,just to learn and dont ask no more.
thanks

Ashen

What have you tried, and what's gone wrong? If you don't give us any details, you'll only get very basic answers that might not cover the problem you're having.

*This recent thread shows how to do conditional scripting. (See monkey_05_06's post for a working example).
* The BFAQ has more details on working with variables.

If one of them solves your problem, remember you should be reading the BFAQ and searching the forums, as well as checking the manual, before you post in the future (the thread I linked is still on the first page of the forum - not too hard to find). If they're no help, then like I said: More details about what you've tried and what the problem is, please.
I know what you're thinking ... Don't think that.

Khris

#2
I strongly encourage you to ditch the Interaction Editor and start learning the script language right away. (The IE isn't a part of AGS anymore as of the latest beta anyway.)

Regarding your problem:
It's generally wiser not to use variables until you have to.
In your case, I assume the box is an object and will change its sprite when it's opened.
So it's enough to check the current sprite of the box to determine which message is appropriate.

Code: ags
//interact with box
  if (oBox.Graphic==21) oBox.Graphic=22;
  else oBox.Graphic=21;

This snippet reads the current sprite of the box-object and changes it.
You'd have to put the actual sprite slots in there instead of 21 and 22, of course.

The other thing you have to do is assign a scriptname to the box; in "Room Editor -> Objects", select the box and put the scriptname into the right textbox.
I choose to name it "BOX", this allows me to reference the box in the script using "oBox".

To display a message only when the box is open, again test oBox.Graphic against the sprite slot number of the open box:
Code: ags
//look at box
  if (oBox.Graphic==21) player.Say("The box is closed.");
  else player.Say("The box is open.");

matteo

khrisMuc,you got what my problem was.
thank you very much,or as we say here in italy:grazie

matteo

finally, i made it in this way: // script for Object 2 (box): Interact object
  //look at box
if (oBox.Animate(7,0)) player.Say("box is open.");

so,after the animation that open the box the player saw it open,if he look it before,i use the intaraction editor :look at the object--->game display a message.

dunno if i am acting right,but i'm inproving myself a little.
thanks anyway

Khris

I wouldn't recommend using
if (oBox.Animate(7,0)) player.Say("box is open.");
The ()-brackets after if are supposed to contain some condition, like player.x < 150 or something like that.
If there isn't some (in)equation in there, the value of the expression is compared to zero.

I have no idea what the return value of the Animate function is, but I don't have to because using it like that is wrong.

I'm afraid right now, if you interacted again with the open box, the player will open the box all over again.

Note that there are two fundamentally different commands for most items:
- Functions, which are used to change something, move something or alter the game in any other way, like player.Walk or oBox.Animate.
- Properties, which hold a value of some kind, used to get the current state of something, like player.x which returns the x-coordinate of the player.

Usually only the second kind ever goes into an if-condition's () brackets.

Use this:
Code: ags
// script for Object 2 (box): Interact object
  oBox.SetView(4);                      // replace 4 with the number of the correct view
  if (oBox.Graphic==20) {    // replace 20 with the sprite of the closed box
    oBox.Animate(7, 0);        // note that the 7 is the number of the loop, not the view!
    player.Say("The box is open now.");
  }
  else {
    oBox.Animate(7, 0, eBackwards);
    player.Say("I've closed the box.");
  }


Also note that using only the "Game - Display a message" action will always display the given message, regardless if the box is open or closed.

SMF spam blocked by CleanTalk