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?24May. 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]
)
```102Jul. 12, 2024
closuredatiswousI've not understood what a closure is from this lesson.30Nov. 26, 2024
Understanding Check: ClosureMrBright01```gdscript
func create_exp_buttons () -> void:
for current_exp: String in expressions:
var button = Button.new()
row_expressions.add_child(button)
var key_example = current_exp
button.text = key_example.capitalize()
button.pressed.connect(
func () -> void:
expression.texture = expressions[key_example]
```
When the lambda function to set the "expression.texture" is finished, the variable "key_example" vanishes. The lambda function has closure because the variable still exists, in the form of "expression.texture"?
Is my understanding correct?30Oct. 28, 2024
Delete nose on the angry expressionCocabody already has nose, and they don't match, so I had to open it on gimp and delete the nose on the angry.png expression10Oct. 16, 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?40Sep. 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?20Aug. 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!20Aug. 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.10Jun. 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!10Jun. 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)
```40Jun. 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!10Jun. 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?
30May. 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.10May. 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?10May. 21, 2024
Duplicate Codesteve@playable.designThe second code block has duplicate code, the bodies and expressions dictionaries are repeated.10May. 15, 2024
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.