Godot Game Engine

11/19/20

Categories: Development Tags: Game Development Godot Android Development

~/contents

This is a repository for little bits and pieces.

List Comprehensions

Something like Python’s list comprehensions really comes in handy when doing Array comparisons. Godot does not, and probably will not, ever have this, but I found Groups were a handy way to organize and find things.

func get_group_members_in_array(array,group_name):
    var children = []
    for item in array:
        if item.is_in_group(str(group_name)):
            children.append(item)
    return children

Screenshots

I’m trying to get better about taking pictures of my apps as they progress. Enumeratuions

func _input(event):
    if event is InputEventKey and event.pressed:
        if event.scancode == KEY_PRINT:
            var datetime = OS.get_datetime()
            var readable_date = str(datetime["month"]) + str(datetime["day"]) + str(datetime["year"]) + " - " + str(datetime["hour"]) + str(datetime["minute"]) + str(datetime["second"])
            var image = get_viewport().get_texture().get_data()
            image.flip_y()
            image.save_png("res://Screenshots/screenshot"+readable_date+".png")
            print("Screenshot taken: " + str(datetime))

Saving and Loading Game Data

var game_data = "user://gamename.save"

func load_game():
  var save_game = File.new()
  if not save_game.file_exists(game_data):
    print("No game save data.")
    table = { "scores": [] }
    return # Error! We don't have a save to load.
  print("Loading game data.")
  save_game.open(game_data, File.READ)
  table = save_game.get_var()
  save_game.close()

func save_game():
  var save_game = File.new()
  save_game.open(game_data, File.WRITE)
  save_game.store_var(Global.table)
  print("Saved game data.")
  save_game.close()

This data is saved under
C:\Users\yourname\AppData\Roaming\Godot\app_userdata\yourgame

https://godotengine.org/qa/2080/save-record