2015-07-14 21:44:31 (edited by pelantas 2015-07-14 22:02:32)

hello all,

In your_adventure we make use of a inventory system, but right now are all the items that can be obtained in the current version listed in this system.below is a part of the code i use with a question afterwards:

void inventory()
{

show_game_window("your inventory");

while(true)
{
dynamic_menu inven;

inven.add_item_tts("resources");
inven.add_item_tts("tools");
inven.add_item_tts("combat usables");
inven.add_item_tts("food");
inven.add_item_tts("miscelanious");
inven.add_item_tts("turn back");
menu_result=inven.run("this is your inventory. select a category to acces the items you have.", true);

if(menu_result == 1)
{

show_game_window("resource inventory");

dynamic_menu res;

res.add_item_tts("fishes ... you own "+fish+" hit enter to examine");
res.add_item_tts("salmons ... you own "+salmon+" hit enter to examine");
res.add_item_tts("grane ... you own "+grane+" hit enter to examine");
res.add_item_tts("flour ... you own "+flour+" hit enter to examine");
res.add_item_tts("turn back");
menu_result=res.run("this is your inventory of resources.", true);

if(menu_result == 1)
{
v.stop();
v.speak_wait("a small silver coloured fish, ready for cooking.");
inventory();
}
if(menu_result == 2)
{
v.stop();
v.speak_wait("a middle-sized pink coloured salmon. ready for cooking");
inventory();
}
if(menu_result == 3)
{
v.stop();
v.speak_wait("a bush of tall granes tied up together");
inventory();
}
if(menu_result == 4)
{
v.stop();
v.speak_wait("a sack filled with flour to bake delicious loafs of bread");
inventory();
}
if(menu_result == 5)
{
v.stop();
v.speak_wait("alright. leaving the resource inventory.");
break;
}
}

As you may notice,quite clunky isn't it? but what i want to know, how can i edit this code so it doesn't display the items you don't have? and only will display the items you do have?
Please an example, since i learn the most from examples.

thanks in advance.

Hope to hear from you soon.

greetz mike

Visit the following website to see what games we have:
http://www.nonvisiongames.com
Or the following English marketplace to see what retrogames and game merchandise I am selling:
https://www.retro-kingdom.com

2015-07-15 00:29:32

You should create a class that represents the items and a array that can be the inventory of the player, because this way you will keep having so much work. A example, imagining that the item class hold a string about the name and the descryption of a item, in this case you should try:
for(uint x=0;x<inventory.length();x++) {
menu.add_item_tts(inventory[x].name);
}
however, if you want keep this system that separates each types of item by cession you will have to define a variable holding the object type, on this case a unsigned int.

2015-07-15 00:38:21

For the example you gave, you might do something like this:

if(fish>0) res.add_item_tts("fishes ... you own "+fish+" hit enter to examine");
if(salmon>0) res.add_item_tts("salmons ... you own "+salmon+" hit enter to examine");
if(grain>0) res.add_item_tts("grane ... you own "+grane+" hit enter to examine");
if(flour>0) res.add_item_tts("flour ... you own "+flour+" hit enter to examine");
res.add_item_tts("turn back");

But that, too, is quite clunky. Also, you would need to use the get_item_name method of the dynamic menu, since the item count can change.
You could go object-oriented, or dictionaries, or parallel arrays, and keep track of descriptions, names, and counts outside of the inventory function. The advantage to this is that you can then use loops to build your menus, and adding new items becomes much easier.
(It'd be nice if BGT dictionaries supported the indexing operator. It's situations like these that make me miss Javascript.)

Example using dictionaries:

dictionary resources, descs;

int get_resource(string name) {
if(resources.exists(name)==false) return 0;
int ret;
resources.get(name, ret);
return ret;
}

// function for adding to resources; returns the current value:
void add_resource(string name, int count=1) {
int value=get_resource(name);
resources.set(name, value+count);
return value+count;
}


// Part of the inventory function:
void inventory() {
// ... 
// Resources:
string[] keys=resources.get_keys();
for(uint i=0; i<keys.length(); i++) {
int count=get_resource(keys[i]);
if(count<=0) continue;
res.add_item_tts(keys[i] + ", you have " + count + "; hit enter to examine.", keys[i]);
}

res.add_item_tts("Turn back", "back");

menu_result=res.run("This is your menu for resources", true);

string name=res.get_item_name(menu_result);

// You might be better off ordering these differently, but this is the simplest for just getting descriptions:
if(name!="back") {
string desc="A " + name + ", no description available.";
if(descs.exists(name)) descs.get(name, desc);
voice.speak_interrupt(desc);
}

// Etc...

}

HTH

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-07-15 13:46:43

hello,

Thanks for the examples. due to this examples and explanations i was able to find a solution for my inventory problem.
I make use of the get_item_name method and it works quite well.

Thanks in advance.

greetz mike

Visit the following website to see what games we have:
http://www.nonvisiongames.com
Or the following English marketplace to see what retrogames and game merchandise I am selling:
https://www.retro-kingdom.com