So I’ve scratched my head now a day or two over this designproblem.
I got a collection of several gameobjects that I want to communicate to each other at init.
Problem is that this collection is dynamically created with a collectionfactory and then I don’t know the full id of any of the gameobjects.
Sure in one gameobject I can use msg.url() and get the full path for that but as I understand it on a release I wont be able to “parse” that hash to a string and retrieve the collection id so I can use it on other gameobjects urls?
How can one gameobject in a dynamically created collection know the url to the others in that collection?
I know I get all hashes in a table when using collectionfactory.create but then it’s too late. I want to be able communicate at init.
My workaround right now is to skip the init part and send a message to one gameobjects from the “creating script” with all the hashes and then when recieved I will have my own init function being handled when message with all hashes is received. But it’s not very friendly as the views will already be created and flickering weirdly on screen.
2 Likes
Not even the workaround worked as planned as I cannot send in the table of hashes into the gameobjects with a normal message as I am not allowed to use hashes as keys in a table… Would like to find a decent solution to this.
Can’t you use relative URL’s? Example:
Collection
a.script
function init(self)
msg.post("b#script", "hello")
end
b.script
function init(self)
msg.post("a#script", "hello")
end
This should work for any collection, regardless of how it came about (subcollection, collectionfactory, proxy, etc).
thing is that it’s path hash is no longer “b” but “/coll_id/b” as I understand it. And as I don’t know the collision_id i cannot create "/coll_id/b#script"
If I only use “/b#script” it cannot found that gameobject
The automatic resolving of relative URL’s should handle this for you. In the above example, if you in a.script#init instead do
print(msg.url("b#script"))
and in b.script#init you do
print(msg.url())
they should print the exact same thing. The engine resolves the two URL’s for you. If you start the URL with a slash ("/b#script") it becomes absolute, and it won’t be resolved.
The section “Absolute and relative paths” in the manual describes this concept.
Really doesn’t seem to work as it doesn’t resolve the URL at all.
I set up a quick test project:
Same script on both gameobjects:
function init(self)
print(msg.url())
print(msg.url("/go_b"))
msg.post(msg.url("/go_b#script"),"test")
end
Endresult on console:
DEBUG:SCRIPT: url: [main:/coll/go_a#script]
DEBUG:SCRIPT: url: [main:/go_b]
DEBUG:SCRIPT: url: [main:/coll/go_b#script]
DEBUG:SCRIPT: url: [main:/go_b]
ERROR:GAMEOBJECT: Instance ‘/go_b’ could not be found when dispatching message ‘test’ sent from main:/coll/go_a#script
ERROR:GAMEOBJECT: Instance ‘/go_b’ could not be found when dispatching message ‘test’ sent from main:/coll/go_b#script
In this case I would of course know that the collection is named “/coll” and could add it myself to the url. But in dynamic creating it I don’t know and cannot parse it as a string from msg.url() hash as that wont work in release mode.
ok. Solved.
Yes you are right Ragnar and I should learn to read better.
having that forward slash “/” appearently is the difference.
code should be:
function init(self)
print(msg.url())
print(msg.url("go_b"))
msg.post(msg.url("go_b#script"),"test")
end
dumstrut på
/A
2 Likes