Checking if a url exists

I would like to be able to determine if a certain url exists or not so I could do something like:

local object=factory.create("some:/object_factory")
if msg.url_exists(msg.url(nil, object, "sound")) then
  msg.post(msg.url(nil, object, "sound"), "play_sound)
else
  msg.post("default:/sound", "play_sound")
end
3 Likes

Can you elaborate the use-case a bit more? It seems like you would always know what you are spawning and thus know if the object has a sound component, right?

In this short code, yes, but in reality I might spawn a level that could or could not have its own music for instance and then from a separate script want to play that music if it exists.
I could also use the check somewhere in code where I am not sure wether the game object in question is deleted or not (I am aware that the object may be deleted between the check and the message pass, but it would still make some errors less frequent)

In your particular usecase I would prefer to use a central game object that control music that level specific music players can register to (at init). I.e dispatch on reciever side and not on sender side.

However, there are other, trickier use-cases where we see the benefit of a go.exists() function, so you would be able to query if a game object exists. This has been requested before and I’ll see if we can add it to the backlog.

Yes I agree, the meaning was to provide a simple example - the problem is simple examples often have simple solutions :slight_smile:

1 Like

The times when I’ve come to a situation where I need to check if a specific URL exists or not, I’ve always come to the conclusion that there’s another (and better) way of achieving what I want. But at the same time I guess a go.exists() isn’t such a bad thing.

2 Likes

Possible use case:

Constructing a URL from configurable parts so that a designer can fiddle with values in a config file. e.g. similar animations with different frame rates could be constructed with a name and an fps (“anim15”, "anim30’)

It would be useful to be able to test for the existence of an anim before trying to play it and resort to a default if it does not exist.

Any update on the status of this in the backlog?

What’s your use case? There are many ways to deal with this. Such as keeping an index of URLs in a shared module.

I have a shared loading sequence that sends some activation messages to a scene, but rather then broadcasting blindly into large collections, it targets an object with a specific name. Some scenes don’t have this object, so rather than printing an error, I’d like to check first.

I guess having an index of likely scenes would work, it just seems tacky.

I faced the same issue.
I decided to reuse existing game object in another part of the project and my original object send message to gui (update some values) on new location i don’t have this object. and as quick hack i planned to check is that object exist or not.

1 Like

I really think this indicates a problem with the design and structure of the code. If you need some data from an object then you need to keep track of it somehow and also notify any interested party when the object in question is removed. If you could describe your specific problem, in as much detail as possible then maybe we can suggest a way to structure your data and objects in a way that makes it easier for you to do what it is you wish to do.

Can you put the GUI in a known position and use absolute urls? Like “/interface#gui”?

Oh, sorry you mean there is no GUI available to receive the message? Then I agree with @britzl that you might want to consider your structure.

Hi.

Today i see that go.exists(go_id) really needed. In my case tracking of some different types of GO is a way to complex code. New func go.exists() will allow significatly accurate code.

go.exists(go_id)
return
go_id - if game object exist than return same go_id
nil - if game object absent or already killed

Thx

2 Likes

Did you try something like this? It doesn’t have to be complex, just add objects to a list on init and remove them on final.

You can also use pcall.

pcall(go.get_position, "not_an_id")
2 Likes

Yes, i see nothing complex in tracking instantioned objects at init or from fabric. But, different GO types has not direct relations with other GO objects - he triggered by timer, by collision and other logic events. This is simple objects with short life time. So i need place tracking + die code logic in many places. Yes, currently this approach implemented and working fine.

But i want try second solution - store “short life time” objects in one array and iterated&check if object alive then do something (such objects killed by collision clipping group, just clarify). So i need method like go.exist() …

Yes, i see pcall feature, but i feel (supernatural feel :D) that this method is too costly in runtime. Have you information about pcall performance?

Thx.

PS I do not have similar problems with the code just discuss feature request. thx.

2 Likes

Ah, good point.

I did this quick iteration test, and pcall actually took the same time as the normal function call. I do share your feeling that it must cost something extra though, maybe it is in some other way or only shows up in “real world” circumstances.

2 Likes

Ok, thx for investigation!