0 votes

(The entire code extends a KinematicBody2D)
In my project you have to press a button to switch dimensions. I am trying to store the old player location and teleport the player in the new node to this location. I have written some code but it just teleports me to the other dimension and doesn't teleport me to the right coordinates. and I would like to know how to edit my code so that it works. Since I didn't find any code samples of a similar project online I had to code this entire script without any help. Even though it isn't much, it is hard for me since I am really bad at coding. I have used 2 global variables: pos: This stores the player position forceactive: This is used because a certain part in my code checks if it is one. The same code is in both nodes, I am using two different scripts for both KinematicBodies in these two nodes. The only thing that varies is that if you switch from 1 to 2 forceactive has to be one and if you switch from 2 to 1 it is set to 2.
This is the sender in the first node:

if Input.is_action_just_pressed("switch"):
    Global.force_active = 1
    Global.pos = position

In the other scene there is a "receiver"

func _reveiver ():
    if Global.force_active == 1:
        Global.force_active = 0
        position = Vector2(Global.pos)

(This is the code if you want to go in the other direction:

if Input.is_action_just_pressed("switch"):
    Global.force_active = 2
    Global.pos = position


func _reveiver ():
    if Global.force_active == 2:
        Global.force_active = 0
        position = Vector2(Global.pos)

If you answer please keep in mind that I am a beginner.

Somebody on Reddit asked me something and I just wanted to clarify a few things. (This is just the reddit comment pasted so it may seem weird)

Thank you for the answer. I had to clarify it, it just switches the
dimension but it doesn't teleport you. In both nodes there are 2
KinematicBody2D and both have similar scripts (They are only different
in one little thing that I will mention later) Let's call their
scripts 1 and 2. If you want to switch from 1 to 2 the following
should happen The sender in the input function in 1 should do the
following: 1: Check if E ("switch") is pressed
2: Store the players position ("pos")
3: Set forceactive to one
4: Switch dimensions In 2 there is the receiver 1: It should check if force
active is 1
2: If it is it should set it to 0
3: It should teleport you the position saved in "pos" The same happens if you want to teleport from 2 to 1 except that it
sets forceactive to 2 and that the script in 1 checks for a 2. Both
variables are global variables (The docs said that I only had to put
my global.gd into autoload and activate singleton) This is the current
global.gd code: extends Node2D var pos = 0 var force
active = 0

Currently it switches dimensions. I tried to try out the variables but
I can't get text labels with variables working even with normal vars
so I figured that It would be easier to ask before I waste another
hour trying to figure out how to debug these vars. I am just
going to paste my entire code into this reddit thread (This is the
code of 1) Maybe it is going to be easier to understand it if you read
my entire code:

extends KinematicBody2D

export (int) var speed = 600
export (int) var jump_speed = -900
export (int) var gravity = 3000
export (float, 0, 1.0) var friction = 0.1
export (float, 0, 1.0) var acceleration = 0.125
var velocity = Vector2.ZERO
var gprt = 0.2
var pos = 0

func get_input():
  velocity.x = 0
  if Input.is_action_pressed("walk_right"):
      velocity.x += speed
  if Input.is_action_pressed("walk_left"):
      velocity.x -= speed
  #Sender starts here
  if Input.is_action_just_pressed("switch"):
      Global.force_active = 1
      Global.pos = position
func _physics_process(delta):
  velocity.y += gravity * delta
  velocity = move_and_slide(velocity, Vector2.UP)
  if Input.is_action_just_pressed("jump"):
      if is_on_floor() && (gprt > 0):
          velocity.y = jump_speed
  var dir = 0
  if Input.is_action_pressed("walk_right"):
      dir += 1
  if Input.is_action_pressed("walk_left"):
      dir -= 1
  if dir != 0:
      velocity.x = lerp(velocity.x, dir * speed, acceleration)
      velocity.x = lerp(velocity.x, 0, friction)
#Receiver starts here
func _reveiver ():
  if Global.force_active == 2:
      Global.force_active = 0
      position = Vector2(Global.pos)
in Engine by (25 points)
edited by

1 Answer

+1 vote

I asked the question on reddit and somebody answered. All I had to do was deleting my _receiver and putting the entire code of it into the _ready function. After I did that I perfectly worked.

by (25 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.