Get object position in other collection (SOLVED)

I need get position player from my enemie script, I get the current position with this method:

function init(self)
	self.id = "/level1/player/player"
	print(go.get_position(self.id))
end

level1 is my collection for the level and player is other collection and player my object, the problem is when I make a new collection with name level2 or level3 etc.

how can access to player in diferent levels from my enemie script?

sorry for my english.

You do not need to be that specific with the url of the player. What you’re using is an absolute URL to the player game object. If both the player and the enemy are created/exist in the same place in the hierarchy of game objects it should be enough use a relative URL, like this:

go.get_position("player")

The concepts of relative and absolute urls are described here: https://www.defold.com/manuals/message-passing/

1 Like

I can not get the object, please check my image

https://drive.google.com/open?id=0B8ayXJxIPt2yZDNtUFRkTkJMSTA

my code is in “script1”, marked in image and I get the position for player .

in script1:

function init(self)
	self.id = "player"
	print(go.get_position(self.id))
end

and get:
ERROR:SCRIPT: /main/Enemies/enemiefire.script:3: Instance player/player not found

With the following setup:

I can do print(go.get_position(“/foobar/player”)) to get the position of the player game object.

In your original question you had an additional “level1” in the URL but I cannot see that in the screenshot that you shared. If you have multiple levels the common practice is to have each level in a separate collection and reference them from collection proxies and only load the current level.

“level1” is the top collection and I put level1 collection in main collection.
following your example i get this print(go.get_position(“/player/player”)) but is not working, I put my code in script1 in enemie1.

print(go.get_position(“/level1/player/player”)) work great, but if I have an other collection with level2 or leve3 my code not working

Do you really want all of the levels loaded at once? Or do you want to load one level at a time?

If you actually want to have all levels (ie collections) loaded at once then yes, you need to explicitly define the full path to the player game object (/level1/player/player). In that case you need to somehow be able to tell the script which path to use. You can use this via message posting or via a Lua module.

If you only want one level loaded at a time then stick all of the levels into collection proxies, load the level you wish to play and use the /player/player URL to the player game object.

2 Likes

thanks, I will use this method, now is working like a charm.

2 Likes