My inventory system for my game is in the form of an array. So let's say I want to check if a player has an item, do I have to manually check each x value? Or is there another way, especially if I need to search for more than one item....
Printable View
My inventory system for my game is in the form of an array. So let's say I want to check if a player has an item, do I have to manually check each x value? Or is there another way, especially if I need to search for more than one item....
Use fastloops to loop through the array.
If the inventory is 10*5 cells, then start loop X 10 times, and on each loop X, start loop Y 5 times. Then on each loop Y, compare the value of Cell(X,Y) in the array.
You can learn more about fastloops here: http://www.clickteam.com/creation_materials/tutorials/download/The_Clickers_Guide_To_Fastloops.pdf
Btw, there is a new inventory object made by Clickteam. You could check that out as well! :)
Yep. Nested loops are the way to go for this. Popcorn is right on the money. I have nothing else to add. I just wanted to pop in. :)
I think the Array X object supports searching for values ;)
Got more to contrib than Konidias, for whom increasing post count is "EVERYTHING!!"
Hahaha... I have less than 1000 posts after 3 years, I sure hope post count isn't everything or I'm doing a terrible job. Okay, I'll add something to the conversation. You can reduce the amount of loops needed if you keep all items in one X value and store them along the Y value. You can easily sort the objects into a grid without resorting to storing each line of objects on different values. This way you only need one fastloop instead of nested fastloops.
Oh, and put the fastloop inside a group (like "Check Item") that you can deactivate when not in use. This will prevent MMF from checking the fastloop every tick when it isn't needing to find an item.
See! See! I can add to the advice. :D
Pah! I'll make it even more petty!
You can replace a 1D array with the Internal List Object!
Or you could just use a 2D array and combine the index with mod and / to change nested fastloops to one fastloop.
Allow me to elaborate...
The advantage of the internal list object is the "Get Index of Item Exact Match" expression. The regular list object has two similar expressions, "Find String" and "Find String Exact". If the string (or substring in the case of "Find String") is found, then the index of that list element is returned. If no match is found, -1 is returned.
Alternatively, you can learn c++ and write your own inventory extension designed specifically for your game. This is not overly complicated.
After trying a few of your guys' suggestions, i got something good. But it's still a bit buggy. How do I do this multiple times, for example if I want to search for two items at the same time?
Thanks!
Have yet another loop that loops through each of the items to search for and compares them to the current item?
I'm sorry this is so confusing. I've tried a few times and I cannot get it to work. Could someone please either make an mfa or write exactly what I need to do? I hate asking that but I have tried so hard and everything ends up looping and going haywire. Thanks in advance.
Hydra, you made my Day :) ".....and everything ends up looping and going haywire."
I see Loops everywhere xD
Seriously, perhaps this "Information on Fastloops is of Value for You: http://www.create-games.com/article.asp?id=106
Perhaps there are other nice articles about fastloops on the net and some other Forum-Members could point you to them too.
With kind regards,
jahkri
Thanks for the information. I am continually trying with no success :(
Well searching for two items "at the same time" is just running the search as many times as you need with fastloops.
You'd first run a loop for the number of items you are wanting to search for. You could store these many ways, but I'd typically use something like a string and then tokenize it or use string parser to break it up. So if you have something like:
"item1,item5,item9" as the items you want to look up, you'd tokenize/parse this string, and then run a fastloop for the number of tokens it produces. In this case, three times.
Then this loop would take each token at that loop index (token 0, then token 1, then token 2) and check it with your other nested fastloop as usual.
So:
Loop #1 will loop through all the items you want to search for.
Nested in Loop #1 is Loop #2, which will run through all X slots in your array
Nested in Loop #2 is Loop #3, which will run through all Y slots in your array
Then you will get your search result in Loop #3, and you can perform whatever actions you need to there. It will then go back to Loop #1 and start the process over again for the next item to search.
Of course, you don't even need loop #3 if you're just using the X index... then you can just cycle through the X and find the match.
OK so I have:
Attachment 8007
And I'm planning on replacing the "1" with your text tokenizing idea. But this simply does not work. Can you see what's wrong?
Thanks for the reply!
Hydra - I think you don't need the last condition on line 795:
Index x (Array)=LoopIndex ("LoopINV")
That's probably what's stopping it from working, since you're not setting the array's current x position.
The first condition should be sufficient for your search.
Personally, I wouldn't have had my inventory stored in the default array. I prefer using the INI++ or the AssArray, and I think, though I might be wrong, that both offer searching functions. Maybe the AssArray only lets you search for keynames and not is contents, but you could make up a simple pattern to store inventory keys as substrings (ex. "item_apple", "item_ball").
In any case, looping every time you want to check for an item might be a bit inefficient if your inventory grows. Can you not run the loop each time the inventory is modified to generate a string index and store it somewhere? This way, searching is made a lot easier and, since it's an index, you'd get the array position where that item is stored. Still, for this you need your inventory engine to redirect all possible inventory interaction through one single "channel" or route.