Collection proxy not reacting to Input Messages passed from Main.collection [SOLVED]

Hello there!

Short background:
Basically I have found defold a few days ago - bonafide newbie here - , I have created a main collection and created a label object, set up input_bindings (Space, h, mouse_left, mouse_right) and it took me an hour to get the label to change content base on the inputs. Started to make a rudimentary text game and all was fine and working 100%, but I run into the issue, that I could not restart my game when it ended >> parsing the defold manual I found out about collection proxies that can be reset (load/unload levels) and it was the perfect solution for my problem.

Following the manual I have created a new collection, a collection proxy, moved all objects and code over to the new collection from main, set up proxy loading, message passing and after a while I managed to load the same stuff, but now via a proxy. Hurray!

And now I can not make the input messages pass between the main and the proxy.
After reading trough this forum I have found and added the debug code that solved someones misnamed proxy/game object Id problem, but I found that I have no such misnaming in my code.

Based on the defold manual and the defold teams video ‘The basics of addressing and message passing’ all the code seems to be correct.

Can you help me what am I missing/doing wrong - why does the code not work?

Win10_x64 - HTML5 project
I have uploaded the project to git:

Thanks in advance!

Hi!

I guess some of your problems come from refactoring the code. Right now main.script is trying to capture the input in the on_message function, while it has to do that in the on_input function, like so:

function on_input(self, action_id, action)
	if action_id == hash("spacebar") then
		msg.post("TitleScreen:/go#TitleScreen", "spacebar")
	end
end

Then, after you’ve passed it to the other collection, you have to use the on_message function in TitleScreen.script to receive it. That function is already there, but you’re trying to compare action_id in it, while you have to use message_id instead.

An alternative solution is to just listen to the input directly in TitleScreen.script without having to send a message from main.script. In that case, just remember to add

	msg.post(".", "acquire_input_focus")

to your init function in TitleScreen.script first.

1 Like

Thanks so much,

I have updated the code - also on github - based on your first suggestion, and it’s kinda working, but not fully.

Let me explain:
The message is passed, but the action component is not passed and I had to remove those lines from the code. So now I have a key press information, but can not differentiate between a pressed or released action.

On your second suggestion as to moving the input focus to the proxy script, in the ‘Caveats and Common issues’ section of the defold manual (Collection proxy manual) there is a section on Inputs stating:

“If you have objects in your loaded collection that require input actions, you need to make sure that the game object that contains the collection proxy acquires input. When the game object receives input messages these are propagated to the components of that object, i.e. the collection proxies. The input actions are sent via the proxy into the loaded collection.”

This is what I have experimented with before I even resorted to the - kinda manual - message passing, but it would not work. This above defold manual text implies add - msg.post(".", “acquire_input_focus”) - and it just works™, but alas, nope… :slight_smile:

I have tried all variations of in which script I put the - msg.post(".", “acquire_input_focus”) - and both on_input/on_message functions to try to capture the inputs, but based on the above the acquire clearly should go to the main.collection and logically in the proxy the on_input function should be used, still the propagation did not happen.

If you want the information from the input action to be sent with the message, you have to pass that information along:

msg.post("TitleScreen:/go#TitleScreen", "right", {x = action.x})

and then read it like this

if message_id == hash("right") then
	print(message.x)
	label.set_text("#PressSpace", "right mouse clicked")
end

But when it comes to the solution of using on_input in TitleScreen.script instead, does it not work if you just add

function init(self)
	msg.post(".", "acquire_input_focus")
end
function on_input(self, action_id, action)
	print("HELLO")
end

in the top of TitleScreen.script? That worked for me anyway using your latest push.

msg.post(".", "acquire_input_focus")

has to be in both main.script’s and TitleScreen.script’s init functions.

3 Likes

Yaaay! Success!

I went with option two, and did the double acquire and use on_input in proxy method.

Thank you so much! I have spent hours on this and felt a bit of a let down that I could not figure it out, but now I am reinvigorated, so after a good 12 hours of much needed sleep, I will hop back on the defold train.

Updated the code on github.

Thanks again!
P.S.: Next project >> figure out how to mark this topic SOLVED…

2 Likes

I’m glad to hear it worked!

You can edit the post title yourself. There’s no button or anything which automatically adds SOLVED :slight_smile:

2 Likes