Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | CodeCraft |
I load a dictionary from a json file, but when comparing hashes the same dictionary hardcoded in the script has a different hash and also sometimes behaves different.
My file (test.level):
{"meta":{"stars":[5,6,7]},"0":{"x":-300,"y":-600,"type":"BUTTON-ON"},"1":{"x":-180,"y":-600,"type":"BUTTON-ON"},"2":{"x":-60,"y":-600,"type":"BUTTON-OFF"},"3":{"x":60,"y":-600,"type":"BUTTON-ON"},"4":{"x":180,"y":-600,"type":"BUTTON-ON"},"5":{"x":300,"y":-600,"type":"BUTTON-ON"},"6":{"x":-240,"y":-450,"type":"XOR","source1":0,"source2":1},"7":{"x":0,"y":-450,"type":"AND","source1":2,"source2":3},"8":{"x":240,"y":-450,"type":"XOR","source1":4,"source2":5},"9":{"x":0,"y":-300,"type":"SWITCH-OFF","source":7},"10":{"x":-120,"y":-150,"type":"OR","source1":6,"source2":-9},"11":{"x":120,"y":-150,"type":"AND","source1":9,"source2":8},"12":{"x":0,"y":0,"type":"AND","source1":10,"source2":11},"13":{"x":0,"y":150,"type":"GOAL","sources":[12]}}
My code:
var level_data = JSON.parse(read_file("user://test.level")).result
var level_data2 = {"meta":{"stars": [5, 6, 7]},"0":{"x":-300,"y":-600,"type":"BUTTON-ON"},"1":{"x":-180,"y":-600,"type":"BUTTON-ON"},"2":{"x":-60,"y":-600,"type":"BUTTON-OFF"},"3":{"x":60,"y":-600,"type":"BUTTON-ON"},"4":{"x":180,"y":-600,"type":"BUTTON-ON"},"5":{"x":300,"y":-600,"type":"BUTTON-ON"},"6":{"x":-240,"y":-450,"type":"XOR","source1":0,"source2":1},"7":{"x":0,"y":-450,"type":"AND","source1":2,"source2":3},"8":{"x":240,"y":-450,"type":"XOR","source1":4,"source2":5},"9":{"x":0,"y":-300,"type":"SWITCH-OFF","source":7},"10":{"x":-120,"y":-150,"type":"OR","source1":6,"source2":-9},"11":{"x":120,"y":-150,"type":"AND","source1":9,"source2":8},"12":{"x":0,"y":0,"type":"AND","source1":10,"source2":11},"13":{"x":0,"y":150,"type":"GOAL","sources":[12]}}
var level_data3 = JSON.parse("{\"meta\":{\"stars\":[5,6,7]},\"0\":{\"x\":-300,\"y\":-600,\"type\":\"BUTTON-ON\"},\"1\":{\"x\":-180,\"y\":-600,\"type\":\"BUTTON-ON\"},\"2\":{\"x\":-60,\"y\":-600,\"type\":\"BUTTON-OFF\"},\"3\":{\"x\":60,\"y\":-600,\"type\":\"BUTTON-ON\"},\"4\":{\"x\":180,\"y\":-600,\"type\":\"BUTTON-ON\"},\"5\":{\"x\":300,\"y\":-600,\"type\":\"BUTTON-ON\"},\"6\":{\"x\":-240,\"y\":-450,\"type\":\"XOR\",\"source1\":0,\"source2\":1},\"7\":{\"x\":0,\"y\":-450,\"type\":\"AND\",\"source1\":2,\"source2\":3},\"8\":{\"x\":240,\"y\":-450,\"type\":\"XOR\",\"source1\":4,\"source2\":5},\"9\":{\"x\":0,\"y\":-300,\"type\":\"SWITCH-OFF\",\"source\":7},\"10\":{\"x\":-120,\"y\":-150,\"type\":\"OR\",\"source1\":6,\"source2\":-9},\"11\":{\"x\":120,\"y\":-150,\"type\":\"AND\",\"source1\":9,\"source2\":8},\"12\":{\"x\":0,\"y\":0,\"type\":\"AND\",\"source1\":10,\"source2\":11},\"13\":{\"x\":0,\"y\":150,\"type\":\"GOAL\",\"sources\":[12]}}").result
print(level_data.hash())
print(level_data2.hash())
print(level_data3.hash())
The output I get is:
137978820
2281966234
137978820
So the first and third are the same (the one from the JSON-File and the one from the JSON-String), but the second (hardcoded) is different and the first and third respond with an error when trying to read keys from the dictionary.
How could I:
- Compare those two correctly?
- Read the keys from the ones read from JSON?
Thanks in advance