Grimmy's Forum Posts

  • Yes, that only works on PC. It doesn't do anything on mobile. God knows why not. :(

  • Still no luck. I even tried doing this script which works perfectly fine on PC but on device it always returns as connected..

    function NetworkTest(runtime)
    {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "GET", "https://www.mysite.com", true );
    	xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
    	xmlHttp.send();
    	
    
    	xmlHttp.onerror =function ()
    	{
    		runtime.callFunction("Change_Connection_Status",0);//NOT CONNECTED
    	}
    	
    	xmlHttp.onload =function ()
    	{
    		
    		runtime.callFunction("Change_Connection_Status",1);//CONNECTED
    	}
    	
    }
    
  • It seems like an on completed Ajax event gets run even when there is no internet connection. Is that right?

    I'm trying to make a reliable way to detect internet connection on device and I would have thought the Ajax On Completed request wouldn't trigger without a connection but apparently not.

    I am currently at a loss of how to detect internet connection on device. I've spent 2 days on this so far.!?!?

    Anyone know the answer? Thanks

  • I cant share the capx but the events to do with the predict function script above look like this:

    [PREDICT]

    | Local number Variable1‎ = 0

    ----* On function 'Do_Sensight_Request'

    ----* Parameter 'image' (String)

    -----> Functions: Call Hide Results

    -----> Functions: Call Reposition Last Photo

    -----> System: Wait for previous actions to complete

    -----> System: Set isDoingPrediction to 1

    -----> LastPhoto: Load image from UserMedia.SnapshotURL (Keep current size, cross-origin anonymous)

    -----> Functions: Call AnimateSnapshot

    -----> Run JavaScript:

    file_in= await runtime.assets.fetchBlob(localVars.image);

    predict(runtime);

    // we got the data now top the loader and update

    ----* On function 'Event_Got_Data'

    -----> Run JavaScript: GetResult(runtime);

    -----> JSON: Parse JSON string returned_data

    -----> System: Set isDoingPrediction to 0

    -----> Functions: Call Update Results

    ----* On function 'Update Results'

    -----> System: Set layer "Results" Visible

    -----> System: Set layer "Loader" Invisible

    -----> ResultText_1: Set text to JSON.Get("0.label")&" : "&JSON.Get("0.score")&"%"

    -----> ResultText_2: Set text to JSON.Get("1.label")&" : "&JSON.Get("1.score")&"%"

    -----> ResultText_3: Set text to JSON.Get("2.label")&" : "&JSON.Get("2.score")&"%"

    ----* On function 'Hide Results'

    -----> System: Set layer "Loader" Visible

    -----> System: Set layer "Results" Invisible

    Hopefully that should be enough to get you through..

  • None of these methods are working for me on device. I have 'always connected' regardless if I am actually connected or not and I'm doing an AJAX check to

    "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"

    every 2 seconds. (I tried various other websites too)

    Like everyone said the browser event doesn't work on a device and nor does the PlatformInfo.ConnectionRTT method work (always returns 0 on device).

    Regarding CORS if I add a header "Access-Control-Allow-Origin" "*" then I just get Not COnnected all the time.

    I also tried various plugins of which all had other compatibility issues.

    I would have thought this was a very commonly used feature in most mobile games, so I'm surprised there isn't an obvious solution.

    Where to turn next?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • SOLVED

    Even if nobody replies, I think that just the act of posting a question here seems to bring about some kind of mystical epiphany.:)

    In case anyone should ever need this; to change this to an asynchronous function which runs while your game is still running, it should be something like this:

    function apiPostRequest(request, body,runtime) {
    	var xmlHttp = new XMLHttpRequest();
    	
    	xmlHttp.open( "POST", baseApiURL + request, true );//TRUE indicates asynchronous
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	
    	xmlHttp.onload =function (e)
    	{
    		
    		console.log("Resp: "+xmlHttp.responseText);
    		//alert ("Resp :"+xmlHttp.responseText);
    
    		resp=xmlHttp.responseText;
    		results=JSON.parse(xmlHttp.responseText);
    		
    		runtime.globalVars.returned_data = xmlHttp.responseText;
    		runtime.callFunction("Event_Got_Data");//call event sheet function once complete
    
    	}
    
    	xmlHttp.send(body);
    	
    }
    
  • I'm currently doing a XMLHttpRequest which works fine but holds up the rest of the game until the data is returned.

    How would I keep the game running while the request is going on in the background?

    Current request looks like this:

    function apiPostRequest(request, body) {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "POST", baseApiURL + request, false );
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	xmlHttp.send(body);
    	console.log("Resp: "+xmlHttp.responseText);
    	
    	resp=xmlHttp.responseText;
    	
    	return xmlHttp.responseText;
    }
    
  • You do not have permission to view this post

  • SOLVED.

    Instead of calling

    GetResult();
    

    I need to call..

    GetResult(runtime);
    
  • Just to explain further in the most simple example:

    I call this function from a JS block..

    var resp;//this is a variable which gets adjusted with other script code
    
    function GetResult(runtime)
    {
    	alert("Looky here: "+resp);//This prints fine as expected! The resp is full of lovely data.
    	
    	runtime.globalVars.returned_data = resp;//The global value is not set in the runtime according to the debugger
    }
    

    and I get the error: TypeError: Cannot read property 'globalVars' of undefined

  • Its got this in the same script. Is that what you mean? ..or do I need something else?

    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    	
    });
    
  • After getting some data (xmlHttp.responseText) from the web I now need the result to be put back into my event sheet.

    My global variable is called 'returned_data' (its a string)

    I tried runtime.globalVars.returned_data = xmlHttp.responseText but my global variable will not change despite the fact that the alert with the same data displays perfectly, as does the console log with the returned data. I need to get the returned data back to the event sheet for further manipulation.

    Here is the JS script

    function apiPostRequest(request, body) {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "POST", baseApiURL + request, false );
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	xmlHttp.send(body);
    	console.log("Resp: "+xmlHttp.responseText);//PRINTS RESULT PERFECTLY
    	alert ("Resp :"+xmlHttp.responseText);//PRINTS RESULT PERFECTLY
    	
    	runtime.globalVars.returned_data = xmlHttp.responseText;//DOES NOTHING - Global ver is not changed
    	
    	return xmlHttp.responseText;
    }
    
  • SOLVED

    Through some miracle, dark magic and the process of elimination I seemed to have managed to get this working. Now i can take a picture with the camera and this image is uploaded to the Sentisight api for image recognition. A result is then returned to my device with the recognition data.

    For anyone who might need something similar in the future, here is how the successful call looks...

    The event sheet should read>>>>>>>>

    // do SentiSIght API request

    + UserMedia: On snapshot ready

    -> AJAX: Request UserMedia.SnapshotURL (tag "got_snapshot_data")

    + AJAX: On "got_snapshot_data" completed

    -> Functions: Call Do_Sensight_Request (image: UserMedia.​SnapshotURL)

    * On function 'Do_Sensight_Request'

    * Parameter 'image' (String)

    -> Run JavaScript:

    file_in= await runtime.assets.fetchBlob(localVars.image);//THIS LINE WAS THE KEY TO THE FIX

    predict();

    and then the predict function and POST request in a JS script looks like this:

    var file_in;
    const baseApiURL = "https://platform.sentisight.ai/api/";
    var token;
    
    function predict() {
    				
    
    	token = "XXXXX";
    	const projectId = "XXXX";
    
    	const modelName = "my_model_name";	
    
    	const file = file_in;
    
    	var fr = new FileReader();
    	fr.onload = function() {
    		results = JSON.parse(apiPostRequest('predict/' + projectId + '/' + modelName, fr.result));
    		console.log(results);
    		alert(results);
    
    	}	
    	fr.readAsArrayBuffer(file);
    
    }
    
    function apiPostRequest(request, body) {
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "POST", baseApiURL + request, false );
    	xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    	xmlHttp.setRequestHeader('X-Auth-token', token);
    	xmlHttp.send(body);
    	console.log(xmlHttp.responseText);
    	alert ("Resp :"+xmlHttp.responseText);
    	return xmlHttp.responseText;
    }
  • You could do something like this...(pseudo code)

    every tick--

    current_viewport_width = Viewpoint_Width

    if current_viewport_width != old_viewport_width

    --do stuff

    old_viewport_width=Viewpoint_Width //and set the old width to the new one

  • I'm still no further along with this. I seem to have hit a dead end. Here is the api js function i want to call: (Its an image recognition api that i want to upload a camera grabbed image to)

    const baseApiURL = 'https://platform.sentisight.ai/api/';
    			var token = '';
    			var predictionId;
    			var results;
    
    			function predict(my_file) {
    				token = "some token"
    				const projectId = "some id";
    				const modelName = ""some name";
    				
    				const file = my_file.files[0];
    				var fr = new FileReader();
    				fr.onload = function() {
    					results = JSON.parse(apiPostRequest('predict/' + projectId + '/' + modelName, fr.result));
    					console.log(results);
    				}
    				fr.readAsArrayBuffer(file);
    			}
    
    			function apiPostRequest(request, body) {
    				var xmlHttp = new XMLHttpRequest();
    				xmlHttp.open( "POST", baseApiURL + request, false );
    				xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
    				xmlHttp.setRequestHeader('X-Auth-token', token);
    				xmlHttp.send(body);
    				console.log(xmlHttp.responseText);
    				return xmlHttp.responseText;
    			}

    I'm trying to pass in a file to predict(my_file) but it just wont work.

    In my event sheet i have a function that runs a piece of JS: predict(localVars.image);

    Most of the time I'm just getting:

    Failed to execute 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'.

    or

    TypeError: Cannot read property '0' of undefined

    And this is my event sheet :

    PS..This should probably be moved to the scripting forum.