Ghost Script Not Removing Itself


#1

I have a sandbox running on a Windows machine. I created a Spawn Entity script with the following code. It uses the Shortbow tutorial assets.

var userData = {
	"grabbableKey": {
		"grabbable": true
	}
};

var id = Entities.addEntity({
	"position": MyAvatar.position,
	"collisionsWillMove": 1,
	"compoundShapeURL": Script.resolvePath("bow_collision_hull.obj"),
	"dynamic": 1,
	"gravity": {
		"x": 0,
		"y": -9.8,
		"z": 0
	},
	"modelURL": Script.resolvePath("bow-deadly.fbx"),
	"name": "Hifi-Bow",
	"script": Script.resolvePath("touchEvent.js"),
	"shapeType": "compound",
	"type": "Model",
	"userData": JSON.stringify(userData),
	"lifetime": 600
});

print ("spawnEvent.js v3 loaded");

Here is the touchEvent.js code that is referred to from above…

(function() {
	this.mousePressOnEntity = function(entityID, mouseEvent) {
		var attachment = {
			modelURL: Script.resolvePath("bow-deadly.fbx"),
			jointName: "LeftHand",
			translation: {"x": 0, "y": 0.020, "z": 0.030},
			rotation: Quat.fromVec3Degrees({"x": 0, "y": 90, "z": -90}),
			scale: 1,
			isSoft: false
		};
		
		MyAvatar.attach(
			attachment.modelURL,
			attachment.jointName,
			attachment.translation,
			attachment.rotation,
			attachment.scale,
			attachment.isSoft
		);
		
		Entities.deleteEntity(entityID);
	};
	
	Controller.keyPressEvent.connect(function(event) {
		if (event.text === "SPACE") {
			print("FIRE!");
		}
	});
	
	print("touchEvent.js v16 loaded");
})

Once spawned, I click the spawned item to attach it to me. I then press the SPACE key to verify it’s processing the keyboard event and it does.

I then open the Avatar Attachments dialog and delete the attachment.

For some reason, when I press SPACE key, I still get the FIRE! message. It’s acting like the script from the attachment didn’t remove itself from the system. I verified that the entity doesn’t show up in the Entities List in Edit Mode and even refreshed it. If it’s removed from my avatar, shouldn’t it be fully removed from the system?


#2

From what you’re describing, it seems like this might be related to a bug we have where entity scripts aren’t loaded properly. This PR: https://github.com/highfidelity/hifi/pull/9879 looks like it should fix this behavior.


#3

Thanks, Livi! I’ll give this a try later today and post my results.


#4

I just tried out the PR release you linked and unfortunately it didn’t help.


#5

Which executable did you try from the PR? I just grabbed the Windows installer and can test with your script this afternoon.


#6

http://cachefly.highfidelity.com/HighFidelity-Beta-PR9879-930c165.exe


#7

I’ve created a video of my issue. If you go fullscreen, you should see better detail of the video. If the video doesn’t load into the page, you may need to refresh the page. The video embed is a little weird at times. Maybe a Dropbox thing. I’m working on compressing the video better just in case.


#8

Played around with it a bit - I think that what’s happening is that the Controller API doesn’t automatically disconnect the function for firing when the Entity is deleted. I’m digging into this in more detail, but I think what would work is specifying an entity unload function that undoes the Controller key press mapping when the entity is deleted.
You could use Disable Mapping (https://wiki.highfidelity.com/wiki/DisableMapping()) or call Controller.keypressEvent.disconnect to remove it when the entity is deleted.

I will post an example tomorrow AM!


#9

Ah! I gotcha. I’ll try implementing that and see how it goes.


#10

Ok. Got it to work when deleting the entity in edit mode. Here’s the bit of code…

function myKeyPressEvent(event) {
	if (event.text === "SPACE") {
		print("FIRE!");
	}
}

Controller.keyPressEvent.connect(myKeyPressEvent);

this.unload = function() {
	print("Unloading...");
	Controller.keyPressEvent.disconnect(myKeyPressEvent);
};

Eventually I’ll get it working with the whole attach/detach from avatar process. Unload doesn’t seem to get called when detached from avatar. So, I’m guessing there’s another event I need to handle like .onDetach or something. I’ll look into it some more in time. Thanks!

I think it would definitely be great if scripts (along with their connects, etc) were garbage collected automatically by the system at the point in which they are absolutely no longer required. Much like when you close the Web browser, any JS scripts running no longer run and are irrelevant. Unless there is some reason why it should be handled manually by scriptors that I am not aware of.