Help with variables and objects

Started by Atrocious Toaster, Thu 02/07/2009 06:31:36

Previous topic - Next topic

Atrocious Toaster

I want it so that if you try to pick up, let's say Bottle, and you haven't finished some puzzle, you are refrained from touching it. But If you have, you are able to. I tried using this sequence:

function Booze_PickUp()
{
if (GetGlobalInt(1)=0)then
cBum.Say ("Hey, fella! Save that for 21! That's the hard stuff! Suprising enough to find a kid in juvie anyways!");
else
cBum.Say ("Well, my friend told me about it, so keep it!");
}

My guess: I'M WAY OFF!

Ghost

#1
If I read your code correctly, the error is the "=".

= means "set some variable to some value"
== means "compare to a value

So x = 2 sets x to 2.
if (x == 2) returns true if x has a value of 2.


If you are using AGS3.x you can simply make your own custom GLOBAL VARIABLE. They are much easier to handle than the (almost obsolete) GetGlobalInt stuff.

A boolean is usually the easiest type if you need a simple switch.

So:
Create a new global variable, say gvCanTakeBooze, type boolean, default value false.

Then, in the booze's pickup section:

if (gvCanTakeBooze)
{
 player.addInventory(iBooze);
 player.say("I now have booze.");
}
else
{
 player.say("Not yet.");
}

And in order to ALLOW the player to take the wretched licker, just set the "switch":

gvCanTakeBoooze = true;

Presto!

Edit:
We really need more stickies. Three people coming up with almost the same answer. Must be some sort of record  :=

Gilbert

(Ghost beat me, but I'll post anyway.)
First, there is not "then" keyword so it's not needed. Second, to compare values you need double equality sign '==' instead of a single '=' which is used for assigning values.

So:
Code: ags

function Booze_PickUp()
{
  if (GetGlobalInt(1)==0)
     cBum.Say ("Hey, fella! Save that for 21! That's the hard stuff! Suprising enough to find a kid in juvie anyways!");
  else
    cBum.Say ("Well, my friend told me about it, so keep it!");
}


Also, since you're still using the SetGlobalInt() and GetGlobalInt() route I know some people may insist to tell you that it is an obsolete way to do stuff, as they're replaced by the global variable pane in the editor, but personally I don't see any problem to use them if you prefer.

TerranRich

If you're using AGS 3.0+ (or, if not, you ever do in the future), check out the built-in global variables... no need to keep track of numbers; they're all named in the editor. :)
Status: Trying to come up with some ideas...

SMF spam blocked by CleanTalk