How do I make Godot wait for player input and stop printing motion for every frame of the game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GamersPointeGames

I’m new to Godot and I’m so frustrated because I simply can’t find a concrete answer to this question. I’ve been searching and experimenting for days and this issue will not clear. Furthermore, those answering the question for themselves or others give vague details, information specific to a different style of game, pose solutions from older versions that don’t work on the current version, or pose solutions for a totally different language. I’m attending a pre-made online course, my instructor hasn’t responded to my question, and I’m practicing by building this game as I learn.

I really hope someone can help me with this. Here’s some background. I’m making a brick breaker game; paddle hits ball, ball hits brick, brick breaks, rinse and repeat. The good ol’ fashioned casual game that many love to burn time with. Maybe it’s just my OCD and this doesn’t really matter in the long run, but I’m afraid this might become a bug that causes performance issues or overuses resources. I want a clean game that doesn’t require a lot of resource usage.

I’m sure I’ve already overdone it while trying to fix this one issue because my test works exactly the same way without much of the code I’ve added, at the advice of others, to address this. Nothing has been effective and I only got two errors on top of the issue I already have.

Errors:

Line 11 (UNUSED_VARIABLE):The local variable ‘x_input’ is declared but never used in the block. If this is intended, prefix it with an underscore: ‘_x_input’(ignore) [I don’t want to ignore this because I haven’t decided whether or not I’ll be using this variable later]

Line 15 (RETURN_VALUE_DISCARDED):The function ‘move_and_collide()’ returns a value, but this value is never used. [I don’t know what to do with this value]

Code:

extends KinematicBody2D

const TARGET_FPS = 60
const SPEED = 60

var motion = Vector2.ZERO

onready var sprite = $Sprite # Grants access to the Hammer sprite.

func _process(_delta):
var x_input = Input.get_action_strength(“ui_right”) and Input.get_action_strength(“ui_left”)

motion.y = 0 # Character is unaffected by gravity.

move_and_collide(motion, Vector2.LEFT and Vector2.RIGHT) # Tells the program there has been a Wall collision.

# Movement
if Input.is_action_pressed("ui_right"):
	motion.x = SPEED
	motion.y = 0
	print("Right pressed.")
if Input.is_action_pressed("ui_left"):
	motion.x = -SPEED
	motion.y = 0
	print("Left pressed.")
else:
	motion.x = 0
	motion.y = 0
	print("Player idle.")
	
print(motion)

pass

Needless to say, I get a wall of:
Player idle.
(0, 0)

It’s printing this for every frame when I run it and I’d like it to stop and just wait for player input before printing again. I’ve watched countless videos of other programmers who don’t have this problem, none address it, and copying parts of their code to test hasn’t helped. I’m using version 3.2. Someone please rescue me. I need to know what I’m doing wrong and how to fix it.

There not really a problem here, get rid of print("Player idle.") and put print(motion) under the if statements.

Also move_and_collide should look something like this

move_and_collide(motion, true, true, true)

The last parameter is test_only and returns collision data without moving the object.

Magso | 2020-03-01 21:55

Thanks for responding. I think I’ll have to start over and re-code the entire thing. As soon as I made any change, everything broke. Even when I restarted the software, it just got worse. It’s constantly reading a left input with no input being made now. It’ll read the right input but there’s no movement in that direction. I had to have screwed something up along the way, even if it was just in my project settings when I first started.

GamersPointeGames | 2020-03-02 01:54

Also you had

if right
if left
else

rather than

if right
elif left
else

Maybe that’s why the changes aren’t working as expected.

Magso | 2020-03-02 02:07

Okay, let me try that before I start over then. Maybe doing it the way I did created an anomaly that it couldn’t resolve as I made further changes. I think I was spooked by a warning while I was still working the code, if memory serves.

EDIT: That did it! Now I just need to understand the move_and_collide arguments. Because I’m still learning, I don’t exactly know what I’m answering true or false to yet.

GamersPointeGames | 2020-03-02 02:54

Okay! I made some progress! I have new errors BUT I’m not done with the block. So, I’m okay with these errors. I have an unused argument, and unused variable, and a discarded value error. Because there is nothing there, I understand that I have to put something there to use those things. Cool. With that empty block, I gained more understanding.

Desired changes:

  1. Red errors are all cleared.
  2. Understandable yellow errors pertain only to the new block.
  3. Coordinates are no longer printed, only the phrases that I wish to see: “Player idle,” “Right pressed,” and “Left pressed.”

Clarification:
Item 3 is for logs that are easier to read. I know I should get used to seeing numbers but I’m not yet advanced enough to find comfort in that.

Lingering issue:
I’m still getting a print for every frame. Magso pointed out this may not really be a problem. I understand I could be a little paranoid and a perfectionist, as this is my first solo attempt at making a game. This is my first time doing the programming myself and I need this to be my first success. I must pass this course and release this game, so I’m sweating the details out of fear of failing.

Updated code for the Hammer:
extends KinematicBody2D

const TARGET_FPS = 60
const SPEED = 60
const IDLE = 0

var _state: int = IDLE

var motion = Vector2.ZERO
var states_strings := {
IDLE: “idle”
}

onready var sprite = $Sprite # Grants access to the Hammer sprite.

func _process(_delta):

motion.y = 0 # Character is unaffected by gravity.

move_and_collide(motion, Vector2.LEFT and Vector2.RIGHT) # Tells the program there has been a Wall collision.

# Movement
if Input.is_action_pressed("ui_right"):
	motion.x = SPEED
	motion.y = 0
	print("Right pressed.")
elif Input.is_action_pressed("ui_left"): # Magso fixed my movement issue.
	motion.x = -SPEED
	motion.y = 0
	print("Left pressed.")
else:
	motion.x = 0
	motion.y = 0
	print("Player idle.")

func _integrated_forces(state:KinematicBody2D) → void:
var x_input = Input.get_action_strength(“ui_right”) and Input.get_action_strength(“ui_left”)

print(motion) """I wasn't getting any output printed without this.  Magso made it clear I need to move it and I will soon."""

pass

Disclaimer:
I’m sure I have some stuff wrong here, I just needed to clear errors and get everything to behave as intended so that I can understand what to do next.

GamersPointeGames | 2020-03-02 04:33

I didn’t understand this before: “There not really a problem here, get rid of print(“Player idle.”) and put print(motion) under the if statements.” But now I get what you were saying. It was printing like that because I literally told it to without realizing that’s what I did. Looking at another thread you answered on is what made it truly click. The other error I was getting stopped me from getting the desired results.

I later ran into a new red error with the change_state not being declared for the class. This was the thread I’m referencing: trigger an event on landing

In their question, they had a block that helped to resolve that issue. Once all of the actual bugs were gone, I asked myself what if I told it to print nothing when idle, like you mentioned. Since nothing else was causing an issue, it worked as intended. I wish there was an option to upvote your answer. You hit the nail right on the head. THANK YOU!

P.S. - I don’t know why it’s not giving me an option to upvote your response. Anyway, you entirely solved my problem. MUCH appreciated!

GamersPointeGames | 2020-03-02 06:40