User:Senka/a bas nije da nas nikad nije bilo/Godot Debugging

From XPUB & Lens-Based wiki

Debugging

Portals

There's a documentation page for portals and rooms [1] but it's outdated (made for Godot 3.5, I'm using 4.3).

Portals to different rooms
This needs a lot of reorganizing of the code for it to work... Currently stuck at trying to delete one room once there has been a switch from another (queue_free function not working).

The script is supposed to instantiate a new scene once the body ("Player") enters the portal. After the player has entered the new scene, the previous one is supposed to be deleted with the queue_free function.

Currently I have:

  • A portal (Area3D) with the script:
extends Area3D
var next_scene = preload("res://rooms/room_2.tscn")
var current_scene = preload("res://rooms/room_1.tscn")
func _on_body_entered(body: Node3D) -> void:
   if body.name == "Player":
       if body.in_transition == false:
           body.in_transition = true
           print("something might work")
           var instanced_scene = next_scene.instantiate()
           get_tree().root.get_node("World").add_child(instanced_scene)
func _on_body_exited(body: Node3D) -> void:
   if body.in_transition:
       body.in_transition = false 
The script with all the code that was tried, didn't work and commented out
  • room2 (Node3D) with the script:
extends Node3D 
@onready var player = $Player
@onready var room1 = "res://rooms/room_1.tscn"
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
       player.camera.make_current()
The code ONLY for the room that needs to be instantiated. It creates the room, the player and the camera the player is looking through and swtiched to the new camera of the player
  • the World (root) node (Node3D):
extends Node3D
@onready var room1 = "res://rooms/room_1.tscn"
var room1_scene = preload("res://rooms/room_1.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
   if room1_scene != null and is_instance_valid(room1):
       #room1.queue_free()
       var deletion_scene = room1_scene.instantiate()
       deletion_scene.queue_free()
       print("room1 deleted")

No error appears with this version of the code but it doesn't work.

The script for the world node from which I am trying to delete the scene the player left

Question:

  • How to adjust the code to delete the previous room? (While allowing the user to enter back)

FIXED WITH adding:

get_tree().root.get_node("World/room1").free()

This fixes it because what I was doing before, calling a variable in the queue_free() function, wasn't working and was giving it a null value
Next steps:

  • Why is the portal in the new scene not appearing?
error code: W 0:00:25:0044   portal.gd:17 @ _on_body_entered(): Node './Portal' was modified from inside an instance, but it has 
vanished.
 <C++ Source>   scene/resources/packed_scene.cpp:254 @ instantiate()
 <Stack Trace>  portal.gd:17 @ _on_body_entered()

The instantiated scene does not appear in the HTML exported version. There are the errors that pop up:

errors that appear when I uploaded the most recent version of the godot project to html
part 2
  • How to do this code in the World script instead of the Portal script?
question for thijs, but only if you have time!
  • Can portals refer to/lead to HTML pages? (I would love to link some rooms to bitsy pages if possible)
question for thijs

Redone to be from the world node (ty thijs)

The Code of world.gd:

extends Node3D
@onready var room1 = "res://rooms/room_1.tscn"
var starting_scene = preload("res://rooms/room_1.tscn")
var next_scene = preload("res://rooms/room_2.tscn")
var test_scene = preload("res://rooms/text_room.tscn")
@onready var RoomContainer = $RoomContainer
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
   print(self.get_children())
   switch_to_room(starting_scene)
func switch_to_room(room: PackedScene) -> void:
   var instanced_room = room.instantiate()
   RoomContainer.add_child(instanced_room)
   instanced_room.get_node("Portal").connect("portal_activated", _handle_portal_activated)
var destination = instanced_room.get_node("Start").global_transform.origin
   get_node("Player").global_transform.origin = destination
# there will always be exactly one room
# new rooms are appended to RoomContainer
# so, if RoomContainer has more than 1 child, we free the first child (the oldest room)
   if RoomContainer.get_children().size() == 2:
       RoomContainer.get_child(0).free()
func _handle_portal_activated(next_scene_name: String) -> void:
   match(next_scene_name):
       "room1":
           switch_to_room(starting_scene)
       "room2":
           switch_to_room(next_scene)
       _:
           printerr("Something went wrong in switching rooms! :(")

In this code what was added was:

  • A custom signal from portal.gd (scroll down to see the code)
  • function switch_to_room which instantiates the new scene, connects to the custom signal, does math to delete the previous room (there can only ever be one room loaded)
  • function _handle_portal_activated which switches the the next room
  • Now there's an empty Node3D which is a starting point the player gets transported to in the new instantiated room

Errors along the way (after adding a staircase that the portal is on top of):

  • The players was getting stuck because of the normals of the 3D models orienting the wrong way

The fix: https://www.youtube.com/watch?v=qJvvDHyK14Y&list=PLjEV6KRxNq9XGyqZhpVOteDTZ13ROhHSy&index=57

  • Go to Edit mode with you object selected, in it deselect everything, or click Select > None
  • Going to Overlaps drop down menu
  • Select Face orientation
  • Click on the drop down menu of Mesh Edit (it's next to Overlays), select Display Normals (it's the third one) and increase the size
  • To flip them, select the vertices you need then go to Mesh> Normals > Flip or Mesh> Normals > Recalculate Inside/Outside (Depending on where you need them to be facing)
  • The movement of the player was lagging because or the mesh of the collision object being too complicated despite using -colonly on the mesh. -colonly is supposed to improve loading compared to just -col

The fix:

  • Re-doing the collision shape to be a much simpler one

More about adding collision to 3D models: https://docs.godotengine.org/en/stable/tutorials/assets_pipeline/importing_3d_scenes/node_type_customization.html#create-collisions-col-convcol-colonly-convcolonly

  • The portals ceased to work once they were placed on the escalator stairs. The error that keep popping up was that the in)transition function cannot work on a StaticBody3D. The player is a CharacterBody3D, so not a static one. Turns out the portal was registering the stairs and not the player.

The fix:

Adding the 'if body.name=="Player": again before the other if function' changing the code slightly:

extends Area3D
#@export var connect_portal: Area3D
#@export var connect_scene: Node3D
#@export var current_scene: Node3D
@export var next_scene_name = "null"
signal portal_activated(next_scene_name: String)
func _on_body_entered(body: Node3D) -> void:
   if body.name == "Player":
       if body.in_transition == false:
           body.in_transition = true
           portal_activated.emit(next_scene_name)
func _on_body_exited(body: Node3D) -> void:
   if body.name == "Player":
       if body.in_transition:
           body.in_transition = false 

If I want to have multiple portals in a room... we have a problem

  • How to do this?
Question for Thijs: Would this be too difficult to do?
Because I can try fragment the models instead so you encounter them multiple times on a more linear journey. But then the aspect of choice is really lost..

HTML/itch.io Exporting

Questions:

  • If the game is uploaded as an html page, is it better to have the menu as javascript button on top of the game (canvas item) or to have an in-game menu? Would the in-game menu access the in-game mechanism better? (Such as room change)
question for thijs
  • Creating subdomains for your website?
  • How to improve loading?

Rich Text Label

Texture

Fixed:

  • All texture issues can be solved with importing glft/glb instead of a blend file
  • light from blender need to be 50mW or less