Grimmy's Forum Posts

  • 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! :)

  • I can confirm that this doesn't work on device (Android). I had a dialogue box and a couple of grid scrollers which work perfectly in preview but when exported to a debug.apk would hang on the start.

    I deleted the ProUI plugin AND all instances of the grid scroller and dialogue box so there was no longer any trace of ProUI and the app runs fine again. I made no other changes to the project, infact I spent all day with this initially deleting all other plugins to track down the issue. ProUI was the last thing I removed.

    The only error I got while remotely debugging on device was:

    Uncaught (in promise) TypeError: Cannot read property '0' of undefined
     at C3.Runtime._LoadDataJson (scripts/c3runtime.js:454)
     at C3.Runtime.Init (scripts/c3runtime.js:454)
     at async f._InitDOM (main.js:5)
     at async f._Init (main.js:5)
    

    I think that's pretty vague, but it might help you.

    Hopefully you can fix this issue soon!!

    Let me know! Thanks

    Simon

  • I just had a similar issue. When it wouldn't install my projects settings were...

    ID: com.ruleoffun.test
    Email: I had none set
    
    

    but then I changed them to..

    ID: com.ruleoffun.someothername
    Email: simonxno@myactualemail.com
    

    ..and now the app installed fine.

    Not sure if it was a random fluke but something else to try if you're having the same issue..

  • Has this been tested on an Android APK on device? It should work right? I installed it the other day but now on my device the app freezes at startup.

    It 'may' be this plugin (I hope not) but I wondered if you could confirm that it 'should' work before I start deleting things.

    Cheers