Defold Input - Invalid cursor script url

Hello,
I’m using Defold Input to create a card game, with simple drag and drop, but I can’t have the listener to work, to prevent dragging some objects.

This :

cursor.listen(msg.url("#cursor"), cursor.DRAG_START, function(message_id, message)
	if message.group == msg_card then
		return true -- return true to block the event
	end
end)

returns an error : “You must provide a valid cursor script url”.

I tried to set it up exactly like the drag and drop example, and call listen() in the init() function, but it doesn’t work.
I have a “game” object, with the cursor script (#cursor) attached, and a main controller script (#game).

All other events and messages work fine. Only the listener doesn’t.
Does anyone have an idea ?
Thanks a lot !

Note that msg.url("#cursor") must resolve to the URL of the cursor.script. In your snippet of code it is assumed that the the script you are calling cursor.listen() from is attached to the same game object as the cursor.script.

If the two scripts are not on the same game object then you need to specify a full url with both game object id and script id.

Hello,
Thanks for your quick answer.
Yes, it is on the same game object.

That’s why I don’t understand why it doesn’t work.
The URL looks valid.

The listen() call is in game.script init() function.

My guess is that the game.scriptis simply initialized before the cursor script, which could be a reason why the script is throwing an error.

From the Defold application lifecycle manual :

The order in which game object component init() functions are called is unspecified. You should not assume that the engine initializes objects belonging to the same collection in a certain order.

A really hacky solution that might work is to send a message to itself on init and then run your code upon receiving that message.

Yes, this is very likely to be the reason! Nice catch!

1 Like

Hello !
Thanks for your tip !
That was the issue ! I replaced the snippet with a msg.post(".", “listen”), and handle the listen() call in the message handler, and it worked.

So, @britzl , you’ve been lucky the examples in Defold-input work, right ?

First I assumed the scripts were loaded in the same order as in the hierarchy, but that’s not the case. Renaming the controller script “a.script” didn’t make a difference.

Anyway, it works now !
Thanks both for your help !

Yes. :four_leaf_clover: Will fix this now :slight_smile:

Fixed so that there is no need to call init() before listen():

5 Likes

Thanks a lot !