How to compare hash and url

I need to delete a value in a table inside enemy_spawner.script
local enemys = {hash("/Instance0")} <---this guy

First thing first a factory created game object(aka. enemy.go) send a msg to a enemy_spawner.script
msg.post("/enemy_spawner", hash("enemy_destroyed"))

enemy_spawner.script recives this msg and then checks every element in a enemys table and compare is it the guy we want or not.

local enemys = {--there are lots of enemy here like 20 of them}
function on_message(self, message_id, message, sender)
	if message_id == hash("enemy_destroyed") then
		for i,p in ipairs(enemys) do
			print(p, sender)
			if p == go.get_parent(sender) then --Here's the critical line--
				print("YESSS BABYYYYYYYYYYYYYYYYYY")
				print("Destroy the ",enemys[i])
				table.remove(enemys,i)
			end
		end
	end
end

and then the console output is this:
DEBUG:SCRIPT: hash: [/instance1] url: [main:/instance1#enemy]
So this means the script doesnt find any equivalent.(whıch is imposible because every enemy inside this table)
The problem is i dont know how to compare a hash and a url.

You can use sender.path to get the game object id as a hash:

1 Like

Thank you Very Much!

And to make clear why your solution did not work, if you use go.get_parent() on main:/instance1#enemy, you will not get main:/instance1 as a parent of enemy, as enemy is not a child object, but a component of the object. So the object you are addressing with main:/instance1#enemy is main:/instance1, and go.get_parent() will try to find a parent of instance1, which is nil if it does not have a parent.

So keep in mind the difference between objects and components.

2 Likes

I will