# A couple of questions I need answering for my game.

Posts: 37Member

Hi, I code on GDScript, and I don't know a few things that could be really helpful for a game I want to make.

First of all, how would you instantly make an object or enemy or whatever go to a specific coordinate? I would be surprised if this function wasn't possible.

Secondly, how would I make an enemy travel just towards the player's x axis, and not its x and y axis?

Thirdly, how can I share variables through scenes and between different nodes? I want to add in some kind of upgrade function but if I can't get a sprite to remember variables between different scripts, then I can't do it.

Finally, there are a few lines of code from tutorials I have watched that I don't get, and therefore am unable to implement on my own:
1) What does Vector2 mean (I know it's to do with movement, but more specifically what and how... if that makes sense)
2) Does motion.x mean moving across the x axis or is it to do with the x position specifically of the sprite?

Thanks

Tags :

edited July 29

@UnrulyElf10 said:

1) What does Vector2 mean (I know it's to do with movement, but more specifically what and how... if that makes sense)

This is a really great youtube channel and the videos are well worth digging through.

@UnrulyElf10 said:

2) Does motion.x mean moving across the x axis or is it to do with the x position specifically of the sprite?

Motion would indicate movement/change in position where as a position would be a location static at least per frame. edit: note that motion and positions would both be kind of vectors.

@UnrulyElf10 said:
First of all, how would you instantly make an object or enemy or whatever go to a specific coordinate? I would be surprised if this function wasn't possible.

For 2D:

``````# Position the object globally:
\$insert_node_here.global_position = Vector2(100, 100)
# You can also use variables. For example:
\$insert_node_here.global_position = \$another_node.global_position

# Position the object relative to its parent
\$insert_node_here.position = Vector2(100, 100)
# It might be translation rather than position... I'm not sure right off.
``````

For 3D:

``````# Position the object globally
\$insert_node_here.global_transform.origin = Vector3(100, 100, 100)
# Position the object locally
\$insert_node_here.transform.origin = Vector3(100, 100, 100)
# or
\$insert_node_here.translation = Vector3(100, 100, 100)
``````

Secondly, how would I make an enemy travel just towards the player's x axis, and not its x and y axis?

Something like this should work:

``````var direction_to_player = global_position.direction_to(\$player_node.global_position)
# cancel any movement on the Y axis by setting it to zero. Then normalize the vector
direction_to_player.y = 0
direction_to_player.normalize()
# Move the enemy (using global_position for this example)
global_position += direction_to_player * movement_speed * delta
``````

Thirdly, how can I share variables through scenes and between different nodes? I want to add in some kind of upgrade function but if I can't get a sprite to remember variables between different scripts, then I can't do it.

You probably will want to store that data in a autoload script. The documentation has a page explaining a bit about how they work.

• Posts: 37Member

Okay, cool. Thanks alot guys- I will try all of these things and hopefully these will all be useful in my game. Thanks for the help!

• Posts: 37Member

@Megalomaniak , my next question to do with Vectors may be why you can't use Vector2 for some different movement functions- like move_and_collide() for example? If you can't use Vector2 for that, then is there some different things you can set the movement variable to? I know I'm asking alot of questions that may be very easy to understand, but I don't fully know why you have to define a motion variable in order to move... or is that not necessary...?

edited July 30

KinematicCollision2D move_and_collide ( Vector2 rel_vec, bool infinite_inertia=true, bool exclude_raycast_shapes=true, bool test_only=false )

Seems to me it does use a vector2.

edit: maybe I'm misunderstanding the question.

• Posts: 37Member

I'm not completely sure what my question is- I think it is a question of is there any other things used like Vector2 that can define a motion variable, and if so, why you have to define a motion variable and what difference it makes.