doc icon indicating copy to clipboard operation
doc copied to clipboard

Clarify and highlight terminology relating to URLs

Open britzl opened this issue 2 years ago • 1 comments

See https://github.com/defold/defold/issues/9804#issuecomment-2504332119

britzl avatar Nov 29 '24 09:11 britzl

Hi, I read the related issue, and I think the explanation in this comment ( https://github.com/defold/defold/issues/9804#issuecomment-2501925055 ) is the best explanation of what msg.url() really is!

I was struggling with it too, because I was using msg.url() at the start of scripts (at script scope, outside of functions) to store my URL in local variables, not in the init() function. I thought it was better than using hash() values (it is stated in the doc somewere). BUT at this time, the objects we are referring to in the msg.url() arguments does not exist yet, so we can only use ABSOLUTE url strings, like msg.url('socket:path#fragment')!

As soon as you use a kind of "relative" URL like msg.url(nil, 'path','fragment') or msg.url('path#fragment') or msg.url('#fragment'), you obtain a nil value! That's because, and this is really not clear in the doc, msg.url() try to resolve to an absolute location at the time of function call!, not at the time where we USE the returned value of it. So it works in init() function, because the objects can be resolve at time of calling, but not in the scope of the script or in a lua module.

So you cant do "local URL_SPRITE = msg.url('#sprite')" and use this exact URL_SPRITE variable in many objects thinking "this url localize a component named 'sprite' " each time you use it. NO, this URL_SPRITE localize THE component name 'sprite' in this particular GO in this particular collection where we called msg.url() function.

I don't know how to put it in the doc, but it should be there!

mathias-a-la-basse avatar Mar 15 '26 21:03 mathias-a-la-basse