Help with InputEventScreenTouch?

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

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:
    	fire()

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
:bust_in_silhouette: Reply From: njamster

That’s because you are using in incorrectly. :wink: Here’s a correct example:

var a = 2
if a in [1, 2, 3]:
    print("a is in the list")
else:
    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):
            fire()

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.

System_Error | 2020-02-14 20:08

Huh… getting an error.

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

System_Error | 2020-02-16 17:46

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

njamster | 2020-02-16 19:28