obsidian/MyObsidianVault/Godot/2D/Tutorial M7 - Telling a story/M7.L8 - Code the expression change/notes.md

37 lines
959 B
Markdown
Raw Normal View History

2024-06-29 12:48:48 +02:00
In this lesson, you will write the code to change the expressions of the characters when clicking the buttons.
Loading image at compile time (advantage of seeing error if file doesnt exist)
```python
var image := preload("res:://image.png")
```
Loading at runtime on demand:
```python
load("res:://image.png")
```
Preloadingloading into a dictionary:
```python
var bodies := {
"sophia": preload("res://assets/sophia.png"),
"pink": preload("res://assets/pink.png")
}
```
Dont forget to multiselect + drag&drop assets into code editor, then multi-cursor correct using
either Alt+Ctrl+Mouseclicks, or Alt+Shift+Cursor Up/Down
*Setting TextureRect texture*
```python
func _ready() -> void:
body.texture = bodies["pink"]
expression.texture = expressions["happy"]
```
It's not mandatory to use square brackets to access dictionary values. You can also use the `.` operator. For example, writing `bodies["sophia"]` is equivalent to `bodies.sophia`.