Send message back to sender

So I think I did everything right, but somehow it is not working.

In the basic_ai.script which belongs to gameobject ai_character I call this line:

msg.post("level_controller#script", "get_new_target", {my_id = go.get_id()})

So the message should go to the gameobject level_controller and from there to the script. This script also created all ai_characters in my game. The on_message function looks like this:

local get_new_target_hash = hash("get_new_target")

function on_message(self, message_id, message, sender)
    if message_id == get_new_target_hash then
    	local new_target = get_new_target(self, message.my_id)
    	msg.post(sender, "new_target", {target = new_target})
    end
end

So the ai_character asks the level_controller for a new target (another ai_character he can go to). Now the level_controller should send a message with the new target back to the ai_character, but that somehow doesn’t work.

on_message of basic_ai.script:

function on_message(self, message_id, message, sender)
    if message_id == new_target_hash then
    	if self.state == looking_for_opponent then
    		self.state = moving
    		self.target = message.target
    	end
    end
end

The ai_character never reaches the state moving. I’m already trying the whole day to find out what I’m doing wrong, but got no further. Am I missing something?

My Level Collection:

Another Question: When I create a gameobject with factory.create() an id will be returned. Is this the same id as the one that I will get, when i call go.get_id() in a script in the created gameobject?

Have you tried putting in print() statements to trace if the message reaches the level_controller alright?

Second question. Yes, that is the same id.

Thanks for helping :slight_smile:

I had this issue Defold engine builds but doesn’t launch (SOLVED) so print statements didn’t help. But now since it is solved, I could use them again and you’re right. The messages never reached the level_controller. I had to change it’s url to level/level_controller#script . I guess because the factory of level_controller didn’t create the ai_characters in the level collection, but directly under main.

I’m so happy that the normal launch works again for me and I’m able to see Debug messages again :slight_smile:

Good to hear that you managed to find the problem!

As a side-note: If you’re working on HTML5 and need to do some print() debugging you’re still able to replace the standard print() function or use another function and send text to a gui scene with a text node.