Script error

Started by simulacra, Sun 12/06/2005 00:28:32

Previous topic - Next topic

simulacra

What is wrong here? I get a type mismatch error.

      if(character[ME].ActiveInventory==1) {
        // Code
      }

monkey0506

I believe:

if (character[ME].ActiveInventory.ID == 1) {
  /* Code */
  }

Use the ID of the ActiveInventory, as ActiveInventory is a Pointer to an Inventory Item (InventoryItem*). ;)

simulacra

What is the ID of the ActiveInventory? I am confused by the 2.7 novelties.

Ashen

I too am confused by the 2.7 conventions.
As I understand it, however, character[ME].ActiveInventory returns the 'Script-O-Name' of the item (e.g. iKey). character[ME].ActiveInventory.ID returns the item number, and so can be used in if (... == 1) conditions.

Incidentialy, I think you can subsitute character[ME]. with cMe. (the 'Script-O-Name' of the character), e.g. cMe.ActiveInventory.ID. (I may be wrong, however - I don't use 2.7 much, yet.)
I know what you're thinking ... Don't think that.

monkey0506

#4
character[ME].ActiveInventory is a pointer to an InventoryItem.  This means that it simply points to an inventory item (the active one).  Which in turn means that you can perform operations on it, just like you would an InventoryItem.

So, you can do things like this (I'm going to use the o-name short-hand in place of character[ME]):

cMe.ActiveInventory.GetAtScreenXY(mouse.x, mouse.y);
string active_inv_name;
cMe.ActiveInventory.GetName(active_inv_name);
string property_name;
cMe.ActiveInventory.GetProperty(property_name);
string active_inv_property_text;
cMe.ActiveInventory.GetPropertyText(property_name, active_inv_property_text);
cMe.ActiveInventory.IsInteractionAvailable(eModeLookat);
cMe.ActiveInventory.RunInteraction(eModeLookat);
cMe.ActiveInventory.SetName("New inventory!");
cMe.ActiveInventory.Graphic = 47;
if (cMe.ActiveInventory.ID == 1) { /* do something */ }

I put buffer strings in italics just for an example.  Also, you could CHANGE the active inventory with a statement like this:

cMe.ActiveInventory = iKey;

Where iKey is the o-name of the inventory item "Key".

In case you're now even more confused, let my try to help:

1.  O-Names:  These are just short-hand methods of writing some things in 2.7:

character[EGO] = cEgo
object[DOOR] = oDoor
hotspot[TREE] = hTree
gui[INVENTORY] = gInventory
inventory[PINKPOSTER] = iPinkPoster

For some of these the o-name is defined for you, some you can define them (such as GUI controls).

2.  Pointers:  A pointer is a special type of variable which "points" to a variable of it's defined type.  It is kind of like a buffer variable; it allows you to work with several different variables at different times.  An example would be this:

InventoryItem* ActiveInventory

ActiveInventory is of the type "pointer to InventoryItem".  This means that it points at an InventoryItem object, in this case, the active InventoryItem.  By pointing to it, you don't have to know what the active inventory is.  Without it you would have to use some sort of integral means to determine the active inventory item based upon its number.  But instead, by using a pointer, you can just define it AS the InventoryItem that is active.  This is what makes lines like the following possible:

cMe.ActiveInventory = iPinkPoster;

Without it you would have to do something like:

cMe.ActiveInventory = 47;

Or:

cMe.ActiveInventory = iPinkPoster.ID;

And then you wouldn't be able to operate directly upon the active inventory (such as the following example does):

cMe.ActiveInventory.Graphic = 83;

This operates upon the active InventoryItem, changing its Graphic.  Without pointers you would have to do something like:

inventory[cMe.ActiveInventory].Graphic = 83;

Also, pointers can point to nothing.  In this case they are defined as "null".  You can even force the pointer to become null with a statement like:

cMe.ActiveInventory = null;

Then you can no longer operate upon the active InventoryItem because ActiveInventory points to nothing, there is no active InventoryItem.

And, in case you're still curious about all this "*.ID" business, with the new OO style, it's obvious why things like pointers would be the obvious choice (as in the example with ActiveInventory), so you loose the option of operating on the active inventory as an integral number, so instead, you have to access it's ID.  The ID is just the integral value of an object.  Many of the objects in 2.7 have an ID to identify them, such as inventory items and GUI controls.  This can help in using legacy style code (2.62 style code) as well as other things.

I hope this helps you to understand 2.7 a little better.  Also, in case you haven't, I would suggest reading in the manual under "Upgrading to 2.7".  It's a lot more thorough (and probably understandable) than I am. :)

Edit:  Also, by the way, I believe that if you defined an O-name for your inventory object #1 (i.e. if inventory #1 is a blue cup, you may have named it iBlueCup), then you could just test the ActiveInventory against the o-name, such as:

if (cMe.ActiveInventory == iBlueCup) {
  /* code */
  }

Another novelty offered by 2.7. ;)

SMF spam blocked by CleanTalk