Text-based inventory? [SOLVED]

Started by frission, Thu 11/10/2007 03:17:30

Previous topic - Next topic

frission

My game is not mouse driven and I would rather have an AGI-style text inventory (you hit tab and a little display box comes up enumerating all of the stuff you have). I plan to just cycle through the inventory, grab the names, and put them in a big string. (If they type in "look at [inv item]" then I'll display the sprite and a little description or something like that.)

Now it seemed natural to me that there would be some way to see what inventory a player had, but for all of my searching through the manual and on the forums I don't see an easy way to do it. The non-easy way would be to cycle through all possible inventory objects (and I imagine I'd have to hard-code a maximum array size to cycle through) and then use InventoryQuantity to see if the player had it. Surely there's a better way!?

Khris

readonly InventoryItem* InvWindow.ItemAtIndex[]
readonly int InvWindow.ItemCount

(readonly static int Game.InventoryItemCount)

frission

Quote from: KhrisMUC on Thu 11/10/2007 04:02:16
readonly InventoryItem* InvWindow.ItemAtIndex[]
readonly int InvWindow.ItemCount

(readonly static int Game.InventoryItemCount)

Great! Thanks!

I don't have an inventory window anymore, so those particular ones don't help, but Game.InventoryItemCount simplifies the process of figuring out whether they have an item, and keeps me from having to hard-code the value.

Here's my quicky text-only inventory display script:

Code: ags

function show_inventory_window () {
	String inventoryListing = "You are carrying:";
	int i = 0;
	int inventoryCount = 0;
	while(i<Game.InventoryItemCount) {
		i++;
		if(player.InventoryQuantity[i]>0) {
			inventoryListing = inventoryListing.Append(String.Format("[ - %s",inventory[i].Name));
			inventoryCount++;
		}
	}
	if(inventoryCount>0) {
	  Display(inventoryListing);
	} else {
		Display("You're not carrying anything.");
	}
}


Of course, there's more to a text-only inventory than displaying it (I've just about finished writing a long function which processes any "look at X" where X is an inventory item and handles it separately than other things that are looked at, in this case it loads it up into a custom GUI that displays the image and a description), but it's coming along nicely.

Khris

Quote from: frission on Thu 11/10/2007 04:42:50I don't have an inventory window anymore, so those particular ones don't help
The GUI/InvWindow doesn't have to be visible. The functions will work nonetheless.

Radiant


Blu Aardvark

I'm designing a text-based (non-graphical) game, and found the above "quicky" script quite helpful (with a few modifications, of course).

My problem: I've discovered that I'm currently limited to displaying only 16 lines at a time, meaning only 15 inventory items would be visible, unless I changed the font or otherwise did something undesirable.

Is there a relatively simple way to space the list into two separate columns?

Here's what I'm looking at:

Code: ags
function show_inventory () {
	String inventoryListing = "You are carrying:";
	int i = 0;
	int inventoryCount = 0;
	while(i<Game.InventoryItemCount) {
		i++;
		if(player.InventoryQuantity[i]>0) {
			inventoryListing = inventoryListing.Append(String.Format("[ - %s",inventory[i].Name));
			inventoryCount++;
		}
	}
	if(inventoryCount>0) {
	  roomText.Text = inventoryListing;
	} else {
		roomText.Text = Game.GlobalMessages[996];
	}
}

Khris

Code: ags
function show_inventory () {
	String inventoryListing = "You are carrying:";
	int i = 0;
	int inventoryCount = 0;

	int rightColumn = 0;
	String tmp;
	int lc;

	while(i<Game.InventoryItemCount) {
		i++;
		if(player.InventoryQuantity[i]>0) {
			tmp = inventory[i].Name;
			if (!rightColumn) {            // left column
				lc = 25 - tmp.Length;
				while (lc > 0) {
					tmp = String.Format("%s ", tmp);  // add space
					lc--;
				}
				inventoryListing = inventoryListing.Append(String.Format("[ - %s", tmp));
			}
			else inventoryListing = inventoryListing.Append(String.Format("- %s", tmp));   // right column
			inventoryCount++;
			rightColumn = 1 - rightColumn;     // alternate column
		}
	}
	if(inventoryCount>0) {
	  roomText.Text = inventoryListing;
	} else {
		roomText.Text = Game.GlobalMessages[996];
	}
}


Not tested.

KiraHaraReturns

Quote from: Blu Aardvark on Tue 16/09/2008 06:30:41
I'm designing a text-based (non-graphical) game, and found the above "quicky" script quite helpful (with a few modifications, of course).

My problem: I've discovered that I'm currently limited to displaying only 16 lines at a time, meaning only 15 inventory items would be visible, unless I changed the font or otherwise did something undesirable.

Is there a relatively simple way to space the list into two separate columns?

Here's what I'm looking at:

Code: ags
function show_inventory () {
	String inventoryListing = "You are carrying:";
	int i = 0;
	int inventoryCount = 0;
	while(i<Game.InventoryItemCount) {
		i++;
		if(player.InventoryQuantity[i]>0) {
			inventoryListing = inventoryListing.Append(String.Format("[ - %s",inventory[i].Name));
			inventoryCount++;
		}
	}
	if(inventoryCount>0) {
	  roomText.Text = inventoryListing;
	} else {
		roomText.Text = Game.GlobalMessages[996];
	}
}


I would like to implement this code in my game. Can anyone explain what roomText is? I've created a textbox named roomText, but nothing happens :(

Khris

A text box is for entering text; you need a label.

KiraHaraReturns

ok I figured it out and it doesnt work as I expected. What I need is a list equivalent to an inventory window, but with names instead of icons. This one above creates just a list with names without implementing interactions :(

Khris

There's actually a thread somewhere with code that does exactly that; the basic idea is to use DynamicSprites and draw the inventory item's name to them.
Create a global array like this:
Code: ags
DynamicSprite* invSprite[30];


Then iterate through the items in game start and do something like
Code: ags
  invSprite[i] = DynamicSprite.Create(60, 12);
  ds = invSprite[i].GetDrawingSurface();
  ds.Clear(0);
  ds.Drawingcolor = 15; // white
  ds.DrawString(1, 1, eFontNormal, inventory[i].Name);

(Not complete code)


SMF spam blocked by CleanTalk