0 votes

Hi all,

I'm having a go at pong and want it to work on mouse, keyboard and touchscreen. So, I'm starting there, on the bats/paddles.

Touch input

Considering the left bat only at the moment. The idea is I have a space to the left of the bat where I can touch with my fat fingers. The bat then moves at X speed to that location. If I slide my finger down then the bat tries to continue to wherever I'm moving my finger to at X speed. This way I can use hand input.

Mouse Input

Basically the same idea. You click the mouse into the area to the left of the bat, it acts just like the touch system.

Keyboard input

Basic A/Z keys. Bat moves at X speed in appropriate direction. Not much to see here.

The Question

The question is, how would you implement the touch / move to? I'm considering something like an Area2d. Finding the Y location of the touch and then tweening the bat to that location.

I want this code to be as modular as possible. The last project got a bit spaghetti code. Hence I'm asking before just diving in.

Any advice greatly appreciated.

in Engine by (823 points)

1 Answer

0 votes

I went doing a bit of testing, and I would structure the scene this way:

- LeftBatArea (Area2D)
    - CollisionShape (rectangle)
- LeftBat (KinematicBody2D + bat.gd)
    - Sprite
    - KeyboardInput (Node + bat_keyboard.gd)
    - MouseInput (Node + bat_mouse.gd)
    - TouchInput (Node + bat_touch.gd)

The bat.gd would only expose a command variable that contains -1, 1 or 0 and moves according to that parameter.

Then, every input node would reference the parent, and set the command for each possible input.
Mouse and touch might want to handle the input_event signal from LeftBatArea so they know when an event happened over the control area of the bat.

One thing to note, you should store the touch index so you know which finger controls the bat, in case your game can be played with multiple users on a touchscreen (doing that with the mouse is impossible because there can be only one mouse).

Another thing is, Godot emulates mouse event on touch devices, so you should test if the mouse input node enters in conflict with the touch input node or not.

by (27,603 points)

Thanks for your help, I'm back from a break and getting back into it.

The bat.gd would only expose a command variable that contains -1, 1 or
0 and moves according to that parameter.

May I ask how the bat.gd would expose this variable? I'm really not the worlds best programmer as you can see. Learning all the time. :-)

Then, every input node would reference the parent, and set the command
for each possible input.

Would that be with a get_parent.variable = stuffhere type command?

RE the rest I'll chip away but first things first. :-) Thanks again!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.