0 votes

Hi all.

Figured I'd attempt to get one of my working games prepped for mobile, but I'm a little confused as to how I should get this done.

I want to use InputEventScreenTouch. There are three main divisions for my screen (two for rotating the turret, one for shooting), and I've been attempting to figure out how to make these areas without adding extra nodes.

Below is what I'm trying to implement into my get_input() function.

if InputEventScreenTouch.is_pressed() == true:
    var TouchPoint = InputEventScreenTouch.get_position()
    if TouchPoint is in LeftTurnArea:
        rotation_Direction -= 1.25
    if TouchPoint is in RightTurnArea:
        rotation_Direction += 1.25
    if TouchPoint is in ShootArea:

In its current state, the error detector's complaining about improper use of in. What's a better way of doing this?

And the areas;

onready var LeftTurnArea #Upper left quarter of the screen.
onready var RightTurnArea #Lower left quarter of the screen.
onready var ShootArea #Right half of the screen
in Engine by (197 points)

1 Answer

+1 vote
Best answer

That's because you are using in incorrectly. ;) Here's a correct example:

var a = 2
if a in [1, 2, 3]:
    print("a is in the list")
    print("a is not in the list")

So by now it should've become obvious you're also using is incorrectly:

extends Node2D

func _ready():
    print(self is Node2D)     # true
    print(self is Control)    # false

That said, here's how I would do it:

onready var screen_width = get_viewport_rect().size.x
onready var screen_height = get_viewport_rect().size.x

onready var LeftTurnArea = Rect2(0, 0, screen_width/2, screen_height/2)
onready var RightTurnArea = Rect2(0, screen_height/2, screen_width/2, screen_height)
onready var ShootArea = Rect2(screen_width/2, 0, screen_width, screen_height)

func get_input():
    if InputEventScreenTouch.is_pressed() == true:
        var TouchPoint = InputEventScreenTouch.get_position()

        if LeftTurnArea.has_point(TouchPoint):
            rotation_Direction -= 1.25
        elif RightTurnArea.has_point(TouchPoint):
            rotation_Direction += 1.25
        elif ShootArea.has_point(TouchPoint):
by (10,249 points)
selected by

Yeah, I figured I wasn't using is and in the right way, but I wasn't sure how to do it right. Sometimes I forget that English and GDScript are two completely different languages.

Thank you very much.

Huh… getting an error.

Non-static function "is_pressed" can only be called from an instance.

That's part of the code you provided in your question, so assumed it'd would work already. See here for a solution.

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.