Hashtable "Load from file" etc.

For questions about using Classic.

Post » Sun Dec 12, 2010 9:58 am

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.
B
10
S
3
G
3
Posts: 110
Reputation: 2,206

Post » Sun Dec 12, 2010 1:45 pm

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")[/quote:2hxllpjk]
when it should be just:
[quote:2hxllpjk]"Gem"[/quote:2hxllpjk]

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).
B
134
S
65
G
16
Posts: 1,765
Reputation: 19,188

Post » Mon Dec 13, 2010 11:54 am

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

[quote="alspal":3u9ss2ov]
2. Not sure why you're using both the ini and hashtable, so I removed the ini file and kept the hashtable.[/quote:3u9ss2ov]

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:

[quote="alspal":3u9ss2ov]
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).[/quote:3u9ss2ov]

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.
B
10
S
3
G
3
Posts: 110
Reputation: 2,206

Post » Mon Dec 13, 2010 3:01 pm

[quote="Vati-nyan":245hzxdg]
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?[/quote:245hzxdg]

I doubt it will affect performance much at all.
B
134
S
65
G
16
Posts: 1,765
Reputation: 19,188

Post » Mon Dec 13, 2010 3:53 pm

Well, then that's great :) Thanks a lot again!
B
10
S
3
G
3
Posts: 110
Reputation: 2,206

Post » Tue Dec 14, 2010 11:19 am

Thanks for the example. I didn't understand hashtables when trying sample from the wiki, this one's better.
ImageImage
B
25
S
6
G
8
Posts: 773
Reputation: 6,643


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests