Is it possible to get node of a *.tscn
await get_tree().process_frame
to wait 1 frame
- Edited
It always returns null error
.
..
Previously i was doing this: !to set the camera limit automatically!
lvlCamTile = get_tree().get_current_scene().get_child(0);
Player.cam2D.limit_right = lvlCamTile.get_region_rect().size.x;
Player.cam2D.limit_bottom = lvlCamTile.get_region_rect().size.y;
i cant bypass that extra delta frame, the scene always returns null and i cant check for null...
I think its the 'if levelGO == true' on top
- Edited
although i think that extra delta frame i have to wait to get the node and set the camera limit, is the split second i get when changing levels when the camera is out of bounds...
this only happens on some ocasions, if lvl1 is 2000 width, and lvl2 500 width... for a split second when transitioning to lvl2 the camera is out of bounds.
[edit]
error
print( get_tree().current_scene.scene_file_path )
res://levels/lvl_e8.tscn
res://levels/lvl_e7.tscn
res://levels/lvl_e8.tscn
res://levels/lvl_e7.tscn
if ( playerlGO == true && get_tree().current_scene.scene_file_path != null ):
E 0:00:04:0786 next_level.gd:37 @ _process(): Parameter "data.tree" is null.
<C++ Source> scene/main/node.h:413 @ get_tree()
<Stack Trace> next_level.gd:37 @ _process()
Stack: invalid get index 'current_scene' ( on base:'null instance' )
i cant check for null ?!?
- Edited
xyz its running on lvl1.tscn the ( next_level node ) its also on lvl1 going into lvl2 once the player touches it
extends Node2D #next_level
@export var nextLevel = "res://levels/lvl_a1.tscn";
@export var plrnextX = 480;
@export var plrnextY = 288;
@export var camZoom = 1;
@export var camLimitX = 480;
@export var camLimitY = 288;
var levelGO = false;
var timer = 0.1; #0.1 seconds
#func _ready():
#
func _process(delta):
if ( timer > 0 ):
timer -= 1*delta;
return;
if ( levelGO == true ):
get_tree().change_scene_to_file(nextLevel);
Player.position = Vector2(plrnextX, plrnextY);
Player.cam2D.zoom = Vector2( camZoom, camZoom );
Player.cam2D.limit_right = camLimitX;
Player.cam2D.limit_bottom = camLimitY;
###lvlCamTile = get_tree().get_current_scene().get_child(0); # camera limit node
###Player.cam2D.limit_right = lvlCamTile.get_region_rect().size.x;
###Player.cam2D.limit_bottom = lvlCamTile.get_region_rect().size.y;
levelGO = false;
func _on_area_2d_body_entered(body):
if ( timer > 0 ):
return;
if body.is_in_group("player"):
levelGO = true;
#body.position = Vector2(plrnextX, plrnextY); #fuck not accurate !!!
#body.cam2D.zoom = Vector2( camZoom, camZoom );
- Edited
jonSS it doesn't matter in which tscn it is. The only important thing is the current state of the node the script is attached to. For some reason that node is not in the scene tree when you call get_tree(). You need to determine why it is not. Can you make a minimal reproduction project that just loads dummy scenes?
- Edited
There it should be a flickering when going from level e1 to level e2 and e4 to e5
for a split second you can see a white rectangle with a red mark... that is the next level node showing up until the camera limit gets corrected
to stop the flickering y need to unComments these 2 line in ( next_level.gd )
#Player.cam2D.limit_right = camLimitX;
#Player.cam2D.limit_bottom = camLimitY;
I was trying to avoid this... because i have to input the camera limits manually for each next_level node on screen, its more confusing and takes more time
- Edited
jonSS Then make a function in Player that will read any needed stuff from current level and adjust camera and player accordingly. Call this function from level's _enter_tree()
It's important to update all at the same time. Currently you have a mismatch because you set player position before the level is added and camera limits for new level only after the level is added. So there is one frame inbetween where player and level are not matched.
Btw no need to set camera limits every frame as you're currently doing. This is in fact causing the problem. Do it only once when level is loaded.
- Edited
jonSS
in player.gd
func level_started():
var lvl = get_tree().get_current_scene().get_child(0);
cam2D.limit_right = lvl.get_region_rect().size.x;
cam2D.limit_bottom = lvl.get_region_rect().size.y;
position = lvl.player_initial_pos
cam2D.zoom = lvl.initial_zoom
In each level:
@export var player_initial_pos: Vector2
@export var initial_zoom: float
func _enter_tree():
Player.level_started()
You can also put a dedicated positional marker node into level and let the level_started()
function use its position for player starting position. That way you won't need a manually filled-in property.
Again, the important thing is to do all this at once as soon as level is added to the tree. So best to keep it all in one function and call that function exactly at right time.
EDIT: if there will be some initializer scripts inside level, it may be better to call Player.level_started()
from level's _ready()
instead from _enter_tree()
- Edited
xyz thanks it worked
but using _enter_tree(): on lvl_e1.gd throws a nill error i had to use func _ready(): on other level e2, e3, e4, etc... _enter_tree(): seems to works.
I used the function in the camera2D instead, my player node script is kind of full:
*lvl_e1.gd
extends Node2D
var map_POS = Vector2(272, 112);
func _ready():
Player.cam2D.level_started()
*lvl_e2.gd
extends Node2D
var map_POS = Vector2(240, 128);
func _enter_tree():
Player.cam2D.level_started()
you can also put a dedicated positional marker node into level
Ive added a Marker2D into the nextLevel, but i cant move this node position when its on the level ?
the other nextLevel scenes on the level are duplicates. There are 2 nextLevel's in most levels the player either comes from the left or right ? I dont see how it can be done in an auto way ?
If i could move the Marker2D node in the editor for each nextLevel it would work, but the sub nodes cant be accessed in the editor
- Edited
jonSS my player node script is kind of full
What do you mean by "full"? Just add this function to existing code. Having it in top node is better design-wise, and calling it looks simpler.
Your setup is kind of strange with Player being a sole autoload. It could be fully automated if level scenes were instantiated manually. This way, somebody needs to notify the autoload that scene was swapped so it can adapt itself to the new scene.
You don't need different scripts for each level. Use the same script just export the properties like map_POS and adjust them in editor. With this, you don't have to type any new code regarding camera/player initialization when you create a new level scene. It's fully automatic.
- Edited
jonSS There's a way to do it without using scripts in levels, from the same place you change the scene. You can pass player position to Player.level_started()
and call it deferred from next_level
scene:
in player.gd:
func level_started(player_pos: Vector2):
var lvl = get_tree().get_current_scene().get_child(0);
position = player_pos
cam2D.limit_right = lvl.get_region_rect().size.x;
cam2D.limit_bottom = lvl.get_region_rect().size.y;
In next_level.gd:
if ( levelGO == true ):
levelGO = false;
get_tree().change_scene_to_file(nextLevel);
Player.call_deferred("level_started", Vector2(plrnextX, plrnextY))
This is then minimal intervention into your existing setup and it's more or less fully automated. You can as well add other arguments to level_started()
if needed.