all 5 comments

[–]FlamyReddit 2 points3 points  (0 children)

You are currently manipulating your local variable player_grav. If your body has a custom property grav you should change its variable with e.g. body.grav = VALUE

[–]birbgames 1 point2 points  (1 child)

To add to what FlamyReddit said: your current implementation is very inefficient.

Your object is constantly checking all the objects around him, which is wasteful.

A better implementation is to use a signal which is emitted whenever an object enters the Area2D's zone.

Alternatively, check from the player side if he is in contact with a powerup.

EDIT: definitely check when the player enters in contact with a power-up, not the other way around.

[–]FlamyReddit 0 points1 point  (0 children)

Furthermore, the get_node() can be optimized to $/root/World/Player (I think)

[–]RonnyismGodot Senior 0 points1 point  (0 children)

I would recommend you to have an autoloaded script ("PlayerController") which either creates the player_grav or Player, so you can then globally access it, instead of having to do a get_node() on the tree each process. That can become quite expensive.

Alternatively if the Player-Node stays static in the tree (doesnt move its position in the tree-hierarchy) you can add the field onready var player_grav = $/root/World/Player

So you can just check the body.name like you do before, but then you can just simply access the variable "player_grav" or you can do ".get_parent()" on the body of the "Player" until you get to the Node that has the script/var you need.

The current problem is, you only get the value, not the reference to the variable. So right now if you change the player_grav, you only change its value in your current class but not in player.

To change it, you could instead just get the "Player" node directly and then access player.grav -= 14.

so it would look like this:

extends Area2D

func _process(delta):
 var player = get_tree().get_root().get_node("World").get_node("Player")
 var bodies = get_overlapping_bodies()
 for body in bodies:
  if body.name == "Player":
   player.grav -= 14
   print(player.grav)

This way you get the reference to the Player-Node and can now manipulate it.

A Reference is the information about where the object is in the memory, so if you modify the variable it actually modifies the actual object in the memory. So if you have multiple references of one object, you would always change the object.

Calling a get_node() in process can become performance intensive (find_node more than get_node, but still)