Grimmy's Recent Forum Activity

  • 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.

  • I am trying to use an external api that sends an image file to a url but I hve no idea how I can refernce a file from construct to the javascript function.

    The js function has something like this:

    var input = document.getElementById('file_in');

    const file = input.files[0];

    var fr = new FileReader();

    ..and then goes on to perform an api call with this data.

    However, I have no idea how to pass a (binary?) file into this js script from the event sheet. Seems I can only pass in strings and numbers(?)

    ..or If its even possible at all.

    Any pointers greatly appreciated.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In my case data.json was a file that I had created manually and put in my files directory. Maybe you speak of another data.json file that lives somewhere else?

  • Something like this?

    CONDITION
    
    If I am moving..
    
    	SUB CONDITIONS
    		..and i am in car..
    			..do this..
    		..and i am on a bicycle
    			..do this..
    
  • In the end it was because I had a file named data.json which seemed to be causing the failure to run on device.

    Once i removed that..everything was fine.

    Naming clash?

  • Apologies to Aekiro. In the end the issue I was having had nothing to do with ProUi.

    That fact that I had a file named data.json seemed to be causing the failure to run on device.

    I can confirm that ProUI is actually very awesome and so to is the support! :)

Grimmy's avatar

Grimmy

Member since 20 Nov, 2012

Twitter
Grimmy has 2 followers

Trophy Case

  • 12-Year Club
  • Forum Contributor Made 100 posts in the forums
  • Regular Visitor Visited Construct.net 7 days in a row
  • RTFM Read the fabulous manual
  • Great Comment One of your comments gets 3 upvotes
  • Email Verified

Progress

17/44
How to earn trophies