I'd have kept it in the form of
func _on_Portal1_body_entered(body: Node)-> void:
pass
Then I'd add a check:
func _on_Portal1_body_entered(body: Node)-> void:
if body.name == str("Pikachu"):
pass
Next I'd also make sure there is a boolean variable defined at the top of the script, lets call it entered_active_portal_area
for now. And make it true when area has been entered by the node.
onready var entered_active_portal_area = false
func _on_Portal1_body_entered(body: Node)-> void:
if body.name == str("Pikachu"):
entered_active_portal_area = true
Then I'd also connect the body exited signal and do essentially the same but instead assign false as the boolean state:
onready var entered_active_portal_area: bool = false
func _on_Portal1_body_entered(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = true
func _on_Portal1_body_exited(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = false
If it's not clear yet we are simply implementing a dynamic check for if the correct body is overlapping the portal area. Next we will introduce the input function:
onready var entered_active_portal_area: bool = false
func _input(event: InputEvent) -> void:
pass
func _on_Portal1_body_entered(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = true
func _on_Portal1_body_exited(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = false
Can you already guess why?
onready var entered_active_portal_area: bool = false
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"):
# I'm just being lazy here and using a default mapping
# don't worry about it, you can use what you have set up for it
pass
func _on_Portal1_body_entered(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = true
func _on_Portal1_body_exited(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = false
So, I guess we need to get a position to "teleport" the body to, huh? Except, oh wait, input function doesn't even yet know what body to move, right? Well, then I guess there's 2 variables more to add:
onready var entered_active_portal_area: bool = false
export var target_portal: NodePath
# exported variables can be assigned a value via inspector! select the portal you want as the target position
var teleport_target: String
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept") and entered_active_portal_area:
var teleportable = NodePath(teleport_target)
if get_node(teleportable).name == "Pikachu":
# Making sure it's actually the node we want!
get_node(teleportable).set_position(get_node(target_portal).get_position())
else:
print("Pikatchu is not on a active portal")
push_warning("teleportable value is: " + str(teleportable))
return
func _on_Portal1_body_entered(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = true
teleport_target = str(get_path_to(body)) # and lets assign it a relevant value
func _on_Portal1_body_exited(body: Node) -> void:
if body.name == str("Pikachu"):
entered_active_portal_area = false
teleport_target = str("not a valid node path")
Ok, so at this point a nagging thought should occur to you in the back of your mind:
that's a lot of code just for 1 portal and it's all in the player script!
Maybe you should consider giving your portal a script and connecting those signals in there instead? And maybe the portals can be instanced scenes while at it? So you don't have to build essentially the same node structure and rewrite the same script over and over again. I'll leave figuring that out as a learning exercise to you for now.