Getting position of another moving game object

Scenario:
When my player character attacks, I create an attack GO with a factory which spawns from my character. I’ve got a collision object on the attack GO and a temporary sprite so I can see where it’s at. My desire is to get the attack to move with the player if the player is moving / jumping so that the collision path is correct.

Problem:
When grabbing the position for the player (either via go.get_position or go.get_world_position), the position appears to remain constant for the duration of the attack’s life. If a new attack is started, the position updates, but for the duration of the attack the x and y remain constant even if the player moves.
I’ve tried using the same code to grab the player’s position in the player script itself (in the update() function) and this updates constantly fine. Am I missing something in regards to how the spawned attack works? Does it grab a snapshot of the world at the time it was spawned or something? My debug code for reference:

local obj = go.get_world_position("#hero")
local obj2 = go.get_position("#hero")
print("World position: " .. obj.x)
print("Position: " .. obj2.x)

No, you will get accurate positions with go.get_position(). How do you fetch the player position in the spawned object’s script?

That code at the bottom of the post was used to fetch the position both in the player script (just testing) and the spawned object. For the spawned object it was only accurate at the time of spawning the object - almost as if it was grabbing its own position… perhaps I’m referencing the player GO incorrectly. Here’s the folder structure:

Am I referencing hero.go incorrectly with go.get_world_position("#hero") from atk1.script?

The folder structure has nothing to do with how you interact with game objects in the game. The way game objects are arranged in the Outline is what matters (and remember that you can rearrange your files any way you want to and all references to files will be updated automatically).

The “#hero” URL implies that there is a component with id “hero” on the same game object as the script that is doing msg.post. I suggest that you read up on addressing in this manual.

2 Likes

Ah, I see. I’m not sure how it’s even resolving a component ‘hero’ on my attack GO then, but I’ll keep trying here to see if I can correctly reference my player GO.

From reading, I would expect “hero” to resolve the correct thing here but that gets me “Instance hero not found”. Regardless, I’ll keep trying a few things.

If you switch to editor 2 you’ll get some additional help to figure out the URLs to use. If you select something in the Outline it will show the URL in the Properties panel. Like this:

2 Likes

Cool! Switched to the new editor, and with the url “level/hero” I’m able to resolve it fine now and the position is constantly updating. This might cause issues down the line because I’m specifying the path (and level could change), but this might not be an issue.


PS: I didn’t download the new editor until now because I couldn’t actually find it (!). Might be some styling issues for narrow widths as I have my screen split atm:

Download link showing:

Download link disappeared:

Anyway, cheers for the help. The new editor looks nice, and you guys are really on the ball with response speed and helpfulness.

3 Likes

Added to the web backlog, thanks for finding this!

1 Like