Hashtable "Load from file" etc.

This forum is currently in read-only mode.
  • Hi all

    I have a question regarding hashtables. I have been working on trying to create a simple inventory for my game, and for that I have looked through the different topics and caps related to this. Unfortunately they didn't completely solve my problem...

    Instead of showing the items as sprites in my inventory, I want a list with item names and amount held. For this I was planning to use a hashtable. But how do I use the "Load from file" feature when creating the hashtable? I have tried loading an INI file and making the hashtable from that, but it gives me trouble regarding values. (It keeps saying "0" even when I pick up items.)

    Creating keys on the fly when picking up items works, but I'd like to be able to somehow open the hashtable list somewhere so I can manage inventory more easily, or I fear I will loose track of what I am doing.

    How do I make the text show a list of all keys with a value above 0? I tried using the "NewLine" command, but the text still only shows the final name on the list.

    Showing the cap would probably be the easiest, so here is my embarrassing example:

    http://www.mediafire.com/?j1im58d0v310727

    Thanks, I hope I'll be able to understand all these things better one day.

  • Here is the file with some changes I made. I think it's what you meant.

    http://dl.dropbox.com/u/1024727/invtest.zip

    Some things:

    1. I think you did your key names wrong. You're writing:

    [quote:2hxllpjk]HashTable2("Gem")

    when it should be just:

    [quote:2hxllpjk]"Gem"

    2. Not sure why you're using both the ini and hashtable, so I removed the ini file and kept the hashtable.

    3. The reason your text wasn't listing items, is because on your "for each key" thing - you're changing the text instead of adding to it. So I had to put the text + new text.

    4. When including values in strings you need to wrap them with str( ), you forgot to do that for the right text box. Since you can't add values and strings together.

    5. I made it save the file when ever you add 1 to gem/sword. However you could just make it save when the player saves the game or before they quit.

    6. Unfortunately, you have to insert keys before you can add to them. So, you could either insert them all at the start of the level, or you could insert them before you add to them. In my example, I just put the insert "sword" with 0, before I added it, this way it creates it then adds to it if it didn't exist (the "insert key" thing is ignored if the key already exists).

  • Thank you, the changes are very useful. I should be able to learn something from this (I hope!).

    2. Not sure why you're using both the ini and hashtable, so I removed the ini file and kept the hashtable.

    That was attempt at including a pre-made text list of items and then loading them into the hashtable. I guess that wasn't working so well, and that:

    6. Unfortunately, you have to insert keys before you can add to them. So, you could either insert them all at the start of the level, or you could insert them before you add to them. In my example, I just put the insert "sword" with 0, before I added it, this way it creates it then adds to it if it didn't exist (the "insert key" thing is ignored if the key already exists).

    Having an event that creates a bucketfull of hashtable keys when the game starts isn't going to slow down the game too much, is it? From then on I guess the game will just load the information from what is saved. That looks useful.

  • Having an event that creates a bucketfull of hashtable keys when the game starts isn't going to slow down the game too much, is it?

    I doubt it will affect performance much at all.

  • Well, then that's great Thanks a lot again!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for the example. I didn't understand hashtables when trying sample from the wiki, this one's better.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)