2024-11-14 19:55:31 +02:00
|
|
|
#+title: Section 03 - Lesson 07, 08 | Moving/ Removing the Gem
|
2024-11-29 06:41:22 +02:00
|
|
|
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../../_share/media/css/godot.css" />
|
2024-11-12 03:53:56 +02:00
|
|
|
|
|
|
|
|
* Links
|
|
|
|
|
- [[../../toc.org][TOC - Godot Notes]]
|
2024-11-14 03:16:25 +02:00
|
|
|
- [[https://www.udemy.com/course/jumpstart-to-2d-game-development-godot-4-for-beginners/learn/lecture/45070467#announcements][S03:L15.07 - Moving the Gem]]
|
2024-11-14 19:55:31 +02:00
|
|
|
- [[https://www.udemy.com/course/jumpstart-to-2d-game-development-godot-4-for-beginners/learn/lecture/45070471#announcements][S03:L15.08 - Removing the Gem]]
|
2024-11-12 03:53:56 +02:00
|
|
|
|
|
|
|
|
* Notes
|
2024-11-13 02:58:06 +02:00
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
** Moving the Gem
|
|
|
|
|
|
2024-11-13 02:58:06 +02:00
|
|
|
we use scripting to extend node functionality
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
*** get the gem to fall
|
2024-11-13 02:58:06 +02:00
|
|
|
- we need to get the Gem/Area2D node to fall
|
|
|
|
|
- if we had the sprite fall, the collision2D node would stay where it was
|
|
|
|
|
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
**** add a script to the root node
|
2024-11-13 02:58:06 +02:00
|
|
|
- click on the attach new script button in the scene pane
|
|
|
|
|
-
|
|
|
|
|
#+ATTR_HTML: :width 200px
|
2024-11-29 07:13:38 +02:00
|
|
|
[[file:../../_share/media/img/albert/attach_new_script.png]]
|
2024-11-13 02:58:06 +02:00
|
|
|
- one script per node is the limit
|
|
|
|
|
- you get access to all the children nodes inside the scene.
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
**** attach node script dialogue
|
2024-11-13 02:58:06 +02:00
|
|
|
- Template is generally Node: Default
|
|
|
|
|
- some object types provide other templates with different boiler plate code
|
|
|
|
|
|
|
|
|
|
- the node we attached a script to will have a script icon next to the eye icon
|
|
|
|
|
-
|
|
|
|
|
#+ATTR_HTML: :width 200px
|
2024-11-29 07:13:38 +02:00
|
|
|
[[file:../../_share/media/img/albert/script_icon.png]]
|
2024-11-13 02:58:06 +02:00
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
**** detaching script from node
|
2024-11-13 02:58:06 +02:00
|
|
|
- sometimes you don't want a certain script attached to a node
|
|
|
|
|
- for testing purposes to prevent a script from doing something
|
|
|
|
|
- you want a different script, etc.
|
|
|
|
|
|
|
|
|
|
- if a script is attached, then when you select the node the "script attach button" will change from a green plus to a red x
|
|
|
|
|
- press the button and detach it
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
*** life cycle of a node
|
|
|
|
|
**** functions
|
2024-11-13 02:58:06 +02:00
|
|
|
- _ready()
|
|
|
|
|
- _process(delta:float)
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
**** tree build by stages
|
2024-11-13 02:58:06 +02:00
|
|
|
1. initialization stage
|
|
|
|
|
- creation
|
|
|
|
|
- function: _init_()
|
|
|
|
|
|
|
|
|
|
2. attachment stage
|
|
|
|
|
- added to tree
|
|
|
|
|
- function: _enter_tree()
|
|
|
|
|
|
|
|
|
|
3. finalization stage
|
|
|
|
|
- node and children ready
|
|
|
|
|
- function: _read()
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
**** how it is built
|
2024-11-13 02:58:06 +02:00
|
|
|
- every node is given an id before it gets a name
|
|
|
|
|
|
|
|
|
|
- all init functions will be called before any other stage for any other node is invoked
|
|
|
|
|
|
|
|
|
|
- similarly the attachment stage, of adding nodes to other nodes takes place before any other function in any other node is activated
|
|
|
|
|
- at this point we get a name for the node
|
|
|
|
|
|
|
|
|
|
- ready tells us that the node is up and ready to go, and so are its children
|
|
|
|
|
- therefore the children of the node will be set up before the parent
|
|
|
|
|
- ready will be called for the children first
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
**** process functions
|
2024-11-13 02:58:06 +02:00
|
|
|
- invoked on all nodes
|
|
|
|
|
- every frame
|
|
|
|
|
|
|
|
|
|
- used to update the node each frame
|
|
|
|
|
|
|
|
|
|
- delta refers to the time that elapsed since previous call on previous frame
|
|
|
|
|
- use delta to normalize things
|
|
|
|
|
|
|
|
|
|
**** _process(delta)
|
|
|
|
|
- called in main loop
|
|
|
|
|
- as fast as possible
|
|
|
|
|
|
|
|
|
|
**** _physics_process(delta)
|
|
|
|
|
- called in main loop
|
|
|
|
|
- regular interval
|
|
|
|
|
|
2024-11-14 03:16:25 +02:00
|
|
|
*** adding code to move gem down
|
2024-11-13 02:58:06 +02:00
|
|
|
- increase the y position
|
|
|
|
|
- all node parameters can be found in inspector (including inherited)
|
|
|
|
|
- position.y = position.y + speed * delta
|
|
|
|
|
- speed can be anything
|
|
|
|
|
- we multiply by delta to normalize it compared to the time of the previous frame
|
2024-11-14 03:16:25 +02:00
|
|
|
|
|
|
|
|
** Removing the Gem
|
|
|
|
|
*** basic idea
|
|
|
|
|
- use the viewport rect
|
|
|
|
|
- find the height of the rect
|
|
|
|
|
- when the y position of the gem is outside the bounds remove it
|
|
|
|
|
|
|
|
|
|
*** implementation
|
|
|
|
|
- use get_viewport_rect to get the w,h
|
|
|
|
|
- check if position.y > rect.size.y
|
|
|
|
|
- use queue_free function to remove this from the queue
|
|
|
|
|
- set process to stop being called via set_process
|