I want to make efficient RPG teleport

samcom2asamcom2a Posts: 3Member
edited December 2018 in 2D

Hello! I'm beginner on Godot. I'm working in a prototype project to I learn while try to work on my final project.
I don't know how to make my Player teleport. I'm creating a "Resident Evil like" game to make the character travel from the scenes, but TOTALLY 2D.

Ok, I've tried to teleport my Player with two manners:

ONE. Putting my Player.tscn on the map scene and using Teleport.tscn as instanced node.
* Result: ERROR
* Screenshot:
* Teleport.gd:

extends Area2D

export (String) var mapa
export (Vector2) var coordenadas
export (String) var posicao

func _on_Teleport_body_entered(body):
    get_tree().change_scene(mapa + ".tscn")
    if coordenadas != null:
        # !!!!! TELEPORT NOT WORKING >:( !!!!!
        print($Player)
        #$Player.position.x = coordenadas.x
        #$Player.position.y = coordenadas.y

TWO. Creating Map.tscn to put my Player on a kind of glass and call the_map's scene_ with "add_child".
* Result:
* Screenshot:
* Map.gd:

extends Node2D

const MAP = "res://Cozinha A.tscn"
const POSITION = Vector2(100, 50)

func _ready():
    teleport(MAP, POSITION, "up")

func teleport(tp_name, tp_position, tp_turn):
    var map_name = preload(tp_name)
    var map_position = POSITION
    var map = map_name.instance()
    var current_map = map_name
    add_child(map)
    $Player.set_position(POSITION)

Answers

  • ZireaelZireael Posts: 81Member
    Answer ✓

    Option 1 won't work because once you use change_scene, the original scene is freed (no longer there).

    For option 2, Godot told you what the problem is. The preload wants a string and you gave it a MAP parameter. Alternately, since the MAP variable points to a string, maybe the problem is that it's a const? Try changing it to var...?

  • samcom2asamcom2a Posts: 3Member
    edited December 2018

    @Zireael said:
    Option 1 won't work because once you use change_scene, the original scene is freed (no longer there).

    For option 2, Godot told you what the problem is. The preload wants a string and you gave it a MAP parameter. Alternately, since the MAP variable points to a string, maybe the problem is that it's a const? Try changing it to var...?

    Option 1: Makes sense, but in the next map have an Player.tscn. How can I link the Player.tscn node with my freed Teleport.tscn node?

    Option 2: This error is ocurring because the Godot want a constant to I use on preload. On my code, I need to use a custom variable to make my funcion teleport works :(

    Thanks a lot for your reply, anyway!! ^o^

  • MegalomaniakMegalomaniak Posts: 1,059Admin

    I've adjusted the OP for the code rendering.

  • TwistedTwiglegTwistedTwigleg Posts: 1,000Admin
    Answer ✓

    For option two, you can replace preload with load and it should work. You cannot preload a variable due to the fact that it may change (even if the variable is marked constant, as there was a bug where constant variables were changing).

  • samcom2asamcom2a Posts: 3Member

    @TwistedTwigleg said:
    For option two, you can replace preload with load and it should work. You cannot preload a variable due to the fact that it may change (even if the variable is marked constant, as there was a bug where constant variables were changing).

    WOAH! WORKS FINE HERE!! VERY VERY THANK YOU @TwistedTwigleg !! <3
    I don't decided whose options is more efficient for my kind of project, but the option two looks works very well for now.

Sign In or Register to comment.