Looks like you're not logged in

Login or Register to continue

Lesson Q&A

Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.

  • Randomise the spawn time tip!MartinBSo I did some tinkering with the timer and it's properties and found a way to randomise the spawn time. At the end of the `_on_timer_out()`** function, just pop in this little bit of code. `get_node("Timer").wait_time = randf_range(0.01, 1.0)` Basically adjust the first and last float variables of the `randf_range()` function and we add a little bit of luck to the mechanics. Random things are fun! 1 19 Mar. 03, 2024
  • Extra Challenge: Make it so that items don't clip outside the viewport.TheHoneydewWhen I saw that some of the Sprite textures clip outside of the viewport, I immediately wanted to think of a way to fix this. So I changed the minimum inside the float generators to 30 and in the maximum I subtracted 30 from both of the axis. This fixed it. I think this would be a fun extra challenge since It's just changing and adding some argument values. 7 11 Mar. 01, 2024
  • Placement of RandonItemPlacer in Space Level scene is importantJayLavoieHey! I just wanted to chime in real quick and let folks know to be careful of where they place their `RandomItemPlacer` asset within the Space Level Scene - make sure the transform origin is set to 0, 0. When I placed the `RandomItemPlacer` I accidentally placed it closer to the center of my scene, and it seemed to affect where the items were spawning in (a lot of times off screen). We could take in to account the offset position of the `RandomItemPlacer` in code, but figured it may be easier just to let folks know to place your asset at 0,0 origin. Thanks again for the great course, I'm really enjoying the lessons so far, and very much excited to dig into the 3D side of things down the road! Take care! 5 8 Mar. 10, 2024
  • Alternative Challenge SolutionBriar UndermoonTo solve the challenge I instead did this as I felt it was a cleaner solution that would scale against however many children the `RandomItemPlacer` initializes with. I also added some padding to avoid pickups spawning off screen. Likely over engineered for a project this small, but I like future proofing! ```gdscript var initial_children := 0 func _ready() -> void: initial_children = get_child_count() func _on_timer_timeout() -> void: var viewport_size := get_viewport_rect().size var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() var random_position := Vector2(0, 0) if get_child_count() < (6 + initial_children): random_position.x = randf_range(40, (viewport_size.x - 40)) random_position.y = randf_range(40, (viewport_size.y - 40)) item_instance.position = random_position add_child(item_instance) ``` 6 4 Jun. 25, 2024
  • Trouble with signals in the challenge scenario.few-apeI am really struggling with connecting the area_entered signal in order to process my function to -1 from my item_count. I assume the signal connects from the item.gd script to the random_item_placer.gd script but where? Is it the same signal connecting to the ship.gd script and I just also need to connect the same signal to the random_item_placer? Or do they need to be set as a separate signal on both the item.gd script and the random_item_placer.gd script? The tip suggests connecting it to the item_instance variable but that variable is within the on_timer_timeout function. Where do I put the following line of code to recognized when and item is destroyed within the random_item_placer.gd script?: item_instance.area_entered(item_destroyed) 6 4 Apr. 12, 2024
  • A Different Method Of Only Spawning 6 Items At A TimeJayV3DJust thought I'd share a different method to the challenge - adding the item instances as a child of another node, then checking the amount of children. The way I have it here, it keeps spawning new items but deletes the oldest item so there are only 6 at once. ```gdscript extends Node2D var item_scenes := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout(): var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() var item_group := get_node("Items") item_group.add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(20.0, viewport_size.x-20.0) random_position.y = randf_range(20.0, viewport_size.y-20.0) item_instance.position = random_position if item_group.get_child_count() > 6: item_group.get_child(0).queue_free() ``` 4 3 Apr. 18, 2024
  • PackedScene in the codemarinaton```gdscript var random_item_scene: PackedScene = item_scenes.pick_random() ``` Why do we need to add a colon and write PackedScene after it? We also need to write the same way if we want to store a node in a variable (from the "Ship Boosting" lesson): ```gdscript var timer: Timer = get_node("Timer") ``` Do they act as type hints or something else? :p 4 3 Mar. 12, 2024
  • How to set the seed of the random generator?rusty_bitsFor reproducibility, eg. for debugging, it would be important to set a random seed before using methods such as `Array.pick_random` or functions such as `randf_range`. How could I go about that? 3 3 Feb. 20, 2024
  • Issue with challengelazy-barracudaHello! I've read your explanation several times, but I still don`t get where should I place signal `item_instance.tree_exited.connect()` For now I wrote this code, using on area entered signal and it works for me. Could you kindly explain as for first grade student? <3 ```gdscript extends Node2D var num_items := 0 var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if num_items == 6: return else: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position num_items += 1 func _on_ship_area_entered(area: Area2D) -> void: num_items -= 1 ``` 4 2 Jul. 20, 2024
  • Why doesn't name completion work for randomly picked scenes?HardRockEven though we provided PackedScene as a type hint when picking a random scene, for the variable in which we store this scene, Godot doesn't provide class method name completion as I start writing .instantiate() after the variable name. Why is this? I could understand this without providing the type hint, since then there would be no way for the editor to know what would be the type of the variable (or more precisely it would be Variant, as Godot's very helpful error explained to me), but I thought type hints should help with name completion as well. Am I missing something? 3 1 Oct. 22, 2024
  • Question about scene names in remote scene dockmilk_manWhen running the game, every second a HealthPack or Gem appears as a child node of RandomItemPlacer. Now intuitively, I would think that the naming order in the remote scene dock would look something like this: **HealthPack** **HealthPack2** **Gem** **HealthPack3** **Gem2** **HealthPack4** Instead, I get something like this: **HealthPack** **@Area2D@3** **Gem** **@Area2D@4** **@Area2D@5** **@Area2D@6** Why is that? 2 1 Sep. 18, 2024
  • Can also use get_child_count()E71 is AW350M3!You can also use get_child_count() to determine is RandomItemPlacer has less than 7 children. 1 1 Jul. 05, 2024
  • get_chldren() for challengeKalmaroSo, I was looking over how other people cleared the challenge and wasn't really getting it. I was trying to increment when stuff went up and down but was trying to access variables. Then I found the get_children() function and that kinda handled everything for me! I ended up doing something like: `var active_items = get_children()` `if active_items.size() <= 6: # Only spawn if there are less than 6 items` Is this a viable solution too? It seems to be working fine for me. I was talking with copilot to brainstorm ideas. 1 1 Jun. 15, 2024
  • When do I use get_node() to connect signals?PixelbladeI understand that the we need to reference the source node (node that emits the signal) when connecting a signal in a script. ```gdscript # extract from script attached to "random_item_placer.tscn" extends Node2D func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_connect) ``` Why is it necessary to reference the source node in some examples but not others? For example, when we setup the signal for the ship, gem or health pack to detect the signal "area_entered", it was not necessary to use get_node(). ```gdscript # extract from script attached to "ship.tscn" extends Area2D func _ready() -> void: area_entered.connect(_on_area_entered) ``` Is it because the script for the ship, gem and health pack are attached to the root node, while the script for the Timer is a child node of the Node2D "RandomItemPlacer"? And, only a signal emitted from the root node is considered to emit from itself and does not require a reference to the source node? I don't get pattern when to use get_node() and when not to use it. 1 1 Jun. 09, 2024
  • (Challenge) Pickups Destroying Each OtherArcangelZero7I thought in a "real life" scenario, if this were an item-collecting game, it would be quite a bug if items annihilated each other from spawning on each other. Especially if this behavior was imperceptible and seemingly random to the player! So I tried some code that's a bit spaghetti but just wanted to see how my logic would work out. What if I could check if the space was clear first, before spawning the powerup? I added an `Area2D` called "SpaceChecker" with a child `CollisionShape2D` as a child to `RandomItemPlacer`. I thought maybe I'd use a signal to say "Hey, this space isn't empty because I'm being collided with!" but that turned out too complicated. Plus, since our `item.gd` script `queue_free()`'s on ANY collision...everything was just blinking in and out of existence! So, scrapping the signal idea, I looked at "collision layers / masks", and also the docs showed that Area2Ds have a `has_overlapping_areas()` method! Bingo? When the script generates a random position, I plop `SpaceChecker` there too, and see if it's occupied. This doesn't seem to have completely eliminated the collision-despawn problem, especially on very short timer settings, but it has helped significantly! I do still wonder why some get through, and wonder if something like framerate might be a factor. I am curious why I don't end up with a situation where the number generator just stops landing on valid coordinates and stops spawning new items. Hm. Here's what I ended up with though, in case anybody finds it interesting! ```gdscript func spawn_item(): var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() # Buffer zone to subtract from viewport size, to avoid spawning awkward edge power-ups. var spawn_window_margin := 80.0 var viewport_size := get_viewport_rect().size - Vector2(spawn_window_margin,spawn_window_margin) var spacechecker := get_node("SpaceChecker") var random_position := Vector2(0,0) random_position.x = randf_range(0,viewport_size.x) random_position.y = randf_range(0, viewport_size.y) # Drop the SpaceChecker collider on the same spot we plan to spawn a pickup! item_instance.position = random_position spacechecker.position = random_position # Check if anything is in the way before spawning! If not, spawn. Else, do nothing. if $SpaceChecker.has_overlapping_areas() == false: add_child(item_instance) else: pass ``` 9 1 May. 27, 2024
  • [solved] Question about remote scene trees and Root nodesragsdr"Remember how we saw that in the remote scene tree, we could see a root node?" I don't see the root node anywhere. Edit: I figured it out. The game needs to be running to see the Remote scene tree. So run the game, alt+tab back to Godot, and the option to see the remote or local scene trees will be available. 1 1 Apr. 25, 2024
  • Challenge - Using Node method 'get_child_count()'?well-to-do-owlHello, I tried the challenge before looking at the hints and was wondering if my solution is also appropriate? I just added two lines of code at the start of the timeout function ```gdscript var current_item_amount := get_child_count() if current_item_amount <= 6: ... ``` Then the rest is the same except indented underneath the if statement, I'll post the entire code at the end. I figured this might be easier since there's a built in method for Node to get the number of children, and since there's nothing in this scene except for the timer and the instantiated items. It seems to work well, but wondering if there's any issues in terms of optimization, coding practices, etc. Thanks! 3 1 Apr. 14, 2024
  • What's wrong with my code? Children still spawn in the top leftbold-goldfinchAlso, when I do print(viewport_size), I get (0,0). Is that supposed to happen? ```gdscript extends Node2D var viewport_size := get_viewport_rect().size var item_scenes := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] func _ready(): get_node("Timer").timeout.connect(_timer_timeout) func _timer_timeout(): var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var random_position := Vector2(0,0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position ``` 1 1 Mar. 15, 2024
  • Confused about the correct sequenceHermundureWhy does this sequence of code works, even though the item_instance.position is set AFTER the child has been added? Could anyone give an advice here? ```gdscript var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) item_instance.position = random_position ``` 8 1 Mar. 10, 2024
  • Check default valuestesfalconBy default, my global scene position was check as "centered" so 0,0 was in the middle of the scene node. By unchecking that, all my position values were now positive since 0,0 was now in the top left corner as it ought to have been. It also cleared up the confusion between to_global and to_local positioning. 1 1 Mar. 09, 2024
  • What is the difference under the hood between PackedScene, instantiated scene and scene added as a child? PJI need to understand why spawning items requires basically three steps. Will it always work like this? 4 1 Mar. 05, 2024
  • position v global positionHilton184Here we set the position. I think for this example it would be identical if we set the global position, since I tested that and the items seem to spawn in the same place. In what context do you choose to use global position instead of position? What difference does it make and can you give some examples of when to choose one instead of the other? 4 1 Feb. 23, 2024
  • Array orderarhinostormI found that when I did the practice it failed one of the tests if I put the energy pack first like this: `var collectible_scenes := [` ` preload("energy_pack.tscn"),` ` preload("coin.tscn")` `]` if I change the order it passes the test: `var collectible_scenes := [` ` preload("coin.tscn"),` ` preload("energy_pack.tscn")` `]` Super minor, but the lesson doesn't mention a specific order is needed to pass the test. 3 1 Feb. 21, 2024
  • Optimizing syntaxmq95Hi, while I was doing the practice, since I did not remember how I did it during the explanation, I wrote this code using my own reasoning: ```gdscript var random_item: PackedScene = collectible_scenes.pick_random() add_child(random_item.instantiate()) ``` It actually worked and passed the test, but then I went back and looked at the explanation, and there it was structured like this: ```gdscript var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) ``` I was wondering wether the syntax I used would be considered better or worse. Is the extra step in this code just for teaching and readability purposes? Is there any actual difference when running the code? After all, if I understood correctly, the variable is just a name we assign to the thing. It should work like math equations right? It ultimately doesn't matter wether i write: ```gdscript x = 2 + (3 + 5) ``` or ```gdscript y = (3 + 5) x = 2 + y ``` the outcome is the same. Which of the two ways of writing code would an actual programmer use though? I guess it depends on the complexity of the code? 4 1 Feb. 20, 2024
  • Difficulty Ramps Up Heremild-stingrayI feel like this is the first lesson that doesn't include a lot of beginner lingo and rushes through some seemingly advanced coding without simplifying what's happening. When you all do an audit of this module, is there a way you can go back through and simplify/clarify some of the explanations. For the first time over the course of this class, I'm not sure I understood what was going on at all. 3 0 Dec. 04, 2024
  • Limiting spawns based on groups.regular-falconHello! First time posting here, I do hope this hasn't been addressed already but I was amazed by the group system and ended up doing a little experimenting in limiting spawns based on the combined group size of health_packs and gems so that they could keep spawning while under said limit. I was curious as to if this would be considered efficient in a larger scale project or if there are more intelligent ways to track and limit based on current existing instances of objects. ```gdscript extends Node2D .... var item_limit := 0 ``` ```gdscript func _on_timer_timeout() -> void: item_limit = get_tree().get_nodes_in_group("health_pack").size() + get_tree().get_nodes_in_group("gem").size() if(item_limit < 6): .... .... ``` 1 0 Nov. 26, 2024
  • error with Timer in instatiated Random placer node AND error with type need to define as Node.kindly-falconI have copy paste random_item_placer.gd from webpage (to avoid any errors). And it works if I play the scene; random crystals and health pack are spawn. But; when I instantiate RandomItemPlacer in space_level scene, I receive an error: E 0:00:01:0245 random_item_placer.gd:11 @ _ready(): Node not found: "Timer" (relative to "/root/Level"). `func _ready() -> void:` `get_node("Timer").timeout.connect(_on_timer_timeout)` I cannot understand why this code is not working as instance under Level. But its fine if I just run a scene. I try `get_node("RandomItemPlacer/Timer").timeout.connect(_on_timer_timeout)` but not any better. 3 0 Nov. 21, 2024
  • A bit more info on get_viewport_rect()eager-hyenaJust to get a more comprehensive sense of the function, get_viewport_rect() provides the size and position of the viewport. We used the size Vector2 value, but I'm curious about the position value as well. Is this position relative to the display it's running on, or is it relative to the space within the game itself? Was curious if the position Vector2 was more relevant to running in windowed mode than, say, running in full screen. Thanks for any clarification you can provide! 1 0 Nov. 18, 2024
  • _on_timer_timeout()Norbert```gdscript func _on_timer_timeout(): var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) ``` In the function "_on_timer_timeout()" I would expect a small window to pop up in line 3 - when I enter the dot after "random_item_scene" - with suggestions for selecting the possible methods. What am I missing: - is it because of my settings? - or have I not understood something? 4 0 Nov. 15, 2024
  • Challenge solution and question about Instantiated scenes in memoryEliorimMy solution works perfectly I just have a question that won't leave me be. Wondering what happens to instantiated scenes if they do not enter the tree? In my challenge solution I write: ```gdscript extends Node2D var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] var item_count := 0 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() if item_count < 6: add_child(item_instance) item_count += 1 var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_instance.tree_exited.connect(item_collected) func item_collected() -> void: item_count -= 1 ``` So in my `_on_timer_timeout` function, when `item_count < 6` returns false, we do not add the instance to the tree. What then happens to the instantiated scene? Is it automatically cleared from memory by Godot to prevent a memory leak? If so, does this happen next frame or the next time the timer sends a `timeout` signal? 2 0 Nov. 07, 2024
  • How to connect timerdofudengameshow would you connect the timeout function? 2 0 Oct. 27, 2024
  • Spawning items overlapsill-fated-heronHi, Is there a way to prevent item spawning at the same position? Maybe a call before the add_child? And then you can generate a new random position if there is already an item there? 2 0 Oct. 23, 2024
  • My challenge solution + Quick questionsPurpleSunrise**A simple solution but it works.** ```gdscript extends Node2D var item_scenes :=[ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] var items_spawned_count := 0 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _process(delta: float) -> void: print (items_spawned_count) func _on_timer_timeout(): if items_spawned_count < 6: var random_item_scene : PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0,0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_instance.area_entered.connect(_on_area_entered) items_spawned_count += 1 else: return func _on_area_entered(area_that_entered : Area2D): items_spawned_count -= 1 ``` **Solution after hints using tree exited signal:** ```gdscript extends Node2D var item_scenes :=[ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] var items_spawned_count := 0 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _process(delta: float) -> void: print (items_spawned_count) func _on_timer_timeout(): if items_spawned_count < 6: var random_item_scene : PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0,0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_instance.tree_exited.connect(_on_tree_exited) items_spawned_count += 1 else: return func _on_tree_exited(): items_spawned_count -= 1 ``` I think `tree_exited` signal is better because in that case the count is -1 only if the node is actually freed from the scene tree? Also is it better to have ` if items_spawned_count < 6:` or `if items_spawned_count == 6`? Last one: What's the difference between `return` and `pass`? They both work and in this case the function would return nothing wouldn't it? 1 0 Oct. 14, 2024
  • L7.P1 doesn't seem to workacidic-mandrillI'm trying to complete the practice, and it seems to be working fine when I run the program, but it keeps failing because the random positions don't match with the reference. ```gdscript extends Node2D var collectible_scenes := [ preload("coin.tscn"), preload("energy_pack.tscn")] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: var random_collectable_scenes : PackedScene = collectible_scenes.pick_random() var collectible_instance := random_collectable_scenes.instantiate() add_child(collectible_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0,0) random_position.x = randfn(0,viewport_size.x) random_position.y = randfn(0,viewport_size.y) collectible_instance.position = random_position ``` However, when I run the program, the reference spawns every collectable at the center of the screen. I tried removing lines 15 and 16, and the code passed the practice, so I'm thinking the reference code is bugged somehow? 5 0 Oct. 12, 2024
  • About Distant Tab from RandomItemplacerHazlarHello, I launched the randomItemPlacer scene, observing the Distant tab to observe the instantiation, and sometimes it instantiates with the real name of the node like Gem or Healing_pack and sometimes by @Area2d@3 and it is the tooltip that lets me know which scene is instantiated, I was wondering if it is a bug or is it normal. Thanks, I reread the course from the beginning, and lots of new curiosities come to me. 1 0 Oct. 09, 2024
  • Extra challenge question!miniature-gazellehello! i got it working so it spawns a maximum of items ```gdscript func _on_timer_timeout() -> void: if item_count == 6: return else: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_count += 1 item_instance.tree_exited.connect(_on_item_collected) print(item_count) func _on_item_collected() -> void: item_count -= 1 ``` and then i thought, why not use the `tree_entered` to add 1 to the item count just like the `tree_exited` . So i did just that and created a function that adds 1 to the item count each time the `tree_entered` signal is called...and nothing happened haha. i used a print(item_count) and saw that the item count was always 0. why is that? 2 0 Sep. 26, 2024
  • L7.P1 code troubleHodeoboBaggins**This is the code I have in random_collectible_placer.gd** extends Node2D var collectible_scenes := [ preload("coin.tscn"), preload("energy_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: var random_item_scene: PackedScene = collectible_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position **I fail the third part of the test but the packs are appearing in random places. Where did I go wrong?** 2 0 Sep. 16, 2024
  • Connectingbrief-whaleThe challenge works for me (code below) but sometimes it is difficult to understand where to connect the signals. Is it possible to send a singal to the RandomItemPlacer through the gem.gd via another node that is not in the same scene as an example? My current link is in the RandomItemPlacer. item_instance.tree_exited.connect(counter) I have now placed it directly in the node of RandomItemPlacer this works direct under add child. item_instance.area_entered.connect(counter) this does not work or do I have to place it somewhere else because it is not defined in _ready. But now the signa connects every time, which is not good because of the performance, is it? Thanks :) ------------------- extends Node2D var item_count :int = 0 var item_scene := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if item_count == 6: return else: var random_item_scene: PackedScene = item_scene.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) item_count += 1 item_instance.tree_exited.connect(counter) #item_instance.area_entered.connect(counter) var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position func counter() -> void: item_count -= 1 1 0 Sep. 15, 2024
  • Little typoThatDarnLagThere's just a little typo in this paragraph: The good news is, random spawning is working, but our collectibles are detecting each other and disappearing. This is because they're areas, and their code is to destroy themselves when they detect another area. So, they're working as we coded them to! Which is right under the part where we randomize which item spawns. Instead of the last word being "to," it should be "too." 1 0 Sep. 10, 2024
  • issues with challengeA weird catI tried, but I do not understand how to do the challenge at all. Could somebody please copy paste the complete code for random_item_placer.tscn with the challenge? 1 0 Sep. 08, 2024
  • Why use randf_range() instead of randi_range()adiWhy we use a function that returns float (`randf_range`) instead of int (`randi_range`) to set the position in px? Is it so that the objects don't overlap perfectly? Isn't it better to set the position of the node by `item_instance.position = random_position` before `add_child(item_instance)` ? 2 0 Sep. 06, 2024
  • Odd issue with Camera 2d nodeDeadDreamsWhisperingFor some reason, adding the camera 2d node causes the gem to appear decentered but when I remove it the gem is centered. This seems to be the opposite of what should be happening? 1 0 Sep. 03, 2024
  • I don't understand why I can't give the ship access to the random item placer scriptsuperior-aardvarkI'm trying to give the ship scene access to an items_on_screen variable I created in the random_item_placer script so more items can spawn after i collect one. I have tried both creating a reference to the random_item_placer node and using the get_node function, but I keep getting an error saying the identifier is not declared in the current scope, meaning the ship scene still doesn't have access. Here's my code for the ship script: ```gdscript extends Area2D @onready var random_item_placer: Node2D = $"../RandomItemPlacer" var max_speed := 1200.0 var velocity := Vector2(0, 0) var steering_factor := 3.0 var health := 20 var gem_count := 0 func _ready() -> void: get_node("res://lessons/random_item_placer.gd") area_entered.connect(_on_area_entered) set_health(health) func _process(delta: float) -> void: var direction := Vector2(0, 0) direction.x = Input.get_axis("move_left", "move_right") direction.y = Input.get_axis("move_up", "move_down") if direction.length() > 1.0: direction = direction.normalized() var desired_velocity := max_speed * direction var steering := desired_velocity - velocity velocity += steering * steering_factor * delta position += velocity * delta if velocity.length() > 0.0: get_node("Sprite2D").rotation = velocity.angle() func set_health(new_health: int) -> void: health = new_health get_node("UI/HealthBar").value = health func _on_area_entered(area_that_entered: Area2D) -> void: if area_that_entered.is_in_group("gem"): set_gem_count(gem_count + 1) items_on_screen -= 1 elif area_that_entered.is_in_group("healing_item"): set_health(health + 20) func set_gem_count(new_gem_count: int) -> void: gem_count = new_gem_count get_node("UI/GemCount").text = "x" + str(gem_count) ``` And the code for the random_item_placer script: ```gdscript extends Node2D var item_scenes := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] var items_on_screen := 0 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _process(delta: float) -> void: pass func _on_timer_timeout() -> void: if items_on_screen == 6: return else: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) items_on_screen += 1 var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position ``` 7 0 Sep. 02, 2024
  • Stumped: item location outside viewportdouble-ramEven with `RandomItemPlacer` position at 0,0, all items are spawned in the bottom right or outside the viewport. Solutions from other questions have not helped. ```gdscript extends Node2D var spawn_count := 0 var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) print("Placer position", position) # 0,0 var viewport_size = get_viewport_rect().size print("Viewport size", viewport_size) print("Viewport position", get_viewport_rect().position) func _decrease_item_count(): spawn_count = spawn_count -1 func _increase_item_count(): spawn_count = spawn_count + 1 func _on_timer_timeout(): if spawn_count > 5: return _increase_item_count() var item: PackedScene = item_scenes.pick_random() var item_instance = item.instantiate() add_child(item_instance) var viewport_size = get_viewport_rect().size var rand_position = Vector2(0,0) rand_position.x = randf_range(0, viewport_size.x) rand_position.y = randf_range(0, viewport_size.y) print("spawn@", rand_position) item_instance.position = rand_position item_instance.tree_exited.connect(_decrease_item_count) ``` Output: > > `Placer position(0, 0)` > `Viewport size(1920, 1080)` > `Viewport position(0, 0)` > `spawn@(45.48315, 1041.273)` > `spawn@(1408.055, 329.1118)` > `spawn@(932.6107, 912.0853)` > `spawn@(390.211, 583.8771)` > `spawn@(1079.063, 117.3772)` > `spawn@(1538.91, 255.0017)` I also don't see any weird stuff in the Remote view when running: there is no camera anywhere for instance. I even recreated the whole placer again from scratch and placed it at 0,0 manually in the 2d view. No difference. Hacky fix (but shouldn't be necessary): ```gdscript rand_position.x = randf_range(-viewport_size.x/2, viewport_size.x/2) rand_position.y = randf_range(-viewport_size.y/2, viewport_size.y/2) ``` Any thoughts? 3 0 Sep. 01, 2024
  • Question about ".connect()" in re: challenge at the end of L7.graceful-clam```gdscript extends Node2D var item_amount := 0 var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if item_amount == 6: return var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0,0) random_position.x = randf_range(0,viewport_size.x) random_position.y = randf_range(0,viewport_size.y) item_instance.position = random_position item_amount += 1 item_instance.tree_exited.connect(item_remover_update) func item_remover_update(): item_amount -= 1 ``` I'd like to see if I properly understand the logic behind the .connect() function. In _ready() we grab the node and prepare it, but since its in a function and not a global variable it can only be used locally in the function. However, we call the .connect() function to attach it and be callable for another function, effectively extending its usecase? Moving on the the challenge part, the hint said to use "item_instance.tree_exited.connect()". I knew I couldn't access the item_instance outside the func since I created it inside. And I needed the update to happen each time the program ran not when the timer timed out - so I tried the .connect() function to extend it item_remover_update in order to see a queue_free(). This worked when I ran it. Have I understood the concept of this, or did I simply fluke a solution? 3 0 Aug. 18, 2024
  • Problem with native type Node2DgonzalopmHi there, i´ve completed lesson, but my code doesn´t run! I´ve added random_item_placer to main scene (space_level), but when i try to run, this issue appears: "Script inherits from native type "Node2D", so it can´t be assigned to an object of type: "Timer" My code (random_item_placer.gd) ```gdscript extends Node2D var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _process(delta: float) -> void: pass func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position ``` 3 0 Aug. 17, 2024
  • Solved issue but had difficulties with items appearingHappyI've been wracking my head against this for a while but I realised where I was going wrong. I believe I wasn't setting up the connection in the correct place since the variable item_instance was created locally and trying to set up the location anywhere but that local section would have issues with it not being declared. I was able to figure that out, however I ran into a different issue. Where I would run the game and sometimes 2 items would appear, or 3 (it would be a random number each time I ran the scene). If I collected the items that appeared then only the number of items that appeared on my screen would spawn again. It was only when I reduced the size of the random position, that did all the items appear and I could collect them. ```gdscript random_position.x = randf_range(0, viewport_size.x/2) random_position.y = randf_range(0, viewport_size.y/2) ``` 6 0 Aug. 13, 2024
  • I tried playing the game a different waydofudengamesi moved my ship so that it was trying to find the pickup always and it would charge but not turn unless it went past the pickup, and it moves really neat! 1 0 Aug. 12, 2024
  • My challenge solution + questionMatejAt first I tried to complete the challenge with 'child_exiting_tree(node: Node)' signal. I only had the first two hints revealed at the time, but quickly realized I have no idea how to make it work (utilizing only knowledge I learned from the course so far). I tried to look at the documentation inside Godot to no avail. But with the help of remaining hints and discussion posts (in particular "Trouble with signals in the challenge scenario." posted by @few-ape) I settled on a rudimentary solution: (While writing this I realize I could add a condition to check if the spawned_item_count is > 0 and only then decrement it) ```gdscript extends Node2D var item_scenes := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] var spawned_item_count := 0 func _ready() -> void: # Get access to child node Timer, # connect its timeout signal to a function get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if spawned_item_count < 6: # Storing viewport size var viewport_size := get_viewport_rect().size # Storing random x and y position in a Vector2 variable # with viewport size as the upper limit var random_position := Vector2(0, 0) random_position.x = randf_range(20, viewport_size.x - 20) random_position.y = randf_range(20, viewport_size.y - 20) # Picking a scene at random from an array # instantiating it and adding it as a child var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() item_instance.position = random_position add_child(item_instance) # Connecting signal to a function item_instance.tree_exited.connect(_decrement_on_tree_exited) # Update the spawned_item_count +1 spawned_item_count += 1 func _decrement_on_tree_exited() -> void: # Update the spawned_item_count -1 when item_instance exits scene spawned_item_count -= 1 ``` My follow up question is about line 36 'item_instance.tree_exited.connect(_decrement_on_tree_exited)' At first I didn't understand why it's written that way and how it works. After thinking for a while I think I understand, but would like to confirm. 👇 My current understanding: we store the instanced scene (which essentially is just a node or a node tree) inside a variable (line 31), and that variable type is now a node (is that technically correct?). And therefore we can access the node signals, like I did in the line 36. 1 0 Jul. 30, 2024
  • My solution to the challenge : )Sandra Moen```gdscript func _on_timer_timeout(): var num_item_instances := 0 for child in get_children(): if child.is_in_group("gem") or child.is_in_group("healing_item"): num_item_instances += 1 if num_item_instances > 5: return . . . ``` - Easy to set up - O(n), could be faster - Only works for predefined items: so it doesn't scale dynamically, if new group names it will break if not updated - if-return to not need lower code to be indented, easier to read 1 0 Jul. 22, 2024
  • New scene root node optionsYourFriendJoeyWhen we make a new scene, what is the thought process for choosing what is the root node for that scene? For example at the beginning of the lesson, we make a new scene using the Node2D as the root. Could I have used the base "Node" for the root? And for another example in this module, the ship's root node is an Area2D. Would there be a major difference if the ship scene's root was a Node2D, with the Area2D and other nodes being a child of that? Thanks! 4 0 Jul. 08, 2024
  • Question about instanciationHazlarHello, In the practice code, I made a mistake (line 11 ) writing random_collectible_scene instead of collectible_instance.position = random_position and it gave me an error saying I want to assign a vector2 to a packedScene but it seems to me that collectible_instance is also a packedScene. Reading some post above I read that the instantiation procedure was to load the original, copy it (instantiate) and add it as a node. My mistake makes me assume that first I was using the original but it was not the unpacked scene so I did not have access to those sub properties inside, is this true? ```gdscript func _on_timer_timeout() -> void: var random_collectible_scene: PackedScene = collectible_scenes.pick_random() #pass var collectible_instance := random_collectible_scene.instantiate() # add_child(collectible_instance) # var viewport_size := get_viewport_rect().size var random_position : Vector2 = Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) collectible_instance.position = random_position ``` 1 0 Jun. 30, 2024
  • SOLVED: get_node("Timer") giving error message.reliable-caterpillarHi all, when running the code below I'm getting an error message; As far as I can see, the ready func isn't able to locate the node "Timer" - even though the timer is currently visible in my node dock as a child of 2dNode "RandomItemPlacer" as per the lesson. I've spellchecked and done my best to ensure that it should work and I'm convinced its some deeper compatibility issue above my understanding. Please see my code below and let me know if you have a fix! Thanks for the awesome lessons. E 0:00:00:0874 random_item_placer.gd:9 @ _ready(): Node not found: "Timer" (relative to "/root/RandomItemPlacer/Timer"). <C++ Error> Method/function failed. Returning: nullptr <C++ Source> scene/main/node.cpp:1639 @ get_node() <Stack Trace> random_item_placer.gd:9 @ _ready() ```gdscript func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timout) ``` 3 0 Jun. 28, 2024
  • get_viewport_rect() and Godot DocumentationIronEvaOut of pure curiosity, I looked up get_viewport_rect() on how developers (like yourselves) just seem to know there is a .size property within the functionality. I typed in get_viewport_rect(), looked on the Godot documentation page and couldn't find anything there. But when I highlighted get_viewport_rect() and right-clicked and searched the Lookup Symbol it was there but didn't mention anything about the get_viewport_rect().size. I guess my question is, how do you know these properties exist? 2 0 Jun. 27, 2024
  • get_node("Timer") doesn't auto complete/typeIronEvaHey all :), I know in the previous lessons the course states the preferred method at GDquest is to use the get_node() function to be able to manipulate nodes. This is all good, but I wondered if there was a way to have it auto-complete when I use it. For example, if I use the get_node("Timer") and then put a "." at the end of it, nothing relevant to the Timer node shows up. But if I drag and drop the node into the script and put a dot at the end of the timer "$Timer." the auto-complete starts showing which is super useful. Is there a way to have it so the get_node() can do the same as the $? 2 0 Jun. 25, 2024
  • A lot of questions M5.L7Nikita Myshkin**1.**Why don't we write the `var` type in ```gdscript var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) ``` **2.**What mean `PackedScene`? I don't see this class in the inspector. And should I even see him? **3.**`var random_item_scene: PackedScene = item_scenes.pick_random()` Why do we put only `:` and not `:=` `after ar random_item_scene` **4.**Why is the `size` property not in the inspector? `var viewport_size := get_viewport_rect().size` **5.**The fifth point is the most terrible and incomprehensible to me. I've been trying to do this challenge a lot. At first I tried to do as in hint 4, but the word RETURN is a magical mystery to me. I don't understand what it should return and why. I found the best option for me, but he doesn't want to work at all. Here it is: ```gdscript extends Node2D var items_in_scene := 3 var item_scenes := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] func remove_items() -> void: if items_in_scene > 3: items_in_scene -= 1 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position add_child(item_instance) item_instance.tree_exited.connect(remove_items) ``` On the one hand, I understand why it doesn't work. The `items_in_scene` variable is not attached to anything, these are empty numbers without content. But then I think I added my `remove_items()` function to `_on_timer_timeout()` and connected it to the `_on_timer_timeout()`. And now I thought that everything would work, but no. Objects appear in an infinite number and appear. Please help me figure it out. 6 0 Jun. 23, 2024
  • The extra challengewell-groomed-seahorse So i tried to think of a way to do it. Its probably terrible but it seemed to work on its own node but when I entered it on the test node. two problems occur The item count just went to 6 and starting adding from there I have to put less then 5 then less then 6. which is minor but its not accurate ```gdscript extends Node2D var collectible_scenes := [preload("res://practices/L7.P1.spawn_random_items/coin.tscn" ), preload("res://practices/L7.P1.spawn_random_items/energy_pack.tscn") ] var item_count = 0 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func set_item_count(new_item_count: int)-> void: item_count = new_item_count get_node("Control/ItemCount").text = "x" + str(item_count) func _on_timer_timeout() -> void: var viewport_size := get_viewport_rect().size var random_item_scene : PackedScene = collectible_scenes.pick_random() var random_position := Vector2(0,0) var Instance_random_item := random_item_scene.instantiate() random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) Instance_random_item.position = random_position if item_count <= 5.0: add_child(Instance_random_item) set_item_count(item_count + 1) ``` 3 0 Jun. 20, 2024
  • Variable inception, is it just for simplification?ConradI found that writing the following code: ```gdscript var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) ``` To be a little bit confusing as its basically putting variables within variables and when I try to read and make sure I'm properly understanding what I'm writing, my brain begins to overload like trying to understand the movie inception. I found writing the code like this, still worked and also was a bit easier to follow along for me as it was contained in a single line without any fragmentation ```gdscript add_child(item_scenes.pick_random().instantiate()) ``` Is there any concerns by writing code like this, perhaps it is less adaptable? 3 0 Jun. 10, 2024
  • Challenge check and other questionp_dev_Please, I'd like to know if there is some problem with my code here: ```gdscript extends Node2D var item_scenes = [ preload("gem.tscn"), preload("health_pack.tscn") ] var item_count = 0 func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position if item_count == 6: return else: item_count += 1 add_child(item_instance) item_instance.tree_exited.connect(_on_tree_exited) func _on_tree_exited() -> void: item_count -= 1 ``` Also, I have noticed that sometimes, when there are six items on screen and I pick one of them, the new one takes the time to spawn, and sometimes it shows up immediately. Is there a problem with this? 3 0 Jun. 04, 2024
  • First challenge confusionpuzzled-magpieThe first challenge was a little confusing to me, because I deleted the ready function ("Remove the *`ready()` function from the script. It was just a test to see how to instantiate scenes in code.")* After that, I didn't know where to put my get_node line. But it turned out I should have kept the ready() function and just remove its content. 1 0 Jun. 04, 2024
  • Last Challenge: 3th hint datiswousI read the 3th hint and tried multiple things, but I can't find a way to propperly implement it. This doesn't work: ```gdscript extends Node2D var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] var item_count = 0 func _ready(): get_node("Timer").timeout.connect(_on_timer_timeout) func _custom_function() -> void: item_count -= 1 func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) var viewport_size := get_viewport_rect().size #print(viewport_size) var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_count += 1 item_instance.area_entered.connect(_custom_function) print(item_count) ``` The second hint in that hint I completelly don't understand. 4 0 May. 31, 2024
  • First remove the ready() function, but then it's suddenly needed in the chalenge..datiswousRight before the first chalenge it states: > Remove the `_ready()` function from the script. It was just a test to see how to instantiate scenes in code. So, no ready function then. I will never think of including the ready() function in the chalenge code, because it's not being used, I thought. Then in the solution it adds the ready() function.. 3 0 May. 28, 2024
  • Challenge Question: On the use of ArgumentsMuseDoesHi, I have a question about arguments in functions. I solved the challenge with the following code: --------------------------------------------------------------------------- var item_limit := 0  func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_item_collected(area) -> void: item_limit -= 1 func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() item_instance.area_entered.connect(_on_item_collected) if item_limit == 6: return add_child(item_instance) item_limit += 1 --------------------------------------------------------------------------- My question is why do I need to add the argument 'area' in the function: func _on_item_collected(area) -> void: item_limit -= 1 It fails to reduce the item_limit variable when written without the area argument like this: func _on_item_collected() -> void: item_limit -= 1 I wanted to experiment so I changed the connecting signal to use 'tree_exited' that was listed in the hints instead of 'area_entered' like this: item_instance.tree_exited.connect(_on_item_collected) When I use 'tree_exited' style of signal, the line '_on_item_collected() -> void:' the code works. Why do I need to clarify that the area_entered signal needs the area argument and not for the tree_exited? 3 0 May. 19, 2024
  • I found a simple solution to the 6 items challengeWlad-IFI```gdscript var max_items = 6 ... func _on_timer_timeout(): if (get_child_count() <= max_items): _spawn_random_item() ``` This is a simple solution I found: note: I put all the code for spawning Items in the *"*_spawn_random_item()*"* function The solution is to count how many children the Item Spawner node has and if it has 7 children (6 items + the timer node), then it will not spawn items. 1 0 May. 18, 2024
  • Challenge working, and I don't understand why :)machinemanIt's not clear to me why the challenge is working... I added: ```gdscript func remove_items() -> void: if item_count > 0: item_count -= 1 ``` And added this line inside of the `_on_timer_timeout()`function inside of the random_item_placer script ```gdscript item_instance.tree_exited.connect(remove_items) ``` But I'm not really sure why the placement for that line works. I guess inside of that function is the only place we know about the item_instance variable, but is this the right place? Am I just getting lucky since this function is constantly called when the timer runs out that it also happens to know when a singnal is called from that variable? Which is another confusing point, up until now only nodes had signals.. but since this is a variable with a scene in it... why does this work? When I started thinking about this before looking at the hints, I thought for certain I would have to do something in the health_pack script and somehow send the decrement notice back over to my script with the current item count in it :( Some of this is just stream-of-thought as I type... I am sure it will click if I mess around with it further. But wanted to get the group's thoughts! 3 0 May. 12, 2024
  • Trying to Despawn the items!machinemanFor a self-challenge, I decided to try to make the health packs disappear after 5 seconds... however, I am getting a hard Godot crash! (Not the editor, but the game executable when it launches). I added a Timer node to the root node of the Health Pack scene. Then, I connected the Timer to a function: ```gdscript get_node("Timer").timeout.connect(_on_timer_timeout) ``` I set the timer to 5 seconds, set it to One Shot and Autostart. Note that it works fine if I run only the health pack scene... I see one item spawn, then despawn 5 seconds later :) But when running the space_level main game, I get the crash. This is the code in the function: ```gdscript func _on_timer_timeout() -> void: queue_free() ``` 4 0 May. 07, 2024
  • Rect2 vs Vector2 ?machineman```gdscript var viewport_size: Vector2 = get_viewport_rect().size ``` So, I'm still struggling with the concept or variable inference, using := when defining variables. I still like to see the type spelled out, so I try to do that whenever I can during the lessons. Anyway, this led me to experiment with the get_viewport_rect() since the session mentioned it was a Rect2 type that described a rectangle. I don't know what I expected, but maybe 2 x,y coordinate sets? (2 Vector2s?) When I tried to use the Rect2 as a type above, I got an error. So I tried Vector2 and it worked... which, when printing the value and understanding it is the viewport size (1920x1080) it makes sense. So how is Rect2 actually defined as a data type and is it in play here, or am I just missing something :) 1 0 May. 07, 2024
  • Challenge questionWacksfordI fall extremely short on understanding the .connect, i have watched a couple of videos explaining it but i just cant understand the concept. i dont know what it does really, and i cant seem to understand the way that videos are explaining it either, this is what i have so far on the Challenge, i manage to get the drops to spawn up until there are 6 on screen but i dont know how to tell the computer that ive entered the area2d and collected one `extends Node2D` `var item_scenes := [` `preload("res://lessons/gem.tscn"),` `preload("res://lessons/health_pack.tscn"),` `]` `var item_can_spawn := true` `var item_on_screen = 0` `func _ready() -> void:` `get_node("Timer").timeout.connect(_on_timer_timeout)` `func _on_timer_timeout() -> void:` `if item_can_spawn:` `var random_item_scene: PackedScene = item_scenes.pick_random()` `var item_instance := random_item_scene.instantiate()` `add_child(item_instance)` `var viewport_size := get_viewport_rect().size` `var random_position := Vector2 (0,0)` `random_position.x = randf_range(0, viewport_size.x)` `random_position.y = randf_range(0, viewport_size.y)` `item_instance.position = random_position` `item_on_screen += 1` `if item_on_screen > 5:` `item_can_spawn = false` `else:` `item_can_spawn = true` `item_instance.area_entered.connect(item_pickup)` `func item_pickup(area):` `item_on_screen -= 1` 1 0 May. 05, 2024
  • Difficulties with the challengeFernandoHi, I got stuck in the challenge because of the error "Standalone lambdas cannot be accessed. Consider assigning it to a variable." My code was like this: ```gdscript extends Node2D var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn"), ] var item_count := 0 #challenge func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if item_count == 6: #Challenge return #Challenge var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) item_count += 1 #Challenge var viewport_size := get_viewport_rect().size var random_position := Vector2(0,0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_instance.area_entered.connect(decrease_count) #Challenge func decrease_count(item_touched) -> void: #Challenge item_count -= 1 #Challenge ``` The error was with the function "decrease_count" in line 33, but after a lot of headache trying to remember if this was already taught in the course I got it to work after moving the function "decrease_count" to below the variable "item_count" in line 10, like this: ```gdscript extends Node2D var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn"), ] var item_count := 0 #Challenge func decrease_count(item_touched) -> void: #Challenge item_count -= 1 #Challenge func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if item_count == 6: #Challenge return #Challenge var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) item_count += 1 #Challenge var viewport_size := get_viewport_rect().size var random_position := Vector2(0,0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_instance.area_entered.connect(decrease_count) #Challenge ``` So, I was kinda lucky to make it work, but I would like to know what happened that it gave an error like that and what "consider assigning it to a variable" means. 2 0 May. 04, 2024
  • Connecting a timer to the script and its signal to a functionNekrysIs there an easy way to understand the logic behind having first the "get_node("Timer"), after the timeout.connected to the function etc? I feel that sometimes I have issues finding the correct flow and what goes first and 2nd when writing gdscript. I usually understand the code and the logic behind it but constructing it by myself is a bit hard sometimes due to some very basic errors Would you say its mostly a matter of time and getting used to it overall? ```gdscript func _ready() -> void: #connect the Timer to the script and #connect the timeout signal to a function on the script get_node("Timer").timeout.connect(_on_timer_timeout) ``` 3 0 Apr. 24, 2024
  • linux to windows pcunripe-tarsierI was doing this course on my linux laptop and I'm switching to my windows pc. I was at this point. Can I copy just the files of the scenes, and assets, ... etc. ? 1 0 Apr. 21, 2024
  • Challenge Code CheckStonemonkeyI managed to get it working but I'm curious if my solution is bad or problematic in some way. ```gdscript extends Node2D var item_scenes := [ preload("gem.tscn"), preload("health_pack.tscn") ] var item_is_spawned : int = 0 #Challenge Code func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: if item_is_spawned < 6: #Challenge Code var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() add_child(item_instance) item_is_spawned += 1 #Challenge Code print(item_is_spawned) #Challenge Code var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position item_instance.area_entered.connect(item_is_destroyed) #Challenge Code func item_is_destroyed(placeholder): #Challenge Code item_is_spawned -= 1 #Challenge Code ``` 1 0 Apr. 14, 2024
  • Connecting nodes via code vs via the Node tabunhealthy-cheetahHow does connecting the Timer to the RandomItemPlacer using code in the `_ready()` function differ from establishing this connection through the editor's Node tab, if there's any difference at all? 3 0 Mar. 28, 2024
  • How come we can't get viewport_size as a variable at the start of the script?ShaneI was thinking that it'd be more efficient to just get the viewport size once, rather than each time **on_timer_timeout()** occurs... but when I put "**var viewport_size := get_viewport_rect().size**" at the start of the script I get the error "**random_item_placer.gd:6 @ @implicit_new(): Condition "!is_inside_tree()" is true. Returning: Rect2()**" How come this is the case? (Also sidenote I really like the "Challenge: Do you remember how to connect a signal in code?" section.. I think its a great way to apply knowledge quickly without doing a full practice) 3 0 Mar. 26, 2024
  • Does the calling order of add_child and the set of the position affect the behaviourNPGameDevHi, as the title describes, I've been setting the position before calling add_child on the instance, and it seemed to work fine, but then I noticed that the code from the lesson sets the position after calling add_child, so: - Is there any reason to execute them in that order? - Are there any side effects of using one or the other? - If so, in which scenarios is one preferrable over the other? My execution order: ```gdscript random_item_instance.position = random_position add_child(random_item_instance) ``` The lesson's execution order: ```gdscript add_child(random_item_instance) random_item_instance.position = random_position ``` Thanks! 4 0 Mar. 23, 2024
  • Pointers on Object TypesJankiKhanSo one thing I'm having a hard time wrapping my head around, specifically for autocompletion. I'm not sure if this is a weakness of GDScript or if I'm doing something wrong. Generally I like to know the type of each object I have, specifically so that autocompletion works. Sometimes I have to do both `:=` and `as Something` together to be able to get autocompletion, sometimes just `as Something`, generally `:=` alone doesn't always help. Are there general guidelines to writing and assigning objects in a way that keeps autocompletion intact? It would be much easier than having to remember the exact property I'm looking for. Thanks! 5 0 Mar. 18, 2024
  • Item disappearing if generated too fastMilky DaddyI tried setting the timer to 0.1s, and I notice that if the new items are generated too fast, some old items that were already on the screen would disappear for no reason. Why is that? Does it mean there is a max limit to add_child()? 1 0 Mar. 02, 2024
  • L7.P1 Possible bug in lesson?MartinBThe last check failed because I had my array in a different order to the solution, it didn't like that I preloaded the energy pack first. 1 0 Mar. 01, 2024
  • Question about function arguments happygavgavWhile I was trying to complete the practice I accidentally wrote "pick_random(item_scenes)" instead of the correct way "item_scenes.pick_random()." Why are arguments sometimes placed in front of the function instead of inside the function brackets? 2 0 Mar. 01, 2024
  • Failed attempt at setting the Timer in scriptnedI tried to set the Timer's autostart to true inside its parent's script, but it didn't work. ```gdscript func _ready() -> void: get_node("Timer").timeout.connect(_onTimeOut) get_node("Timer").autostart = true ``` 4 0 Feb. 26, 2024
  • Why preload and instantiate only gem.tscn in _readyBaffledMcBundleHi! I don't really understand why we only preload and instantiate the gem scene in _ready and not the health_Pack scene as well. Or why we bother doing it at all with the gem scene cause it seems that the code in _on_timer_timeout only cares about what's in the item_scene variable. Actually I even originally failed the practice because I had repeated these steps with the coin scene in the ready function of the practice. It finally worked when I removed this part of my code. Is there something I'm not getting? Thanks in advance for your time and answers :^) 3 0 Feb. 24, 2024
  • Why did I need an additional var in order to get add_child to work?SingleMom420When trying the practice task, the first time I tried it, my code looked like this: ```gdscript func _on_timer_timeout() -> void: var random_scene = collectible_scenes.pick_random() random_scene.instantiate() add_child(random_scene) ``` Doing it this way, the game crashed upon loading. So, copying the tutorial's code, I added a variable so the code looked like this: ```gdscript func _on_timer_timeout() -> void: var random_scene = collectible_scenes.pick_random() var item_instance = random_scene.instantiate() add_child(item_instance) ``` Of course, this worked, but I don't understand why placing the instantiation of the random scene in another variable is essential? Because the var "random_scene" returns a random preloaded scene from the array, right? Why can't I run "add_child" on a preloaded scene without instantiating it through another var first? I looked at the documentation of the "add_child" method and it doesn't seem to mention anything about needing to instantiate anything. 6 0 Feb. 22, 2024
  • About the prop generation position problempast-muleWhen I finished the course, it did generate props randomly, but some of them were not visible on the screen, as if they were generated outside the screen. ```gdscript extends Node2D var item_scenes := [ preload("res://lessons/gem.tscn"), preload("res://lessons/health_pack.tscn") ] func _ready() -> void: get_node("Timer").timeout.connect(_on_timer_timeout) func _on_timer_timeout() -> void: var random_item_scene: PackedScene = item_scenes.pick_random() var item_instance := random_item_scene.instantiate() var viewport_size := get_viewport_rect().size var random_position := Vector2(0, 0) random_position.x = randf_range(0, viewport_size.x) random_position.y = randf_range(0, viewport_size.y) item_instance.position = random_position print(random_position) print(viewport_size) add_child(item_instance) ``` 5 0 Feb. 21, 2024
Site is in BETA!found a bug?