(AGS 2.72) Refer to inventory items by name or number?

Started by Wellington, Thu 28/02/2008 03:14:42

Previous topic - Next topic

Wellington

Is there anything like an "AGS Best Practices" document for coding novices that suggests ways to avoid shooting oneself in the foot? I've just run into an issue.

When I edit the interactions for a region in AGS 2.72, and I want a response to an attempt to use an inventory item on an object, I can either use "Conditional - If Inventory Item Was Used" and pick an Inventory ID number, or manually script in something like this:

Code: ags
String nameofused = player.ActiveInventory.Name;
if ((nameofused == "Flint") || (nameofused == "Torch")) {
	Display("Sliding down a flaming railing would be a neat trick, but the thing just won't light.");

(.. etc...)


The problem is, neither solution seems ideal. The use of ID numbers means that any renumbering of the inventory will turn my code into a huge mess, but using a script means that I have do dive into the scripts to edit messages. Is there a smarter solution that I'm missing?

Gilbert

I think there's no better solution, as if you want to refer objects/inventory items/characters/etc. by script names you need to use the text script.

One more point is, in AGS V3 the interaction editor is now gone, so unless you're not going to upgrade in the future, you need to be familiar with scripting one day.

OneDollar

Quote from: Wellington on Thu 28/02/2008 03:14:42
The use of ID numbers means that any renumbering of the inventory will turn my code into a huge mess, but using a script means that I have do dive into the scripts to edit messages. Is there a smarter solution that I'm missing?
Are you likely to be changing the inventory ID numbers?

For the record, an alternative (and possibly neater?) script for checking the inventory item used is as follows:
Code: ags

function hRailing_UseInv()  //when player uses an item on the railing hotspot
{
  if((player.ActiveInventory==iFlint)||(player.ActiveInventory==iTorch)){  //check if the used item has certain script names
    Display("Sliding down a flaming railing would be a neat trick, but the thing just won't light.");  //run the interaction
  }else if(player.ActiveInventory==iSomethingElse){
    Display("You used something else");
  }else{
    Display("You can't use that here");
  }
}

This checks the objects script name (here prefixed by i so that you can easily recognise it as an inventory item), much in the same way that your script did but without needing to pass the name to a string first. Otherwise they're pretty much the same.

Quote from: Wellington on Thu 28/02/2008 03:14:42
Is there anything like an "AGS Best Practices" document for coding novices that suggests ways to avoid shooting oneself in the foot?
I don't think there's a document, its more of a case of looking at other people's code and establishing a style. Perhaps go over the code in the DemoQuest and the manual tutorials and see how it was done there. Everyone has a different style and different ways of coding things.

However I would suggest that you never change the IDs of anything once you've started using them, and you try and avoid changing the script name of things because you'll end up breaking that bit of older code that uses it and you forgot about.

SMF spam blocked by CleanTalk