Code doesn't work

func _slowtime():
if Input.is_action_just_pressed(“Slow”):
print(“test”)
Engine.time_scale = 0.5
print(“test2”)

i want it to slow the game time down when pressing the “e” key but it doesn’t work nor does it print either test (i tripled checked i set it to the right key and even tried changing the key)

Cool, why doesn’t it work? What did you expect to happen versus what really happens?

1 Like

Yeah… So you made a function that isn’t called anywhere and…? We don’t know how you’re using this. Are you calling this in _process() or something?

Put that code in _unhandled_key_input() and it’ll probably work.

2 Likes

You seem to be lacking an understanding of how functions work. Other than built-in functions like _process or _input, Functions need to be called explicitly (by you) from somewhere to do anything.
The line if Input.is_action_just_pressed("Slow"): is never even checked because the engine doesn’t know it’s supposed to be checked. You need to move it into _process, _input or _unhandled_input and then call your function _slowtime() from there:

func _process(delta):
	if Input.is_action_just_pressed("Slow"):
		slowtime()

func slowtime():
	print("slowtime")
	Engine.time_scale = 0.2

By the way, names with an underscore at the start shouldn’t be used for your own functions because the name signals that the function was declared in a native script and is supposed to be overriden, that’s why I changed the name from _slowtime to slowtime

Underscores are also the recommended naming convention for private functions and variables. From Godot’s style guide for GDScript:

Prepend a single underscore (_) to virtual methods functions the user must override, private functions, and private variables:

2 Likes