This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

How to set spawn locations after a loading zone? [3D]

KindosaurGamingKindosaurGaming Posts: 64Member
edited February 2020 in Programming

Hi there,

I was able to make code which switches scenes when a KinematicBody enters an Area node. But I also need a line of code that teleports the player when he steps into a different area and it changes the scene but teleports the player to a new spawnpoint.

So something like this:

Player Steps In Scene Changer 1 > Outcome 1 > Location X = 20, Y = 12, Z = 30
Player Steps In Scene Changer 2 > Outcome 2 > Location X = 40, Y= 12, Z = 5
etc.

I do think the outcomes need be scene connected or something like that but I am not sure on that.
for example:

Exits HouseScene1 > Outcome WorldScene2
Enters WorldScene4 > Outcome LevelScene1

this is the code that changes the scene when body has entered the area:

extends Area

var _is_player_in_Area = false
export (String) var player_node_name = "Player"
signal on_player_entered
var _start_new_scene = false

func _ready():
    connect("body_entered",self,"_on_Scene_Load")
    connect("on_player_entered",self,"_Print_Load");

func _process(delta):
    if _is_player_in_Area == true:
        emit_signal("on_player_entered")
        _start_new_scene = true
        get_tree().change_scene("res://Scenes/Scenes 3D/Title Screen/Title/MENU.tscn")


func _on_Scene_Load(other):
    if other.name == player_node_name:
        _is_player_in_Area = true


func _Print_Load():
    print ("Load Scene!");

Thanks for the help :P


Tags :

Answers

  • MegalomaniakMegalomaniak Posts: 2,580Admin

    Edited the post to better format the code since you were using inline code formatting, hope you don't mind.

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin

    The way I have handled this kind of thing in the past is to have a singleton/autoload script store the location to spawn the player at across scenes. Something like this:

    var player_position = null
    # optional
    var player_rotation = null
    

    Then in the player script, I have something like the following in _ready (assuming the singleton is called Globals):

    func _ready():
        if (Globals.player_position != null):
            global_transform.origin = Globals.player_position
        if (Globals.player_rotation != null):
            rotation_degrees = Globals.player_rotation
    

    Finally, in the thing that changes the scene, I use something like the following:

    export (Vector3) var position_in_next_scene = null
    export (Vector3) var rotation_in_next_scene = null
    
    # Other code
    
    func change_scene():
        Globals.player_position = position_in_next_scene
        Globals.player_rotation = rotation_in_next_scene
    

    There probably are better ways of handling it, but something like the code above is what I have used in several games and it works okay. The biggest downside is the data entry is a little time consuming.

  • KindosaurGamingKindosaurGaming Posts: 64Member

    @TwistedTwigleg
    Hello again

    I asked the same question on the Q&A forum of godot and got an answer on this question aswell

    It is a different approach which uses a dictionary to discribe where sertain spawn areas are. Is this a good solution as well

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin

    @KindosaurGaming said:
    @TwistedTwigleg
    Hello again

    I asked the same question on the Q&A forum of godot and got an answer on this question aswell

    It is a different approach which uses a dictionary to discribe where sertain spawn areas are. Is this a good solution as well

    Seems like a good solution.
    From what I can gather, the idea is that you place all of the locations (and rotations) into a dictionary in a singleton, which would make it where you can link the position to a name, which might make using it a little easier to use and since everything is in one place (both a pro and a con) there is no scene hopping trying to figure out what goes where.

    The only, albeit minor, downside with placing all the locations into a dictionary is that depending on how many locations you have stored, managing the dictionary in code and finding specific locations may become a little more difficult. Not a huge issue, but if you have dozens of locations, it may be something to be aware of.

  • KindosaurGamingKindosaurGaming Posts: 64Member

    @TwistedTwigleg
    I do want to implement around 7 islands

    Maybe I can try to give each Island a separate Scene (or even file) and only transfer the player data.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.