HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: How to check Container for Item (or a String)?  (Read 2661 times)

Amarak

  • General Accounts
  • *
  • Posts: 281
    • View Profile
How to check Container for Item (or a String)?
« on: Sep 24, 14, 01:57:09 PM »

Hi guys! I need to know how to iterate through a container, such as an inventory, to find out if it contains a certain item. For example, if you are on a quest, and happen to have a few quest items in inventory, how do I check that? Another example would be to check if a crafting station (set up as a container with a few slots) has everything needed to make an item.

I've been searching but just can't seem to grasp this concept with the container system. I know it would be something like:
for each item in Inventory, if has itemA and itemB, then return true.

Another thing would be how to check quantity in the Stackable Items.

Any help or pointers to the right place in the wiki is greatly appreciated!
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: How to check Container for Item (or a String)?
« Reply #1 on: Sep 24, 14, 07:07:48 PM »

First part is knowing how to access the slots in your container, in mine I have slot names - slot1, slot2, slot3, ...

When you add an item to that container, knowing how it is added and then how to find out what that item is helps. I use names for items in the container. For a quest, check thru each slot and see if any names match.

For crafting, see what is in each slot of that container, and see if its the item needed. If you have more then 1 item needed to craft something (most do), then check for each item on list being there. If you find any missing, the combine is not possible, if all there then combine.

When stacking items, check each time you add an item. If that item is already there, add a number to that count, make sure to show that number, and just not add the item to a diff slot.
Logged

Amarak

  • General Accounts
  • *
  • Posts: 281
    • View Profile
Re: How to check Container for Item (or a String)?
« Reply #2 on: Sep 24, 14, 08:55:51 PM »

Thanks Thazager, I was thinking along those lines.
I am working with the Foundation Framework, and found the included Item system does have a function for what I need. In the script _ItemContainerClassMethods it has this method:

Code: [Select]
//Used to determine if the container has a specific item
method _HasContainedItemByID(containedItem as ID) as Boolean
  if me._containedItems has containedItem
    return true
  .
  return false
.

Now I need to find out how, in my own script, to call the method and input specific items by ID or Spec Key. This is where my newbieness in programming gets me, I'm not sure exactly how to grab the reference of the item I want to check for. 
Logged

keeperofstars

  • General Accounts
  • *
  • Posts: 998
    • View Profile
    • StarKeeper Online
Re: How to check Container for Item (or a String)?
« Reply #3 on: Sep 24, 14, 10:09:20 PM »

shouldn't hard code slots,

Should use a list or lookup list depending on how your items / needs are.

With a lookup list, which would work really well for like the crafting box. you can use the HAS function.

So when someone drags an item on the crafting GUI it adds that item to a craftingCombineList which is a lookup list. as a player adds or removes items from the GUI, you add or remove them from the list. When the player hits craft, you look up the recipe they crafting, and load that list in. Then for each item in the recipe list see if craftingCombineList HAS that item. if it doesn't return false and break out. If it does just move on to next list item of the recipe.

That way you are never set on having to store empty data, never have to worry about later on needing to expand from 6 slots to 20 for crafting etc.

Logged
[img]http://screencast.com/t/x7btcSSyp3h0[\img]

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: How to check Container for Item (or a String)?
« Reply #4 on: Sep 25, 14, 12:43:45 AM »

Yes, hard coding is not good. Which is why I add the slots in a loop to the clientarea, and the loop counter says how many. Easy to change. Names are simply "slot"+ i.

For using specs, Keeper would know more about those then I, as he has worked through them. Where I would rather write my own system, that way I can change it as the game needs.
Logged

Amarak

  • General Accounts
  • *
  • Posts: 281
    • View Profile
Re: How to check Container for Item (or a String)?
« Reply #5 on: Sep 25, 14, 12:11:57 PM »

A Lookup List does seem to be the right way to go. That leads to another question: Can I create the crafting recipes using a spec oracle to create the ingredient list?

Edit:
I can add fields of type string to the spec oracle, the strings are the names of items, and they can be placed in the lookup list.
« Last Edit: Sep 25, 14, 05:24:01 PM by Amarak »
Logged

Jrome90

  • General Accounts
  • *
  • Posts: 330
    • View Profile
Re: How to check Container for Item (or a String)?
« Reply #6 on: Sep 25, 14, 05:44:51 PM »

A Lookup List does seem to be the right way to go. That leads to another question: Can I create the crafting recipes using a spec oracle to create the ingredient list?

Edit:
I can add fields of type string to the spec oracle, the strings are the names of items, and they can be placed in the lookup list.

You could also just use a "List of String" and add to that list each item required to craft the item. You may also want a way to know how many of an item is required (i.e. 2 iron ore and 3 coal to make steel)

lookuplist indexed by string of integer
Where the string is the name, and the integer is the quantity needed.


Or another solution is a list of a class. The class holds a name and a quantity needed for that item.
Probably best to use the lookuplist.
Logged

Amarak

  • General Accounts
  • *
  • Posts: 281
    • View Profile
Re: How to check Container for Item (or a String)?
« Reply #7 on: Sep 25, 14, 06:01:58 PM »

Thanks Jrome, that's pretty much what I need. :)
Coding is my weak point, so it's slow going for me. I'm still trying to learn the right syntax and how to get references to items and such which makes it even harder.

I'm beginning to think I am in over my head at this point. I went to school for Game Art and Design, not programming.
Logged

keeperofstars

  • General Accounts
  • *
  • Posts: 998
    • View Profile
    • StarKeeper Online
Re: How to check Container for Item (or a String)?
« Reply #8 on: Sep 27, 14, 09:09:20 PM »

Yes, hard coding is not good. Which is why I add the slots in a loop to the clientarea, and the loop counter says how many. Easy to change. Names are simply "slot"+ i.

For using specs, Keeper would know more about those then I, as he has worked through them. Where I would rather write my own system, that way I can change it as the game needs.

but your hard coding the number of loops. What if one player needs to have 5 inventory slots, and another 10. So would have to look the number of "loops" up by a field stored on the player, or container Node, which means you can't reuse the code to work for lets say a crafting box, or for a trade window, or a quest event where you need to check for if the window has X items.
So by using a lookup list you can take the same Has items system, where you pass an indexed list of items to check, and pass in a lookup list to be checked, then you can reuse that same code for about a million different reasons, without rewriting a thing. With the reuse any time you need to check if some list has some objects, you can just pass the two pieces into the function and get the output you need. So 10 lines of code can be reused for dozens of systems.
Also the HAS function being used with the hash table is much more efficient then trying to run a recursive loop over the and comparing, then re-running over it again and again. The number of checks and comparisons skyrocket. Not to mention you can store very complex data types off the back end of the lookup list without having to check the data for that. So if you needed to load a whole class worth of data, you don't have to look at the data to check for it, just the key value. Which can be a much smaller data aspect, saving you bandwidth, and processing power. Not to mention the flexibility.


Logged
[img]http://screencast.com/t/x7btcSSyp3h0[\img]

Jrome90

  • General Accounts
  • *
  • Posts: 330
    • View Profile
Re: How to check Container for Item (or a String)?
« Reply #9 on: Sep 27, 14, 10:51:07 PM »

Yes, hard coding is not good. Which is why I add the slots in a loop to the clientarea, and the loop counter says how many. Easy to change. Names are simply "slot"+ i.

For using specs, Keeper would know more about those then I, as he has worked through them. Where I would rather write my own system, that way I can change it as the game needs.

but your hard coding the number of loops. What if one player needs to have 5 inventory slots, and another 10. So would have to look the number of "loops" up by a field stored on the player, or container Node, which means you can't reuse the code to work for lets say a crafting box, or for a trade window, or a quest event where you need to check for if the window has X items.
So by using a lookup list you can take the same Has items system, where you pass an indexed list of items to check, and pass in a lookup list to be checked, then you can reuse that same code for about a million different reasons, without rewriting a thing. With the reuse any time you need to check if some list has some objects, you can just pass the two pieces into the function and get the output you need. So 10 lines of code can be reused for dozens of systems.
Also the HAS function being used with the hash table is much more efficient then trying to run a recursive loop over the and comparing, then re-running over it again and again. The number of checks and comparisons skyrocket. Not to mention you can store very complex data types off the back end of the lookup list without having to check the data for that. So if you needed to load a whole class worth of data, you don't have to look at the data to check for it, just the key value. Which can be a much smaller data aspect, saving you bandwidth, and processing power. Not to mention the flexibility.



For searching:
Lookuplists are O(1) best case and O(n) worst case
Lists on the other hand are most likely O(n)
Therefore, a nested loop is O(n^2)  :(

with lookuplists you can have items in a player defined order (If the key was the slot #).
Lists have no such ability.
« Last Edit: Sep 27, 14, 11:01:22 PM by Jrome90 »
Logged

Thazager

  • General Accounts
  • *
  • Posts: 1155
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: How to check Container for Item (or a String)?
« Reply #10 on: Sep 27, 14, 11:27:45 PM »

Yes, hard coding is not good. Which is why I add the slots in a loop to the clientarea, and the loop counter says how many. Easy to change. Names are simply "slot"+ i.

For using specs, Keeper would know more about those then I, as he has worked through them. Where I would rather write my own system, that way I can change it as the game needs.

but your hard coding the number of loops. What if one player needs to have 5 inventory slots, and another 10. So would have to look the number of "loops" up by a field stored on the player, or container Node, which means you can't reuse the code to work for lets say a crafting box, or for a trade window, or a quest event where you need to check for if the window has X items.
So by using a lookup list you can take the same Has items system, where you pass an indexed list of items to check, and pass in a lookup list to be checked, then you can reuse that same code for about a million different reasons, without rewriting a thing. With the reuse any time you need to check if some list has some objects, you can just pass the two pieces into the function and get the output you need. So 10 lines of code can be reused for dozens of systems.
Also the HAS function being used with the hash table is much more efficient then trying to run a recursive loop over the and comparing, then re-running over it again and again. The number of checks and comparisons skyrocket. Not to mention you can store very complex data types off the back end of the lookup list without having to check the data for that. So if you needed to load a whole class worth of data, you don't have to look at the data to check for it, just the key value. Which can be a much smaller data aspect, saving you bandwidth, and processing power. Not to mention the flexibility.



For searching:
Lookuplists are O(1) best case and O(n) worst case
Lists on the other hand are most likely O(n)
Therefore, a nested loop is O(n^2)  :(

with lookuplists you can have items in a player defined order (If the key was the slot #).
Lists have no such ability.

Entering data into a spec makes it hard coded. That data is saved some place in the repository. That info is sent to the player when they download the game. It is hackable, so enjoy it.
Logged

keeperofstars

  • General Accounts
  • *
  • Posts: 998
    • View Profile
    • StarKeeper Online
Re: How to check Container for Item (or a String)?
« Reply #11 on: Sep 28, 14, 03:04:19 PM »

Yes, hard coding is not good. Which is why I add the slots in a loop to the clientarea, and the loop counter says how many. Easy to change. Names are simply "slot"+ i.

For using specs, Keeper would know more about those then I, as he has worked through them. Where I would rather write my own system, that way I can change it as the game needs.

but your hard coding the number of loops. What if one player needs to have 5 inventory slots, and another 10. So would have to look the number of "loops" up by a field stored on the player, or container Node, which means you can't reuse the code to work for lets say a crafting box, or for a trade window, or a quest event where you need to check for if the window has X items.
So by using a lookup list you can take the same Has items system, where you pass an indexed list of items to check, and pass in a lookup list to be checked, then you can reuse that same code for about a million different reasons, without rewriting a thing. With the reuse any time you need to check if some list has some objects, you can just pass the two pieces into the function and get the output you need. So 10 lines of code can be reused for dozens of systems.
Also the HAS function being used with the hash table is much more efficient then trying to run a recursive loop over the and comparing, then re-running over it again and again. The number of checks and comparisons skyrocket. Not to mention you can store very complex data types off the back end of the lookup list without having to check the data for that. So if you needed to load a whole class worth of data, you don't have to look at the data to check for it, just the key value. Which can be a much smaller data aspect, saving you bandwidth, and processing power. Not to mention the flexibility.



For searching:
Lookuplists are O(1) best case and O(n) worst case
Lists on the other hand are most likely O(n)
Therefore, a nested loop is O(n^2)  :(

with lookuplists you can have items in a player defined order (If the key was the slot #).
Lists have no such ability.

Entering data into a spec makes it hard coded. That data is saved some place in the repository. That info is sent to the player when they download the game. It is hackable, so enjoy it.

actually it's stored on the server repository, and gets called / loaded the first time the player makes a call to the spec. At that point it will cached the spec into the local repository, which is a complex binary mess to hack into. Since it's a binary mash up it's quite hard to "easily" hack. Also you can periodically invalidate the spec client side forcing it to load the server copy again. The spec data that gets stored in the spec is the static data anyways, the more changing data the items that would get / have potential for hacking, gets stored in nodes and marshaled back and forth via replication of the spec data.

so actually the spec system makes it much harder to hack.
Logged
[img]http://screencast.com/t/x7btcSSyp3h0[\img]

Jrome90

  • General Accounts
  • *
  • Posts: 330
    • View Profile
Re: How to check Container for Item (or a String)?
« Reply #12 on: Sep 28, 14, 06:41:48 PM »

Entering data into a spec makes it hard coded. That data is saved some place in the repository. That info is sent to the player when they download the game. It is hackable, so enjoy it.

Indeed, it is hackable. But what benefits does the user have being able modify the spec on the client?
Any important data used on the server would be retrieved from the server side spec.

Logged