• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

json_decode argument 1 incorrect type (undefined) expecting a Number (YYGR)

N

NoFontNL

Guest
I get the error:
json_decode argument 1 incorrect type (undefined) expecting a Number (YYGR)
I request with http_get using this code:
Code:
if (!requested) { // inside draw event
            http_get('http://gmscoreboard.com/handle_score.php?tagid=mytagid&getscore=1')
            requested=true;
}
And this is in the HTTP event (WebAsyncEvent)
Code:
if (!ready) { // inside HTTP event
    wrjson = async_load[? "result"];
       
            wrjsonlist = json_decode(wrjson);
            worldrecord = wrjsonlist[? "s1"];
            wrowner = wrjsonlist[? "p1"];
            ready=true
       
}
But sometimes, not all the time, but at some random moments, I get this error. Short version above:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Async Event: HTTP
for object obj_menu:

json_decode argument 1 incorrect type (undefined) expecting a Number (YYGR)
at `� (line 4) - wrjsonlist = json_decode(wrjson);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_menu_WebAsyncEvent_1 (line 4)
I never requested the http_get with another thing. Only the GM Scoreboard URL which is working sometimes, but sometimes my game crashes. What is the problem here??
 
T

The Shatner

Guest
Hey NoFontNL!
The Draw Event isn't really the best place to make GM do math... Maybe try moving this piece of code to the Step event.
I've had a similar issue recently and, despite the fact that it doesn't seem to matter much, using the Step event worked better than the Draw event.

I can also assume that you're missing something out, like...
Code:
http_get('http://gmscoreboard.com/handle_score.php?tagid='+string(mytagid)+'&getscore=1')
Instead of
Code:
http_get('http://gmscoreboard.com/handle_score.php?tagid=mytagid&getscore=1')
Give it a shot and tell us what happens...

If it doesn't work, please describe more of what you're attempting to do with the http_get function... Maybe there's an easier alternative to what you're attempting to do.
 
N

NoFontNL

Guest
Moved it. Still get the same error. It says it happens in the WebAsyncEvent. But I cannot figure out what the prob is...
When async, it waits until that function finishes right?
And if not, how can I 'wait' til wrjson is actually the result? Or am I again totally wrong? xd.
If it doesn't work, please describe more of what you're attempting to do with the http_get function... Maybe there's an easier alternative to what you're attempting to do.
Okay, I want to get the JSON formatted text, from that URL. Then it decodes the JSON and puts it in a ds_map. Then I grab the map, and find the value s1 (score 1st place) and p1 (player name 1st place). It's nothing because of that. Because it sometimes DOES work, but sometimes it doesn't. Pretty annoying, because my game musn't crash at all.
I think it is because it tries to decode the json before it actually loaded the json in the wrjson variable. But I cannot find any function to 'wait'. Please help if you can :)
 
T

The Shatner

Guest
Well... Theoretically, when using async, your object gets the information as soon as it's fetched, without locking the game in the same step (as would happen in the Step event). So maybe you could add a debug check to see what is happening... For example, exporting the data to an INI or TXT at all times before the json_decode (which is what's returning the error) and checking the text file out when it works and when it doesn't. That kind of poor man's log should tell you the difference...
But again, theoretically, if it works most of the time it could be a problem with the website you're fetching data from. Json expects a string to decode, but if the website is offline it could return something else, such as a 404 error, and json_decode doesn't know how to decode that. If this is the case, you can do (1) show an error message to the player ("unable to connect to the server") or (2) force the script to happen again until it returns something that json_decode can understand. Both failsafes should work, but (2) can be quite slow if the website is offline for a long period of time...
Give it a shot and tell us what happened.
 
N

NoFontNL

Guest
How would I check if It's different than JSON?
if (!wrjson.startsWith('{')) {}
doesn't seem to work in GML
 
N

NoFontNL

Guest
Okay, gonna use the second method, then set a time and if it hasnt got a value it says cannot connect. I will reply the result
 
N

NoFontNL

Guest
Alright, it returns undefined when crashing, but I can't check for a string to be undefined right?
 
N

NoFontNL

Guest
Oh, wait, must be a new feature. Couldn't check before the update...
 
N

NoFontNL

Guest
It works now. Thanks, I checked for undefined, and if so it tries again. After 5 seconds, if the site still doesn't respond, I've made a bit of code, so it says ingame that it cannot connect to the server.
 
Top