Last active
February 3, 2021 20:04
-
-
Save Einlander/465a5fa24329b754b3e4cf954e596443 to your computer and use it in GitHub Desktop.
A super useful script for fps games. Attach it to the root node in your scene. Provides an FPS counter, fullscreen toggle, exits the game, and reloads the scene.
This file contains hidden or 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
# Simple Utility Script | |
# Traps Mouse Cursor and hides it. | |
# ESCAPE to quit game | |
# F5 to reload current scene | |
# F9 to toggle collision shape dispay. Scene MUST be reloaded | |
# F10 to toggle fps Display | |
# F11 to switch from windowed to fullscreen | |
# F12 to take screenshot | |
# | |
# Works great for fps games | |
extends Node | |
var fpslabel = Label.new() | |
func _ready(): | |
# Called when the node is added to the scene for the first time. | |
# Initialization here | |
get_viewport().size | |
self.add_child(fpslabel) | |
fpslabel.set_anchors_and_margins_preset(Control.PRESET_TOP_RIGHT) | |
fpslabel.margin_left = -40 | |
var ev = InputEventKey.new() | |
ev.scancode = KEY_ESCAPE | |
InputMap.add_action("debug_quit") | |
InputMap.action_add_event("debug_quit", ev) | |
ev = InputEventKey.new() | |
ev.scancode = KEY_F5 | |
InputMap.add_action("debug_reload_scene") | |
InputMap.action_add_event("debug_reload_scene", ev) | |
ev = InputEventKey.new() | |
ev.scancode = KEY_F9 | |
InputMap.add_action("debug_toggle_collisions") | |
InputMap.action_add_event("debug_toggle_collisions", ev) | |
ev = InputEventKey.new() | |
ev.scancode = KEY_F10 | |
InputMap.add_action("debug_toggle_fps") | |
InputMap.action_add_event("debug_toggle_fps", ev) | |
ev = InputEventKey.new() | |
ev.scancode = KEY_F11 | |
InputMap.add_action("debug_toggle_fullscreen") | |
InputMap.action_add_event("debug_toggle_fullscreen", ev) | |
ev = InputEventKey.new() | |
ev.scancode = KEY_F12 | |
InputMap.add_action("debug_take_screenshot") | |
InputMap.action_add_event("debug_take_screenshot", ev) | |
func _enter_tree(): | |
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) | |
push_warning("WARNING Scene Debug: Be sure to replace this script with proper features") | |
print("WARNING Scene Debug: Be sure to replace this script with proper features") | |
func _exit_tree(): | |
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) | |
func _process(delta): | |
fpslabel.text= "FPS:" + var2str(Engine.get_frames_per_second()) | |
fpslabel.margin_left = -fpslabel.rect_size.x | |
if Input.is_action_just_pressed("debug_toggle_fps"): | |
print("Toggling FPS") | |
fpslabel.visible = !fpslabel.visible | |
if Input.is_action_just_pressed("debug_quit"): | |
print("Stopping Execution") | |
get_tree().quit() | |
if Input.is_action_just_pressed("debug_reload_scene"): | |
print("Reloading scene") | |
print(get_tree().reload_current_scene()) | |
if Input.is_action_just_pressed("debug_toggle_fullscreen"): | |
print("Toggling window mode") | |
OS.window_fullscreen = !OS.window_fullscreen | |
if (Input.is_action_just_pressed("debug_toggle_collisions") == true): | |
var stree = get_tree() | |
stree.debug_collisions_hint = !stree.debug_collisions_hint | |
print("Toggling Visible Collisions") | |
#take screenshot | |
if Input.is_action_just_pressed("debug_take_screenshot"): | |
print("Taking Screenshot") | |
#if Input.is_key_pressed(KEY_F12) == true and screenshotkeydown == false: | |
yield(get_tree(), "idle_frame") | |
yield(get_tree(), "idle_frame") | |
var capture = get_viewport().get_texture().get_data() | |
capture.flip_y() | |
var timestamp = 0 | |
print("Taking Screenshot") | |
#steam style file name | |
timestamp = (OS.get_datetime().year * 10000000000) | |
timestamp += (OS.get_datetime().month * 1000000000) | |
timestamp += (OS.get_datetime().day * 1000000) | |
timestamp += (OS.get_datetime().hour * 10000) | |
timestamp += (OS.get_datetime().minute * 100) | |
timestamp += (OS.get_datetime().second) | |
print("user://"+var2str(timestamp)+".png") | |
capture.save_png("user://"+var2str(timestamp)+".png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment