Hi, first post here and I would be very grateful for any help.

I'm making a newspaper puzzle-esque puzzle game, and have chosen to store puzzle solutions as arrays, e.g.:
[
[0,0,0,0,2,0],
[3,1,2,4,3,0],
[0,4,3,1,2,3],
[0,3,4,2,1,0],
[0,2,1,3,4,0],
[0,3,2,2,0,0]
]

I am trying to create a level select screen where the user can load different puzzles by clicking on different buttons, which store the puzzle solution as an array export variable.

When the export var type is set to an array, however, Godot asks for each item of the array separately in the export var menu for the scene. This method of inputting the puzzle solution is pretty slow, as my solution arrays contain multiple arrays inside them that must be input one index at a time.

I would much rather be able to copy and paste the solution into the export var field as a string in the form shown above, but I can't find any method of converting a string containing an array into the array contained within.

While googling I found one potential solution using JSON, e.g.:
var json_string = to_json(my_array)
var new_array = parse_json(json_string)

However, it seems that this still returns a string, rather than an array?

I am pretty new to Godot, so I might be making some kind of silly misunderstanding, but I hope somebody can help!

  • duane replied to this.
  • The problem with the code as it is, is that you're taking a string (puzzle_data), converting it to json (of a string -- json_string), and then parsing it back to new_array. It's still a string, of course.

    export var puzzle_data := ""
    
    func _on_button_up() -> void:
    	var json_string = to_json(puzzle_data)
    	var new_array = parse_json(json_string)
    	
    	global.current_puzzle = new_array

    I think you mean to feed puzzle_data into parse_json.

    export var puzzle_data := ""
    
    func _on_button_up() -> void:
    	var new_array = parse_json(puzzle_data)
    	
    	global.current_puzzle = new_array

    Just make sure that the string you put in is valid json -- the string in your first post works fine.

    You would use split.

    var level_string = "3,1,2,4,3,0"
    var level_array = level_string.split(",")
    for level_num in level_array:
    	print(int(level_num))

    frooki

    When I run that code, I get an Array of Arrays of Float back from parse_json(). Are you sure you're getting a String?

      duane

      Ah - you're right. The print statement I inserted in the above screenshot does print, so that must not be the problem.

      The reason I thought it was outputting the wrong type was because I'm getting the above error, which I thought seemed to be telling me that I am trying to set global.current_puzzle (which is cast as an array) to a string. I guess the issue is something else, though?

      EDIT:
      When I replace
      global.current_puzzle = new_array
      with:
      global.current_puzzle = [
      [0,0,0,0,2,0],
      [3,1,2,4,3,0],
      [0,4,3,1,2,3],
      [0,3,4,2,1,0],
      [0,2,1,3,4,0],
      [0,3,2,2,0,0]
      ]

      the rest of the code works perfectly - so it seems there may be something wrong about the typing of new_array vs. global.current_puzzle, or some other issue with setting global.current_puzzle?

      EDIT again:
      typeof(new_array) returns a 4 = String, so it does indeed seem like new_array is not an array?

        frooki changed the title to (STILL UNSOLVED) Turning a String containing an Array into an Array? .

        duane

        Hey, thanks - it's declared as an array as above.

        You can do it the way I showed for a single array. What you have is a multi-dimensional array, which is more complex. So you would have to use a Dictionary. There are many ways to convert a String to the Dictionary, though JSON may be the most automatic. In any case, you should post your full code, cause it's not clear what exactly you are trying or what the problem is.

          cybereality

          Thank you, yes, it's a multi-dimensional array - I didn't know the correct terminology.

          I've uploaded the entire project file here - the problematic script is ChangePuzzleButton.gd.
          Thank you so much!

          https://we.tl/t-DhiArFe534

          The problem with the code as it is, is that you're taking a string (puzzle_data), converting it to json (of a string -- json_string), and then parsing it back to new_array. It's still a string, of course.

          export var puzzle_data := ""
          
          func _on_button_up() -> void:
          	var json_string = to_json(puzzle_data)
          	var new_array = parse_json(json_string)
          	
          	global.current_puzzle = new_array

          I think you mean to feed puzzle_data into parse_json.

          export var puzzle_data := ""
          
          func _on_button_up() -> void:
          	var new_array = parse_json(puzzle_data)
          	
          	global.current_puzzle = new_array

          Just make sure that the string you put in is valid json -- the string in your first post works fine.

            duane

            Thank you so much! This fixed the problem perfectly!

            I hadn't encountered json before, and so didn't properly consider what that code was doing - I basically copied it from another thread I found hoping it would work. It makes sense that I wouldn't need to convert the original variable to json now that you point it out.

            Thank you cybereality too - you were both very helpful. ^^

            cybereality changed the title to Turning a String containing an Array into an Array? .