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.

  • Multiple vars of the same name in a single scope?SingleMom420I thought you couldn't have multiple vars sharing the same name within a single scope, but it seems here we create multiple "button" vars (one for each iteration of the loop). Why is this allowed here? 1 4 May. 17, 2024
  • Personal challenge: creating a version with a generic create_button functionmatteoscopelAs a personal challenge, I've tried to come up with my own version of this script. It features a generic `create_button` function that can be used for both bodies and expressions. The `_ready` function has a `for` loop that automatically loops over every entry, which I've put inside a dictionary. Do you think this is okay? Would you optimize it more? Did I introduce too many dictionaries while I could have done this in a simpler way? Thank you :) ```gdscript extends Control @onready var texture_nodes := { "body" = %Body, "expression" = %Expression } @onready var rows := { "body" = %RowBodies, "expression" = %RowExpressions } var types := { "sophia" = "body", "pink" = "body", "happy" = "expression", "regular" = "expression", "sad" = "expression" } var textures := { "sophia": preload("res://assets/sophia.png"), "pink": preload("res://assets/pink.png"), "happy": preload("res://assets/emotion_happy.png"), "regular": preload("res://assets/emotion_regular.png"), "sad": preload("res://assets/emotion_sad.png") } func _ready() -> void: for type in types: if types[type] == "body": create_button(type, "body") else: create_button(type, "expression") func create_button(target: String, type: String): var button := Button.new() rows[type].add_child(button) button.text = target.capitalize() button.pressed.connect(func() -> void: texture_nodes[type].texture = textures[target] ) ``` 8 1 Jul. 12, 2024
  • No DRY?davidakUsually in programming, a good practice is Don't Repeat Yourself (DRY). You seem to argue against it at the end. Can you clarify when this recommendation applies and when not? Is it common sense in gamedev or just your personal opinion? Like in this example with the character expressions: When i know from the start i will have 4 or 10 expressions, do you argue to create a prototype with only 3 or to copy-and-paste simple code 10 times instead of the data-driven approach? >Refactor, don't predict I think in such a case it is not a prediction, but a plan (to add 10 expressions). So the advice might not apply there? 3 0 Sep. 17, 2024
  • Preloading assets dynamically?RaspiThis item was super useful. I imagine for the sake of making a character building dynamic, we will need to make the assets to be preloaded dynamically? Is it possible to load assets from a folder or to define somewhere like a csv file or similar paths? So we can add additional assets into the libraries and dictionaries without having to change the code in every new addition? 2 0 Aug. 20, 2024
  • Clarify a phrase about refactoringTJHi! The extra info block about refactoring contains the following phrase: > Depending on the UI you're working on, you may want to keep the elements separate in the scene and in code to have more control over how each behaves. I struggle a bit to understand this one. What is meant in particular with keeping the elements separate? I can't picture it clearly in my mind, I'm missing an example I suppose. Thanks for your help! 2 0 Aug. 13, 2024
  • Closure on closuretesfalconU mentioned closure twice, but there doesn't seem to be any explanation of it. 1) "We'll create closures w lambda functions." 2) "The lambda function at the end is an example of a closure." But what IS a closure? You're giving an example without defining the term. 1 0 Jun. 19, 2024
  • For Loopsfew-apeIn this Lesson, the value of the current_body variable of our for loop appears to be implied since we're looping over a dictionary. I imagine there will be moments where we would want to loop over other things where the for variable is not implied and would need to be defined. Accounting for the amount of enemies on screen or items in inventory, etc.. We would need to define the value of this variable in these instances, correct? Hoping we get to see more examples of loops being used! 1 0 Jun. 17, 2024
  • Destructuring in `for` loop variable?rusty_bitsAs creating the body and expression buttons are quite similar, I wanted to solve the two tasks at once. In python, I would have done something like ``` for dictionary, h_box_container, texture_rect in ( (bodies, row_bodies, body), (expressions, row_expressions, expression) ): ``` but I got an error. Is there a destructuring pattern in GDScript to a similar effect? For the record, I got around the issue by writing an auxiliary function: ``` func create_buttons() -> void: _create_buttons(bodies, row_bodies, body) _create_buttons(expressions, row_expressions, expression) func _create_buttons( dictionary: Dictionary, h_box_container: HBoxContainer, texture_rect: TextureRect ) -> void: for key: String in dictionary: var button := Button.new() button.pressed.connect(func() -> void: texture_rect.texture = dictionary[key]) button.text = key.capitalize() h_box_container.add_child(button) ``` 4 0 Jun. 04, 2024
  • No script at end of the lesson.fledgerI know the expressions script is over and we do not need it. I was not sure if not posting the script was intentional for this lesson, but I figured I would make mention of it incase that was an oversight or may help someone else in the course! 1 0 Jun. 02, 2024
  • How can I just add the Angry emotion button alone in runtime?honored-crocodileI tried to add the [Angry] to the dictionary by right-clicking in-game, then I noticed that the Angry button won't show up since the create_button function only ran once at (func _ready), so I called the create_button again when I right click, but then the old button are duplicates. End up I tried to kill the child nodes and create them all again every time I right clicked, it presented as what I wanted, but I felt hard-coded and unsatisfied, there must be a clever way to do it but just can't figure it out, can you please take a look for me? 3 0 May. 24, 2024
  • Regarding levels on indentation.DargorI get the error "Line 28:Expected end of file." on the following code: ```gdscript func create_button_pink() -> void: var button = Button.new() row_bodies.add_child(button) var key := "Pink" button.text = key.capitalize() button.pressed.connect(func() -> void: body.texture = bodies[key] ) #Line 27 #Line 28 ``` The error goes away if I delete the indentation in the closing parenthesis, making it as the same level as the beginning of the create_button_pink function. I thought closing parenthesis had to be on the same indentation level as the opening statement. Edit: Found this thread on Github: [https://github.com/godotengine/godot/issues/66322](https://github.com/godotengine/godot/issues/66322) Decided to re-download Godot and the error is gone now. It was the same version that I had, so I honestly don't know how or why it happened. 1 0 May. 23, 2024
  • is that lambda functions is good technique in UI inventory just like switching its items yes or no?Celsusing variable to create child node for refactoring thanks for this it solve my biggest problem.. I just want to have question about lambda is that a good way in UI inventory for example just yes or no? 1 0 May. 21, 2024
  • Duplicate Codesteve@playable.designThe second code block has duplicate code, the bodies and expressions dictionaries are repeated. 1 0 May. 15, 2024
Site is in BETA!found a bug?