CallJS.hx with data

Hello,

I’m trying to send player coordinates to JS from haxe and can’t seem to get it working. I have tried variations of the below code and nothing seems to work.

package arm;

import iron.Trait;
import iron.object.Object;
import iron.object.Transform;

class UpdatePlayerTrait extends iron.Trait {

	

	public function new() {
		super();


		notifyOnUpdate(function() {
			untyped __js__('window.updatePlayer();');// works
			untyped __js__('window.updatePlayer('+object.transform.x+');');//doesn't work
		});
	
	}
}

another example

package arm;

import iron.Trait;
import iron.object.Object;
import iron.object.Transform;

class UpdatePlayerTrait extends iron.Trait {

	public function new() {
		super();


		notifyOnUpdate(function() {
            var test:String = 'window.updatePlayer('+'"'+object.name+'"'+');';
		    trace(test); // logs window.updatePlayer("Scene");
		    untyped __js__('$test');//doesn't work
            untyped __js__(test);//doesn't work
		});
	}
}

Thanks in advance for any help.

What doesn’t work exactly? Have you checked the console?

Have you seen the wiki doc for javascript https://github.com/armory3d/armory/wiki/js ?

The browser console is giving the below error at the very end of the kha.js file and is not helpful. The console in blender never seems to output anything. I used the wiki link you provided as a starting point and got both of them to work. As soon as I try to get actual data from the scene to the browser even just a static string it no longer works.

Uncaught SyntaxError: Unexpected end of input

I added another very simple example of just trying to pass the object name through and it also doesn’t work and gives the same error in browser console.

I figured it out, see solution below.

package arm;

import iron.object.Object;
import iron.object.Transform;

class UpdatePlayerTrait extends iron.Trait {

	var trans:Transform;

	public function new() {
		super();

		notifyOnInit(function() {
			trans = object.transform;
		});

		notifyOnUpdate(function() {
            var method:String = 'window.updatePlayer("${trans.loc.toString()}");';
			untyped __js__("(1, eval)({0})", method);
		});
	}
}
3 Likes