Calling a Variable from Another Node

goodmuttgoodmutt Posts: 3Member

Hi all!
I am stuck on a piece of my code that is driving me crazy. Hopefully someone here can help. I have two nodes that I want to interact with each other. One is a kematicbody2d Player node (called "Lizard") the other is an area2D ("Light"). What I want to do is when the player enters the area2d, it starts tracking the player's current position. When player leaves the light, it stops.

Right now I am able to print the player's position upon entering the Area2D node, however it only prints the INITIAL position at game start, not the current position. I am using var "loc" to get my player's position in the Lizard code. If I set the variable in the _fixed_process(delta) function, when I try to print it in Area2D it doesn't print anything. If I set var loc outside of process delta, it only gives me the start position of the node and doesn't update.

Lizard Code:
extends KinematicBody2D
export var speedmult = 10
export var MOTIONSPEED = 500

func _ready():

---set_fixed_process(true)

func _fixed_process(delta):
---var loc = self.get_pos()
---var motion = Vector2()
---#get_node("Light").Brightness
movement
---if(Input.is_action_pressed("move_up")):
------motion += Vector2(0,-1)

---if(Input.is_action_pressed("move_left")):
------motion += Vector2(-1,0)

---if(Input.is_action_pressed("move_down")):
------motion += Vector2(0,1)

---if(Input.is_action_pressed("move_right")):
------motion += Vector2(1,0)

---if(is_colliding()):
------print("hi")
------#print(Light.Brightness)

---motion = motion.normalized() * MOTIONSPEED * delta
---move(motion)
---#print(get_pos())

Light code:
extends Area2D
export var Brightness = 2
onready var getLizard = get_node("/root/World/Lizard").get("loc")

func _ready():
---self.set_scale(Vector2(Brightness,Brightness))
---set_process(true)

func _process(delta):
---self.set_scale(Vector2(Brightness,Brightness))

func _on_Light_body_enter_shape( body_id, body, body_shape, area_shape ):
---print(getLizard, "Hello")

func _on_Light_body_exit_shape( body_id, body, body_shape, area_shape ):
---print("Goodbye")


Any help appreciated, I've been stuck on this for DAYS.

Best Answer

  • goodmuttgoodmutt Posts: 3
    Accepted Answer

    I figured it out! I just declared the variable for the lizard's location at the top of the lizard code, then I called assigned it to get_pos in process delta so it was tracking tracking the current position. When I called this variable to Light, I declared the getLizard variable, but I didn't assign the "get_node" until down in process.

    Ultimately I was just having trouble with when to call/assign variables...
    Hope that answer makes sense.
    Thanks!

Answers

  • kow__kow__ Posts: 24Member

    You should create a variable like is_in_area.

    Connect body_enter() and body_exit(). When the lizard enters you set it to true and when it exits you set it to false.

    Then just put a check in _process() for the variable, and if it's true then get the lizard position.

  • goodmuttgoodmutt Posts: 3Member

    @kow__ said:
    You should create a variable like is_in_area.

    Connect body_enter() and body_exit(). When the lizard enters you set it to true and when it exits you set it to false.

    Then just put a check in _process() for the variable, and if it's true then get the lizard position.

    Thank you! I will give this a try as soon as I have a chance to sit back down at it.

  • goodmuttgoodmutt Posts: 3Member
    Accepted Answer

    I figured it out! I just declared the variable for the lizard's location at the top of the lizard code, then I called assigned it to get_pos in process delta so it was tracking tracking the current position. When I called this variable to Light, I declared the getLizard variable, but I didn't assign the "get_node" until down in process.

    Ultimately I was just having trouble with when to call/assign variables...
    Hope that answer makes sense.
    Thanks!

Sign In or Register to comment.