0 votes

Hi there! im new to gogot and so far im loving it, but i have a problem. im currently trying to figure out how to make my map go on for infinity and have no clue how to do it, please help. thanks!

in Engine by (12 points)

2 Answers

0 votes

Parallax Background, maybe?

by (195 points)
+1 vote

This is a basic example to generate random tile squares according to their probability:

extends Node2D
onready var tilemap = $TileMap
var set_desert = {"sand" : 85, "dirt" : 10, "grass" : 5}
var set_plain = {"sand" : 0, "dirt" : 10, "grass" : 90}

func _ready():
    make_chunk(10,10, set_desert)

func make_chunk(width:int, height:int, set:Dictionary) -> void:
    for i in width:
        for j in height:
            var tile_name:String = set.keys()[get_random_weigths(set)]
            tilemap.set_cell(i, j, tilemap.tile_set.find_tile_by_name(tile_name))   

func get_random_weigths(set:Dictionary) -> int:
    var sum_weigths:float = 0.0
    var offset:float = 0.0
    var index:int = 0
    var rnd:float = randf()

    for value in set.values():
        sum_weigths += value

    for value in set.values():
        if rnd <= (offset + value) / sum_weigths:
            return index
        index += 1
        offset += value 
    return index    

In this example I have a tilemap as a child, which has a tileset with 3 single-tiles called "sand", "dirt" and "grass".
So as the player walks, you create random or semi-random "chunks" around them. I don't see it that easy. The code I put uses Tilemap, which uses Tileset as a resource. And I also used Dictionary. Look at those classes in the manual for more details.

by (2,260 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.