|
|
|
|
Reply From: |
Wakatta |
This is why the language of C is so beautiful (no type misunderstandings)
‘event’ is not a reserved keyword it is a variable instance of class InputEvent
when called from the virtual functions _input(event : InputEvent):
or _unhandled_input(event : InputEvent):
and _gui_input_event(event : InputEvent):
rather than having to extend that class’s functionality into a variable to use in the _process
func
I’d recommend to use the Input class instead like for example
func _process(delta):
if Input.is_action_just_released("ui_right"):
node.position.x += 100
Thanks a lot, Wakatta.
This is a great working solution. Had been struggling with this for so many days now.
Can you please explain what are virtual functions? How do they behave differently from inbuilt functions like, say, print() in Godot?
ashish | 2020-11-19 11:47
A topic too grand to detail here.
But simply put virtual functions change the functionality when called form a derived member class if you’ve noticed something done automatically when you create a Script.gd
extends Node
class GrandParent:
func _show_bad_habits():
print("horde heirloom")
class Parent extends GrandParent:
func _show_bad_habits():
._show_bad_habits() # call inherited function
print("Expect too much from kids")
class Child extends Parent:
func _show_bad_habits(): # -> completely override
print("Rebel without a cause")
func _ready():
var parent = Parent.new()
var child = Child.new()
parent._show_bad_habits()
child._show_bad_habits()
Wakatta | 2020-11-19 16:03
Thanks Wakatta, for your detailed explanation. Really appreciate.
What does the dot (.) before the call to _show_bad_habits() in class Parent represent? This is in the third line of class Parent.
ashish | 2020-11-21 03:29
Not sure what terms are used in Godot but that dot (.) represents Super
Used to superseded any overridden(Virtual functions) with the one defined in the base class
So because _show_bad_habits()
func was changed in the Parent class if you wanted to still access the original functionality from the GrandParent class that’s where the Super comes into play
! Please note that my explanations here are super simplified and does not express the full scope of use cases
Wakatta | 2020-11-21 04:37
Thanks again.
Simplified or not, this unlocks a lot of things for me.
So, if we don’t insert the dot, will it become a recursive call?
When I removed the dot and ran the app, there was a stackoverflow.
Also, with the dot (.), shouldn’t ‘horde heirloom’ print twice when the app is run?
BTW: I like your placards of Child, Parent and Grandparent.
ashish | 2020-11-21 12:22
BTW: I like your placards of Child, Parent and Grandparent.
Yeah its the best way to show class inheritance.
So, if we don’t insert the dot, will it become a recursive call?
No. What would happen is _show_bad_habits()
would be called only from the GrandParent class regardless of which class is initialized
Also, with the dot (.), shouldn’t ‘horde heirloom’ print twice when the app is run?
No the output should be
parent._show_bad_habits()
# horde heirloom
# Expect too much from kids
child._show_bad_habits()
# Rebel without a cause
But if you added the ._show_bad_habits()
to the child class then yes it would print
# horde heirloom
# Expect too much from kids
# Rebel without a cause
When I removed the dot and ran the app, there was a stackoverflow.
Removed from where? because that should not happen with this simple example
Wakatta | 2021-04-12 21:36