#+HTML_HEAD:
#+HTML_HEAD:
#+title: Section 03 - Lesson 07, 09 | Moving/ Removing the Gem
* Links
- [[../../toc.org][TOC - Godot Notes]]
- [[https://www.udemy.com/course/jumpstart-to-2d-game-development-godot-4-for-beginners/learn/lecture/45070467#announcements][S03:L15.07 - Moving the Gem]]
- [[https://www.udemy.com/course/jumpstart-to-2d-game-development-godot-4-for-beginners/learn/lecture/45070471#announcements][S03:L15.09 - Removing the Gem]]
* Notes
** Moving the Gem
we use scripting to extend node functionality
*** get the gem to fall
- we need to get the Gem/Area2D node to fall
- if we had the sprite fall, the collision2D node would stay where it was
**** add a script to the root node
- click on the attach new script button in the scene pane
-
#+ATTR_HTML: :width 200px
[[file:../../../_share/media/img/albert/attach_new_script.png]]
- one script per node is the limit
- you get access to all the children nodes inside the scene.
**** attach node script dialogue
- 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
[[file:../../../_share/media/img/albert/script_icon.png]]
**** detaching script from node
- 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
*** life cycle of a node
**** functions
- _ready()
- _process(delta:float)
**** tree build by stages
1. initialization stage
- creation
- function: _init_()
2. attachment stage
- added to tree
- function: _enter_tree()
3. finalization stage
- node and children ready
- function: _read()
**** how it is built
- 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
**** process functions
- 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
*** adding code to move gem down
- 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
** 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