Stuck with looping through an array and comparing it to dictionary

0 favourites
  • 5 posts
From the Asset Store
5 Types of Touch Buttons containing 90 touch buttons from D-Pads to Touch Joy Sticks and more.
  • So I'm working on a sort of CYOA thing.

    It unfolds in scenes (scene 1, 2, 3, 4, 5, etc), and each scene I need to check to see what items the player has, and print a message to a textbox depending on that.

    I'm doing this with a dictionary object (dictStoryText) and using an array to store the items the player currently, which can change as the game proceeds (arrInventory).

    This is what I need to do, roughly:

    I can't post my capx, but what I've been trying to do is something like this:

    My dictionary object looks like this:

    This is the code I've been messing with:

    This prints, but it doesn't print the right messages (it doesn't seem to be able to figure out what items I have).

  • It's difficult to figure out by your screenshots, but one mistake I noticed is that you are using arrInventory.CurValue in event 193. CurValue expression is only available in "Array For each element" loop. It will not work in System-For loop. But, the problem with Array-For loop is that you can't stop it, so you'll need to rewrite some of the code.

    Besides, your events 192-193 are inside the For loop, so they are executed multiple times. I think the structure of your function should be like this:

    On function checkProhibitedItems
    ...System For x from 0 to arr.width-1
    ......arr contains 
    ...prohibitedItemsLoop=arr.width : success
    ...Else : fail
    

    Also, prohibitedItemsLoop counter is not a good indicator that the array doesn't contain prohibited items. What if the prohibited item is the last element in the arrInventory? The loop will stop at the end, but the function will still return "success". I suggest using a boolean variable "prohibitedItemFound" instead. Set it to 1 if array contains prohibited item.

    .

    Finally, try running the game in debug mode and also add Browser-Log actions in key events to print some important information, like variable values etc. This will help to understand what's going wrong.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hey, thanks for responding.

    Good catch on the CurValue. Also, thanks for the tip about browser log. I didn't know that existed.

    Is this something like what you're talking about? (the boolean variables are a good idea for use within the loops):

    That seems to never return failure though, even though it seems from browser logs that the tokenat(tokenat(dictStoryText.CurrentKey,2,"|"),loopindex("x"),",") is finding the right items.

  • Almost. You need to remove the "Else" part. Reset the boolean variable to 0 at the beginning of the function (or you can use local variable by the way). And only set it to 1 when array contains value, don't reset it to 0 when it doesn't.

  • Got it working after a good bit of trial and error. Thanks for your help.

    Final function wound up looking like this:

    Additionally, I had to update the checkRequiredItems function (this was tough for me, because it needs to check multiple times if there were multiple required items):

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