Mikhail
Mikhail

Reputation: 11

godot game instances positions

I'm making a multiplayer game and I'm running 2 instances of the game and I want that when starting from the editor, the windows of these instances appear side by side from each other (one on the floor of the screen on the left, the other on the floor of the screen on the right).

I know that you can adjust the position of the game window in the editor, but how to do it for 2 instances?

Upvotes: 1

Views: 163

Answers (1)

Jorvan
Jorvan

Reputation: 131

I ran into the same problem and, based on what KonstantinosPetrakis commented on this issue https://github.com/godotengine/godot-proposals/issues/3357, I came up with the following solution:

func _adjust_both_windows():
    randomize()
    await get_tree().create_timer(randf_range(0,1)).timeout
    var the_path = 'user://lil_number.tres'
    var save_the_value = func(val): 
        var the_file = FileAccess.open(the_path, FileAccess.WRITE)
        the_file.store_64(val)
        the_file.flush()
        the_file.close()
    var read_value = func():
        if FileAccess.file_exists(the_path):
            await get_tree().create_timer(.5).timeout
            var the_file = FileAccess.open(the_path, FileAccess.READ)
            var the_value = the_file.get_64()
            the_file.close()
            return the_value
        return 0 
    var the_value = await read_value.call()
    save_the_value.call(the_value + 1)
    get_window().size = get_window().size*0.75
    if (the_value % 2 == 0):
        get_window().position.x = 0
    else:
        get_window().position.x = get_window().size.x

With that you can just:

func _ready():
    _adjust_both_windows()

And you're ready to go (however, you'll probably want to tweak some values). I hope this helps someone, but it seems like this is soon going to be a problem from the past, as I read that the next Godot version will provide facilities to find and control multiple session instances 🤷‍♂️

Upvotes: 0

Related Questions