Class Objects & Constructors | Godot GDScript Tutorial | Ep 16 | Godot Tutorials 5/8/23 4:43 PM
Courses / Introduction-To-Gdscript / Godot-Tutorials-Gdscript-16
Class Objects & Constructors | Godot GDScript
Tutorial | Ep 16
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16
PREV EPISODE NEXT EPISODE
Learning Materials
Article Resource
https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-16 Page 1 of 4
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16 | Godot Tutorials 5/8/23 4:43 PM
Class Objects
A class object is an instance of a class. A class object can also be referred to as a class instance, instanced object, or class object.
#Player.gd
extends Node2D
class_name Player
# class variables and functions
#Scene.gd
extends Node2D
var playerInstanceObject = Player.new()
Class Constructors
The class constructor is a particular function in which it is called every time a class object is created.
You define a class constructor using the _init() method:
#Player.gd
extends Node2D
class_name Player
var playerHealth: int
# Class Constructor
_init():
playerHealth = 100
#Scene.gd
extends Node2D
var playerInstanceObject = Player.new()
print(playerInstanceObject.playerHealth) # 100
You are also able to pass arguments into the constructor as well:
https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-16 Page 2 of 4
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16 | Godot Tutorials 5/8/23 4:43 PM
#Player.gd
extends Node2D
class_name Player
var playerHealth: int
# Class Constructor
_init(startingHealth:int = 100):
playerHealth = startingHealth
#Scene.gd
extends Node2D
var playerInstanceObject = Player.new(200)
print(playerInstanceObject.playerHealth) # 200
Creative Common License
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16 video & article by Godot Tutorials is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License .
Spread the love, share this if it helped you!
SHARE ON TWITTER SHARE ON FACEBOOK SHARE ON REDDIT SHARE WITH EMAIL
Subscribe to my Newsletter
Join our newsletter and get news in your inbox! We hate spam too, you
won't get any from me :)
Email... SUBSCRIBE
https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-16 Page 3 of 4
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16 | Godot Tutorials 5/8/23 4:43 PM
GODOT TUTORIALS
WEBSITE RESOURCES LEGAL OTHER
Courses Godot Privacy Policy Contact Me
Trello Cookie Policy Website Info
Github Terms & About Me
Conditions
Disclaimer
©2021 Godot Tutorials | Website Powered by Hugo Framework
https://godottutorials.com/courses/introduction-to-gdscript/godot-tutorials-gdscript-16 Page 4 of 4