playMaker

Author Topic: Inventory and Hash Tables loop lag[SOLVED]  (Read 4651 times)

Bjakuja

  • Junior Playmaker
  • **
  • Posts: 75
Inventory and Hash Tables loop lag[SOLVED]
« on: September 07, 2013, 06:11:01 AM »
Hi! I need some help here, not programmatically cause I am very familiar with Playmaker but technically related to Hash Tables and Array lists:

I am working on inventory, something like in Gothic 2 where you have chest items on the right side and inventory on the left side. When picking items from chest they will spawn in inventory with automatic list sorting and positioning by name on the screen, you can see in image provided what I am talking about. If I mouse over items they will scale and item information will be displayed on the bottom of the chest or inventory (depends on where the item is). By clicking items in chest they will be added to inventory hash tables and to number count hash table and vise versa they will be stored in chest by clicking them in inventory if chest is opened. This is system is fully working without bugs but still I have problems due to the hash table lag. Tried many different solutions, with planes instead of GUI textures and also with only text but none solution solved lag problem.

The inventory system works this way:
When item is clicked, it will set event properties (just item name) and send event to chest to subtract item from Chest Hash Table and to add it to Inventory Hash Table by get / set hash table. After that chest system will get event properties (store item name) destroy current chest list and create another empty list, then get hash table keys in array list and then loop by ArrayListGetNext through list by until it get's all items. 20 Items to loop is enough for me cause I can use loop index from 0 to 20 but still have that lag when clicking fast on items. Current inventory is made by 16 items loop, you can see on images bellow!

So I need technical help, did you test how many items can be looped through hash table instantly cause more items I have the greater lag will be.
Also if you want to see what I am talking about just use that Array Inventory Example and try to add 20 - 30 items to the loop system and you will get lag.
If hash tables and arrays can't support more then 20 - 30 items then what can I do, is there any other way for doing those things instead hash table / array list GetNext?
Gothic has 35 items in inventory and chest at the same time which is 70 and items spawn instantly when clicked .

I am planning to use this system to storage, shop and item transfer from chest to inventory and vise versa but I can't create bigger list due to the lag, when have 9 items in chest and 9 items in inventory then I don't have lag but more then that lag will start little by little.
Can anyone help me here or can you check those Hashtable systems, or JeanFabre can you somehow reduce that lag by checking HashTable / ArrayListgetNext?

If you solve this for me then you will see soon some very great game created only with Playmaker and I will be very grateful to you ;)
Thanks in advance!
« Last Edit: September 09, 2013, 06:37:15 AM by Bjakuja »

Bjakuja

  • Junior Playmaker
  • **
  • Posts: 75
Re: Inventory and Hash Tables loop lag
« Reply #1 on: September 08, 2013, 05:49:43 PM »
Go to 4:30
and you will see two huge lists like in my inventory a lot bigger and items when clicked they spawn instantly in inventory or chest and this game is made 10 years ago.
Just wanted to add this example of inventory from Gothic 2, this is my thought and I want to make something that uses this system in my own game. So I know I can make this in Playmaker, I already made it but with lag and delay when picking items and it's causes by item loop through array list. I hope we can solve this problem. If you have any idea pls post here.
Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Inventory and Hash Tables loop lag
« Reply #2 on: September 09, 2013, 01:47:44 AM »
Hi,

Ok, let's sort this out :)

 There is one thing that is slowing down your system given your description.

you should not delete and recreate the list on every action, instead, you should simply add/delete where it applies, and ONLY to the item targeted.

Can you confirm this?

What 2d framework are you using to show the items on screen?


Bye,

 Jean

Bjakuja

  • Junior Playmaker
  • **
  • Posts: 75
Re: Inventory and Hash Tables loop lag
« Reply #3 on: September 09, 2013, 02:38:30 AM »
Hi Jean! Well is not that simple cause I want for the list to be sorted every time new item is added that's why I used destroy/create list where I add / remove items from hash tables, clear array list, use hash table keys and sort list in order for new items to be created properly on the screen, then I used ArrayGetNext and create every item again, using GUI textures to display items, but that's not the issue, I used texts, even plane with different materials for each item but system is slowed by loop through items and create each every time action is called.
I thought the problem is in Hashtables and some sort of a bug but I was wrong cause I checked it's script and there is nothing wrong with it.
Good thing is that I just woke up, started unity and figured out how to create this properly and then I saw this reply where you suggesting me the same thing :D
Currently working on this and I got an idea of how to do this, so I will post here my results, I am on verge of solving this ;)
Thanks Jean!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Inventory and Hash Tables loop lag
« Reply #4 on: September 09, 2013, 02:54:11 AM »
Hi,

 Very good.

One thing: Unfortunatly, the example from "gothic 2" is most likely using a database, which means sorting is instant and all handled by the database engine.

 in your case, indeed there is an overhead. In order for me to give you a headstart on how to sort things better, can you tell me what's the "field" you expect to be sorted?

 Typically, to sort things, I usually extract the keys of the hashtable, into an arrayList, sort that list, and then to feed that back in to the gui somehow. In your case, you could simply add the string to the sorted list, sort it, and then find it's location, that woudl totally prevent having to rebuild everything, and it would give you the right sorted index, even before having added it into your inventory. So you can place your various related properties for that item in the right indexes.

Bye,

 Jean

Bjakuja

  • Junior Playmaker
  • **
  • Posts: 75
Re: Inventory and Hash Tables loop lag
« Reply #5 on: September 09, 2013, 03:14:36 AM »
Yeah, just as I thought, it's working this way, I have reduced complexity of my ArrayList a bit and I set everything on item that is clicked, it's very complex and a lot of branching but I finally managed to create list on both sides instantly when picked like in Gothic. The only lag and small delay will occur when inventory and chest are opened because that's the time when items are created on the lists but after that when item is added only that item is created and rest of the items are rearranged and sorted by their indexes and names.
That lag on the start is meaningless because I noticed it in almost every game when you open bigger inventory so I suppose they use similar method like this one.
I can say it's solved but still I would like to run some tests here and Jean, is there any way to create an action that will get multiple Hash Tables values and multiple HashTable set values?
What I mean is that when I use hash table get, I can only get values from one table at the time, then I have to repeat this action few more times in order to get all values, so this action would simplify this , don't you think?
Like in the example image at the bottom, similar to set/get event properties you provided where you have to define number of hash tables and then for each to set/get values?

Thanks for everything ;)


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Inventory and Hash Tables loop lag
« Reply #6 on: September 09, 2013, 05:22:37 AM »
Hi,

 Good request, I meant to do that since a long time.

Simply download it from the wiki again:

https://hutonggames.fogbugz.com/default.asp?W715

look for HashTableAddMany, HashTableGetMany and HashTableSetMany

Bye,

 Jean

Bjakuja

  • Junior Playmaker
  • **
  • Posts: 75
Re: Inventory and Hash Tables loop lag
« Reply #7 on: September 09, 2013, 06:22:23 AM »
Wow Jean!
Don't know what to say, that's exactly what I wanted and now I can make perfect inventory system with those actions :)
And btw I made a few tests and loop working perfectly now, no more lag nor delay, inventory working with tremendous speed now, items spawn instantly and they arrange in a blink of an Eye :)
Thanks a lot Jean for everything!
You can say this thread is SOLVED now ;)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Inventory and Hash Tables loop lag[SOLVED]
« Reply #8 on: September 09, 2013, 07:16:31 AM »
Hi,

 Good! I marked this thread as solved.

 Bye,

 Jean