Figuring out level generation (help needed)

So I was happily following the 2d godot course "Master Mobile Game Develpment" until I reached the lesson “Generating the Level”. I have tried my best at checking where the error is at, but the platforms just won’t show up, not even on screen, and not even the first line of platforms which should appear at the bottom of the screen. I can send whatever part of the proyect is needed, for now, this is the code on the game scene script:

extends Node2D

@onready var platform_parent = $PlatformParent
var camera_scene = preload ("res://scenes/game_camera.tscn")
var camera = null
var platform_scene = preload ("res://scenes/platform.tscn")


func _ready():
 camera = camera_scene.instantiate()
 camera.setup_camera($Player)
 add_child(camera)

 var viewport_size = get_viewport_rect().size
 var platform_width = 136 
 var ground_layer_platform_count = (viewport_size.x / platform_width) + 1

 var ground_layer_y_offset = 62
 for i in range (ground_layer_platform_count):
  var ground_location = Vector2(i * platform_width, viewport_size.y - ground_layer_y_offset)
  create_platform(ground_location)

func _process (delta):
 if Input.is_action_just_pressed("quit"):
  get_tree().quit()
 if Input.is_action_just_pressed("reset"):
  get_tree().reload_current_scene()


func create_platform (location: Vector2):
 var platform = platform_scene.instantiate()
 platform.global_position = location
 platform_parent.add_child(platform)
 return platform

Thanks! :slight_smile:

For reference, platforms worked perfectly before this lesson.

Thanks for taking the initiative to post your script! No worries, we’ll get it sorted one way or the other =)

Here’s what I would start with:

  • Run the game and check the Remote Tab to see if the platforms are simply being spawned offscreen. It wouldn’t be the first time I ran into something like that, even in my own projects. Assuming that doesn’t shed any light on the problem:
  • Immediately after your declaration of ground_layer_platform_count, print its value and confirm that it’s something reasonable. It should be, since it will be a minimum of 1, but this will also confirm that _ready() is actually executing.
  • Just inside the for-loop, print i or anything else you want. There shouldn’t be anything wrong with the for-loop, but it’s best not to take anything for granted when there’s a bug.
  • After platform_parent.add_child(platform), print platform.global_position to see if that’s being assigned as expected. Again, it looks obvious and I don’t expect any problems here, but there genuinely are situations in which it wouldn’t work.

Chances are pretty good that something in there is going to lead you to the solution. If not, or you need help interpreting the results, by all means let us know (and if you fix it, let us know that too!).

I tested every point you mentioned: the platforms do show up in the remote tab and the prints make sense. So you are probably are right about the spawns being off camera, ill take a look at that stuff for now. Thanks!

1 Like

So finally I decided to just redo the whole project since I wasn’t that far. I got past the last issue but only got a new one, it looks like my ground generation its messed up.

Game script:

extends Node2D


@onready var platform_parent = $PlatformParent


var camera_scene = preload ("res://scenes/game_camera.tscn")
var platform_scene = preload ("res://scenes/platform.tscn")
var camera = null


func _ready():
 camera = camera_scene.instantiate()
 camera.setup_camera($player)
 add_child(camera)

 var viewport_size = get_viewport_rect().size
 var platform_width = 136 
 var ground_layer_platform_count = (viewport_size.x / platform_width) + 1
 var ground_layer_y_offset = 160
 for i in range (ground_layer_platform_count):
  var ground_location = Vector2(i * platform_width, viewport_size.y - ground_layer_y_offset)
  create_platform(ground_location)

func _process (delta):
 if Input.is_action_just_pressed("quit"):
  get_tree().quit()
 if Input.is_action_just_pressed("reset"):
  get_tree().reload_current_scene()


func create_platform(location: Vector2):
 var platform = platform_scene.instantiate()
 platform.global_position = location
 platform_parent.add_child(platform)
 return platform

Game_camera script:

extends Camera2D


var player: Player = null
var viewport_size

func _ready():
 viewport_size = get_viewport_rect().size
 global_position.x = viewport_size.x / 2
 limit_bottom = viewport_size.y
 limit_left = 0
 limit_right = viewport_size.x
 limit_top = 0

func _process (delta):
 if player:
  var limit_distance = 420
  if limit_bottom > player.global_position.y + limit_distance:
   limit_bottom = player.global_position.y + limit_distance


func setup_camera (_player: Player):
 if _player:
  player = _player
  

func _physics_process (delta):
 if player:
  global_position.y = player.global_position.y

Well, fair enough, that can work sometimes, but unfortunately it leaves you with unanswered questions about the original problem. If you do restart from scratch, it’s always a good idea to keep the previous version of the project until you’re absolutely certain it’s been completely amalgamated. Maybe you did, I can’t tell from what’s written here, just a lesson of my own that I learned early on =)

The good thing is that it sounds like you gave the debugging process a real solid effort first, which is commendable. You can only get better by practicing!

Anyway, since both versions of your game script are posted here, I ran them through WinMerge, which is a free and simple tool used to find differences between different versions of the same text. There are only 2 real differences between them and I don’t think these would contribute, but the reason I bother to explain this is because if you do in fact still have your old code, I’d encourage you to play around with WinMerge to see what other differences might exist in other scripts. It’s a nice thing to have in your toolbox, even if you don’t actually need it very often.

What I think probably happened here is one of two things:

  • Either the object scene itself (platform) has an offset that it shouldn’t,
  • Or there is an offset introduced by ancestor nodes via Transform inheritance. In other words, something else that directly or indirectly owns the platform has an offset that it shouldn’t and it’s passing it on.

I experienced exactly this problem myself, and if I remember correctly, it turned out to be both of these things. That’s what I’d look at next; happy hunting!

1 Like

Turns out I just had an uppercase/lowercase difference that was causing issues. I had $Player instead of $player in the ready func of the game script, where the line is: camera.setup_camera($something). Also adding the line: limit_top = 0 in the ready func of the game_camera script helped too.

I’ll still get that tool since it looks like it could have saved me a lot of trouble.

I’ll mark the thread as solved since now I’m pretty sure I know what the problem is. Thanks for taking the time to help a beginner

1 Like

Haha, you’re quite welcome! ^v^

Here’s a quick link to WinMerge if you haven’t already checked it out:
https://winmerge.org/

This thread will close in a little less than a day; if it turns out there is still another twist to go through, PM me and I will reopen it for you (which would be preferable to making a new topic).

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms