Skip to content

Array find and has api don't work when data from json #29513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zszen opened this issue Jun 5, 2019 · 6 comments
Closed

Array find and has api don't work when data from json #29513

zszen opened this issue Jun 5, 2019 · 6 comments
Labels

Comments

@zszen
Copy link

zszen commented Jun 5, 2019

Godot version:
3.1

OS/device including version:
MacOS, internal runing test

Issue description:
I save config to local with json and load it back next time. I found that the array object can't check unit with "find" or "has" api

Steps to reproduce:

  1. read json file from user:// path and parse success.
  2. get an array from dic which from jsondata.result
  3. check if the array contain some data
  4. result is always false or return -1
    ...
  5. I then do manual loop check, the value can compare and can check it exist

Minimal reproduction project:
right code:

 if not dic.has(key):
	dic[key] = []
var arr:Array = dic[key] as Array
var isContain:=false
for k in arr:
	if k==value:
		isContain = true
		break
if not isContain:
	arr.append(value)
	saveData()

wrong code : (always save the same data)

 if not dic.has(key):
	dic[key] = []
var arr:Array = dic[key] as Array
var isContain:= arr.find(value)>=0 # arr.has(value)
if not isContain:
	arr.append(value)
	saveData()

dic defined:

var dic:Dictionary = {}

func _ready():
	saver.open("user://savegame.save", File.READ)
	var data = JSON.parse(saver.get_as_text())
	saver.close()
	if data!=null && data.result!=null:
		dic = data.result
@zszen
Copy link
Author

zszen commented Jun 5, 2019

maybe I save the number , it read back to string ?

@zszen
Copy link
Author

zszen commented Jun 5, 2019

I wrote a custom 'has' api:

func arrContain(arr:Array, value)->bool:
	var type = typeof(value)
	for k in arr:
		if typeof(k) == type:
			if k==value:
				return true
	return false

@Calinou
Copy link
Member

Calinou commented Jun 5, 2019

I save config to local with json and load it back next time.

You should use the ConfigFile class to store user configuration, it's specifically intended for that purpose. JSON can do it, but it's simpler with ConfigFile 😃

@zszen
Copy link
Author

zszen commented Jun 5, 2019

@Calinou thank you very much <3

@zszen zszen closed this as completed Jun 5, 2019
@KoBeWi KoBeWi added the archived label Jun 5, 2019
@KoBeWi
Copy link
Member

KoBeWi commented Jun 5, 2019

The problem was probably that any number parsed from JSON is a float and you used find/has with integer.

@zszen
Copy link
Author

zszen commented Jun 5, 2019

@KoBeWi
After restart the app. the Array api is works well. Maybe cause by app memory leak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants