Godot 2.1 Useful notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GODOT | |
#1. loading resources | |
var res = load("res://robi.png") # resource is loaded when line is executed | |
get_node("sprite").set_texture(res) | |
func _ready(): | |
var res = preload("res://robi.png") # resource is loaded at compile time | |
get_node("sprite").set_texture(res) | |
#use preload when path is constant. it’s more optimal | |
#use load when path is dynamic | |
const Corner = preload("corner.gd") #load class to use it | |
func _on_shoot(): | |
var bullet = preload("res://bullet.scn").instance() | |
add_child(bullet) | |
#spawn object to stage. load scene and call instance() method | |
#2. paths | |
path user:// that is always writable. | |
path res:// point to project resources | |
#3. root viewport | |
get_tree().get_root() # access via scenemainloop #Viewport class | |
get_node("/root") # access via absolute path #Viewport class | |
#3a. root scene | |
get_tree() #top of tree, #SceneTree class | |
#4. change scene | |
func _my_level_was_completed(): | |
get_tree().change_scene("res://levels/level2.scn") | |
#get_tree().change_scene_to(scene_instance) | |
#5. curr scene | |
get_tree().get_current_scene() | |
#6. collisions | |
convex - выпуклый (fast) | |
concave - вогнутый | |
#7. positions | |
Spatial -> Transform -> Local ~ get_global_transform().basis (3 X Vector3) + 1XVector3 is origin | |
Transform -> Translation ~ get_global_transform().origin (converted) | |
#8. area collision listeners | |
func _on_Area2D_body_enter( body ): | |
if body.is_in_group("enemies"): | |
queue_free() | |
func _on_coin_body_enter(body): | |
if (not taken and body extends preload("res://player.gd")): | |
taken = true | |
func _on_Area2D_body_enter( body ): | |
if body.get_name() == "player": | |
queue_free() | |
get_node("/root/world").collect_diamond() | |
#9. extend script from base script | |
extends "res://enemies/enemy.gd" #in the beginning of file | |
#10. enable cantacts in rigid body | |
extends RigidBody2D | |
func _ready(): | |
set_contact_monitor( true ) | |
set_max_contacts_reported( 5 ) | |
connect("body_enter",self,"collion_now") | |
#or add area, and collision shape to area, handle body_enter event on area then | |
#11. animations | |
one shot - plays one time | |
mix node - play 2 animation in same time | |
blend node - smoothly goes from one animation to another | |
time scale - slowdown/insrease speed of animation | |
transition - switch between animations | |
time seek - play animation from new position (useful when need to play anim from first frame) | |
# anim_tree.timeseek_node_seek("jump_seek", 0) #params in code only | |
#12. add class definition | |
const Pool = preload("res://com/brandonlamb/pool/pool.gd") | |
var pool = Pool.new(BULLET_POOL_SIZE, BULLET_POOL_PREFIX, GreenBullet) | |
#in pool.gd | |
#define constructor | |
func _init(size_, prefix_, scene_): | |
#13. _ready fix | |
func _notification(what): | |
if (what == NOTIFICATION_INSTANCED): | |
#all internal initialization, you can use get_node here to get internal nodes and object will be to some degree usable even if not added yet to scene tree | |
#it's called after object construction, at this point object is not even inside scene tree | |
#also it's called only once | |
#add_hero() | |
pass | |
elif(what == NOTIFICATION_READY): | |
#only parts that are dependent on outside world (on theparents etc/also called when reparented) | |
#usually it's quite ok to update external references if object is reparented so it usually works ok without any further thinking | |
#load_track(track_file) | |
#add_first_bars() | |
pass | |
#14. randi() | |
randi()%11+1 returns an int from 1 to 10 | |
randf()*11+1 returns a float from 1 to 10 | |
rand_range(1,11) returns a float from 1 to 10 | |
randi()%2 = { 0, 1 } range | |
#15. cos ( a in radians ) !!! | |
#16 number functions | |
print(floor(2.3)) #2 | |
print(floor(2.9)) #2 | |
print(ceil(2.3)) #3 | |
print(ceil(2.9)) #3 | |
print(round(2.3)) #2 | |
print(round(2.9)) #3 | |
#16. tmp folder /Users/boom/.godot/tmp/ | |
#17. dir | |
var dir = Directory.new() | |
if !dir.dir_exists("user://Saves"): | |
dir.open("user://") | |
dir.make_dir("user://Saves") | |
#18. dynamic texture | |
var tex = ImageTexture.new() | |
var img = Image() | |
img.load("image.png") | |
tex.create_from_image(img) | |
#19. integrate array | |
for pos in postions: | |
#use it | |
if not pos in positions: | |
positions.append(pos) | |
#20. compile engine | |
cd godot-2.1 | |
scons platform=osx bits=64 tools=yes | |
cp -r misc/dist/osx_tools.app ./Godot.app | |
mkdir -p Godot.app/Contents/MacOS | |
cp bin/godot.osx.tools.64 Godot.app/Contents/MacOS/Godot | |
chmod +x Godot.app/Contents/MacOS/Godot | |
#21 | |
show file in system file browser | |
#var dir = Globals.globalize_path(stream.get_path()) | |
#dir = dir.substr(0, dir.find_last('/')) | |
#OS.shell_open("file://" + dir) | |
#22 | |
#print from back | |
for i in range(notes.size()-1, -1, -1): | |
print("index:", i, "value:", notes[i]) | |
#23 | |
#bubble sort | |
var notes = [7,2,5,1] | |
print("notes:", notes) | |
for i in range(notes.size()-1, -1, -1): | |
print("i:", i) | |
for j in range(1,i+1,1): | |
print("J:", j) | |
if notes[j-1] > notes[j]: | |
var temp = notes[j-1] | |
notes[j-1] = notes[j] | |
notes[j] = temp | |
print("sorted notes:", notes) | |
#24 | |
keyboard and touch display | |
if (ie.type == InputEvent.KEY): | |
if (Input.is_action_just_pressed("ui_left")): | |
vehiclePos[0] = !vehiclePos[0]; | |
if (Input.is_action_just_pressed("ui_right")): | |
vehiclePos[1] = !vehiclePos[1]; | |
if (ie.type == InputEvent.SCREEN_TOUCH && ie.pressed): | |
if (ie.x < viewportSize.width/2): | |
vehiclePos[0] = !vehiclePos[0]; | |
if (ie.x > viewportSize.width/2): | |
vehiclePos[1] = !vehiclePos[1]; | |
#25 | |
String p_path | |
p_path.c_str() | |
#26. scons with verbose info | |
scons p=iphone tools=no bits=64 target=debug arch=arm64 verbose=yes | |
#27. scons multi process | |
scons platform=x11 tools=no target=debug bits=64 -j 7 | |
#28 scons platform=x11 dev=yes | |
#29 maybe linux bugfix | |
What Godot version are you using? If compiling yourself, use `use_static_cpp=yes` when using such a recent GCC version to stay compatible with older system |
Comments
Post a Comment