How do I Properly Use Array Conditions

  • Hello, I've run into a couple of problems enough times to think that I must be doing something wrong, or there are unusual limitations in how arrays work. Here's a couple of examples: 1) I have an array with data set up in it and I can use an action to tell me the value at a location in the array with no problems. But if I try to use the array location in a condition like "array: If value at (x,y) = 1, then... do this", it doesn't work. 2) If I try to combine actions into something like "multiply the value at array (x,y) times 2", it doesn't work either. For 1) I have to first set the value at (x,y) into a local variable and then use THAT in the event's condition. For 2), I have to use two actions: "set the value at array(x,y) to a local or instance variable and then use a second action to multiply or manipulate the value saved.

    Is this the norm?

    Also, how and when would you use the condition: Compare Current Value?

    Thanks

  • try using a "compare 2 variables" condition instead, and compare Array.at(x) = 1 instead

    does that work?

    also, try putting the "do this" in a sub event instead.

  • Works fine:

  • Ok, upon further testing, I've found that if you actually set the value immediately before, and then you test the value in a subevent of said event, like BlackHornet did in his example above, then it does work. BUT, if I've loaded the values into the array from a .json file in a previous function and now I'm just comparing a value in the array, it still isn't working. Any action that reads the array works fine, but the condition comparing the value at xy to a set value isn't working. (I think I forgot to mention that the array was loaded from a .json file in my original post...)

  • Ok, I made an example of what I'm doing. Maybe you can point out what I'm doing wrong: https://www.dropbox.com/s/rvjnr3h59yq2k ... .capx?dl=0

    Thanks

  • "A" is not a valid JSON string. Have a look at my example. If you want to initialize the array, do so as I did in the first example and then save that AsJSON, then load that from WebStorate to get it back.

  • I think I see now - you want Load from JSON: WebStorage.LocalValue("A")

  • I must apologize. I linked the wrong example file for you to see. Here is the correct one, based on your example you sent earlier: https://www.dropbox.com/s/r4hj8pspx3mot ... .capx?dl=0

    Thanks again

  • Your data is all in quotes which makes them strings, so comparing against a number won't work. Either compare to "10" or get rid of the quotes to make your data numbers.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi BlackHornet, and thanks for your time on this. The datafile in the capx is this (separated for clarity): https://www.dropbox.com/s/17n2tbu8351t6 ... .json?dl=0

    It seems to be a standard .json format. It loads the array with the proper data, all of which are numbers, and I don't think they are in quotes. Is this the data you are referring to? I'm using a .csv to .json converter to get the data from a .csv into .json format. Maybe that is where the problem lies?

  • Ok, I see the quotes you are referring to. I just thought that was how .json files worked. I wonder how they are getting placed there from the .csv file?

  • I've been using the CSVtoArray plugin to copy the .csv file, which contains words along with numbers into an array, then downloading that array's data into a .json file. From then on, I only use the data in the .json file for the game, which has been working for me, thought with the added difficulties mentioned in the OP. Is there a better, easier way to get a large amount of data from a Excel file into an array for a game to use? I'd appreciate any help.

  • If the data is out of you control, you can also use int() to convert the string to a number.

  • I just went to one of those online .csv to .json converter websites and it is also putting all the numbers in quotations. Is this something that happens if there are words and numbers in the .csv? I'm using the 1st column of the spreadsheet for the name of each row's purpose. There are also rows used for dialog. Should I not be mixing words and numbers in the same .csv before converting to .json? Maybe I have to use quotation marks before and after any box that has a word or sentence in it on the spreadsheet? I'll have to experiment.

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