Game Development Community

Checking a inventory variable for true or false

by Jonas · in Technical Issues · 07/16/2010 (9:25 am) · 7 replies

Hey community!

I have been working on a door for a couple of days and im getting close to finishing it (if you are trying to make a door yourself try "Creating a door trigger" by justin knight).

The door i am creating is locked and there for it needs you to have a key with you in your inventory
to open this door, but i can't figure out how to check for the value of that particular variable i keep coming up with strange results.

The door is governed by 2 triggers one on either side. The one on the back side works fine its the "key" side that i cant figure out.

This is what i got so far:
--------------CODE------------------

function DoorTrigger::onEnterTrigger( %this, %trigger, %obj)
{
if(Map01Key01 == 1)//)
{
echo("You opened the door with the key and left it there!");
%trigger.door.playThread( 0, "open");
%obj.decInventory(%this.Map01Key01, 1);
}
else
{
echo("You do not have the key!");
}
}
--------------CODE------------------

A note on the side its about 10 in the morning here and i haven't slept yet so that might contribute to total crappy code/wrong way to do it.

PS: i filed this under technical since the jist of the issue is how to check a variable and then act on that.

Best regards
/Jonas

About the author

Freelance 3D artist at day scripter/coder on Enduring Life at night. Lover of all things TorqueScript.


#1
07/16/2010 (5:15 pm)
I'm not positive, but based upon you "decInventory" line, do you mean for the if-statement to also check for "%this.Map01Key01"?

if( %this.Map01Key01 == 1 ) ...
#2
07/16/2010 (6:00 pm)
Make sure that you're player is allowed a key in their inventory.
maxInv[Map01Key01] = 1;

Get the Player -> Check the inventory -> for the key -> amount of keys
-> if amount of keys are not zero -> do stuff
if(%obj.getInventory.Map01Key01 != 0)

As William noted, you are destroying the key once you've used it ... right?
%obj.decInventory(Map01Key01, 1);
#3
07/16/2010 (10:39 pm)
Hey

i have updated my code with your help but im still coming up dry...

Now it recognizes the object Map01Key01 and i no longer get the 'getname' function error to find it, However when i walk through the door it always say that i do not have the key even if i do so ill fill you in a bit more and lets see what we can do shall we?

function DoorTrigger::onEnterTrigger( %this, %trigger, %obj)
{
   if(%obj.getInventory.Map01Key01 != 0)  
   {
      echo("You opened the door with the key and left it there!");
      %trigger.door.playThread( 0, "open");
      %obj.decInventory(Map01Key01, 1); 
   }
   else
   {
      echo("You do not have the key!");
   }
}

the inventory maxInv is present yes but i wonder if i have missed any file i need in the server directory? when i glace over it i see that all objects do have a /scripts/server script as well.

maxInv[Map01Key01] = 1;

Best regards
/Jonas
#4
07/17/2010 (12:34 am)
Apologies if you already know this and this isn't the problem ...

maxInv(entory) is not the same as actually having the object ... it's just to say that you can pick it up, and then gives the number of how many you can hold.

I know ... it does seem a bit odd at first ... but it has it's uses.

So you need to give the player the object - either spawn it and run into it to pick it up (make sure the model has a boundingbox!), or add it at spawn.

Gamecore.cs -> ::loadout -> %player.setInventory(Map01Key01,1);

or add it through the console using the player's name or ID.
#5
07/17/2010 (1:10 am)
Ye i do have the object created it just eludes me why it won't react to it.

This is how i got it all setup:

datablocks/Misc/SPkeys.cs
NOTE: the object itself 'rocket.dts' is there for testing purposes.
datablock ItemData(Map01Key01)
{
   // Mission editor category
   category = "Misc";

   className = "Misc";

   // Basic Item properties
   shapeFile = "art/shapes/weapons/SwarmGun/rocket.dts";
   mass = 2;
   elasticity = 0.2;
   friction = 0.6;

   // Dynamic properties defined by the scripts
   pickUpName = "Map01Key01";
   maxInventory = 1;
};

datablocks/player.cs
maxInv[Map01Key01] = 1;

scripts/server/door.cs
function DoorTrigger::onEnterTrigger( %this, %trigger, %obj)
{
   if(%obj.getInventory.Map01Key01 != 0)  
   {
      echo("You opened the door with the key and left it there!");
      %trigger.door.playThread( 0, "open");
      %obj.decInventory(Map01Key01, 1); 
   }
   else
   {
      echo("You do not have the key!");
   }
}

i tested adding the item via gamecore.cs but the door still won't react to it and still tells me "you don't have the key" also when i place the key in the level and run over it i get the msg that i picked it up but the result still wont change.

I cant stop feel that im missing something in the object datablock thing or any other part related to the object since it seems that is the problem.

best regards.
#6
07/17/2010 (9:42 am)
Difference between field access and function calling:
if(%obj.getInventory.Map01Key01 != 0)
Should be
if(%obj.getInventory(Map01Key01))

#7
07/17/2010 (10:01 am)
Thank you Michael! it works now!

Thanks to everyone who helped out ! im thinking of making a complete step by step lots of images tutorial to making and setting up a door since i see many new creators (including myself) struggling with this, so if you got a opinion on that idea like good or bad feel free to drop a word.

Best regards and thanks again!