#Randomly generating terrain

1 messages · Page 1 of 1 (latest)

lost python
#

I can see 2 potential directions to explore here
First, the gaps becoming smaller. Ideally, the new positioning of your Area2D should be tied to the room's size. Something like

(current_room_number * room_width) + room_width - area_distance_from_edge

Same goes for placing the room itself, is should be placed at

(current_room_number * room_width)

(I'm assuming here that all rooms are of equal size) On top of that, I don't see why not just teleport the Area there instead of duplicating it, this way you'll avoid triggering it twice in the same spot

#

Second, is it intentional that the rooms never get deleted? Meaning, can the player backtrack? If they can't, delete the rooms! Only 3 rooms should exist at a time (previous, current, next). And that's even me being charitable with memory, it can be brought down to 2 after some tinkering

But let's assume the player can backtrack so you want to be sure the rooms don't change once generated. In that case I'd separate room data from scenes. Have some lightweight data structure, let's say an array, that stores room IDs. Make a var id and assign a unique one to each room variation. Then:

If the RNG says the next room is "forest", you do

forest.instantiate() # add_child and whatever else
room_array.append(forest.id)```
If the RNG says the previous room (to the left) is "forest", then you do
```swift
forest.instantiate() # add_child and whatever else
room_array.push_front(forest.id)```
You also need some way to keep track of which room is current. You need some counter variable like current_index. Maybe calculate that from coordinates, or add triggers when the player enters the next room. Don't also forget to update that index if you do push_front() because the array shifts. That, or use some more complicated data structure like a dictionary or a class and record those too... There are a lot of possibilities

In any case, having the array and the current index lets you reconstruct your room layout at any point. You know that if you're in room 5, then to go left you need the scene that lives in room_array[4]. This means you no longer need the scene itself, so you can delete it at any point and instantiate it again once it's needed. Thus returning to the paradigm where you only have 2 or 3 room scenes loaded at most.
idle talon
#

sorry I asked this question in the wrong channel by accident, I'll move it to the beginner help instead, I apologize for the inconvenience