Well, you wouldn't put that code in the map, because it's not the map's position you're trying to limit (and the map doesn't have a velocity).
Think of it this way: when you add a node, it has a bunch of different properties and functions that come along with it. A Node2D has a position
for example.
When you attach a script to a node, what you are doing is adding additional custom properties and functions to that node, which is why the script starts with extends
.
Now you can technically put code on any node, and use get_node()
to find another node and access its properties, but this is usually a good way to make a bunch of messy and unorganized code.
For example, in the map, you can access the player via $Player.position
, etc. But as you can see in the example above, I didn't use any map properties or variables - I used the player's own camera node's properties. The code I gave you would go in the player's _physics_process()
method, which also makes sense since that's where you're moving the player (changing its position
). In general, you should try to put player code on the player, map code on the map, etc. If the map needs to tell the player something, like how big it is, you can do that like we do in the map's _ready()
.
One more piece of advice: be careful you're not trying to do too many things at once. In my experience this is a very common pitfall for beginners. If you notice when following my videos, I add one thing at a time. When that thing works, I move on to the next. When following a tutorial it's easy to just parrot what's on screen and keep going. Don't move on until you are confident that you understand what you just did.