Invalid call. Nonexistent function 'is_pressed' in base 'GDScriptNativeClass'.

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

I am trying to implement touchscreen response in my game…
And for that I have written the following code –

# TOUCHSCREEN MOVEMENT


if InputEventScreenTouch.is_pressed():
	
	if InputEventScreenTouch.get_index() == 1 :
		
		if InputEventScreenTouch.get_position() < touch_division :
			input_direction = -1
			
		elif InputEventScreenTouch.get_position() > touch_division :
			input_direction = 1
		
	elif InputEventScreenTouch.get_index() == 2 :
		input_direction = 'up'	
	
	else :
		input_direction = 0

But it giving the error…
The script is attached to a KinematicBody2D…

The full script is –

extends KinematicBody2D


var input_direction = 0
var direction = 1
 
var speed = Vector2()
var velocity = Vector2()
 
const MAX_SPEED = 600
const ACCELERATION = 1200
const DECELERATION = 2000
 
const JUMP_FORCE = 700
const GRAVITY = 2000
 
 
func _ready():
	set_process(true)


func _process(delta):
	
	var touch_division = get_viewport_rect().size.x	/ 2
	
	if input_direction == direction :
		input_direction = direction


# TOUCHSCREEN MOVEMENT


	if InputEventScreenTouch.is_pressed():
		
		if InputEventScreenTouch.get_index() == 1 :
			
			if InputEventScreenTouch.get_position() < touch_division :
				input_direction = -1
				
			elif InputEventScreenTouch.get_position() > touch_division :
				input_direction = 1
			
		elif InputEventScreenTouch.get_index() == 2 :
			input_direction = 'up'	
		
		else :
			input_direction = 0
	
	

# KEYBOARD MOVEMENT

	if Input.is_action_pressed("move_left"):
        input_direction = -1
		
	elif Input.is_action_pressed("move_right"):
		input_direction = 1
	
	else:
		input_direction = 0

	
		
	if input_direction == - direction:
		speed.x /= 3
		
	if input_direction:
		speed.x += ACCELERATION * delta
		
	else:
        speed.x -= DECELERATION * delta
	
	speed.x = clamp(speed.x, 0, MAX_SPEED)
	
	speed.y += GRAVITY * delta	
	 
	
	velocity = Vector2(speed.x * delta * direction, speed.y * delta)
	var movement_remainder = move(velocity)
	
	if is_colliding():
		var normal = get_collision_normal()
		var final_movement = normal.slide(movement_remainder)
		speed = normal.slide(speed)
		move(final_movement)	
		

Looking Forward to Solution…

:bust_in_silhouette: Reply From: eons

InputEventScreenTouch is a class, these methods are for instances, the error is trying to say that you are trying to execute methods on a class.

What you need to do is use _input(event), check if the event is of that class and use it to get values to later use in _physics_process.

Something like:

func _input(event):
    if event is InputEventScreenTouch:
      if event.is_pressed():
        if event.get_index() == 1 :
            if event.get_position() < touch_division :
                input_direction = -1
            elif event.get_position() > touch_division :
                input_direction = 1
        elif event.get_index() == 2 :
            input_direction = 'up'  
      elif event.is_released():    
         input_direction = 0 

It is still not working .
Here is the full script for your reference –

extends KinematicBody2D
    
    
    var input_direction = 0
    var direction = 1
     
    var speed = Vector2()
    var velocity = Vector2()
     
    const MAX_SPEED = 600
    const ACCELERATION = 1200
    const DECELERATION = 2000
     
    const JUMP_FORCE = 700
    const GRAVITY = 2000
     
     
    func _ready():
    	set_process(true)
    
    
    func _process(delta):
    	
    	var touch_division = get_viewport_rect().size.x	/ 2
    	
    	if input_direction == direction :
    		input_direction = direction
    
    
    # TOUCHSCREEN MOVEMENT
    
    
    	if InputEventScreenTouch.is_pressed():
    		
    		if InputEventScreenTouch.get_index() == 1 :
    			
    			if InputEventScreenTouch.get_position() < touch_division :
    				input_direction = -1
    				
    			elif InputEventScreenTouch.get_position() > touch_division :
    				input_direction = 1
    			
    		elif InputEventScreenTouch.get_index() == 2 :
    			input_direction = 'up'	
    		
    		else :
    			input_direction = 0
    	
    	
    
    # KEYBOARD MOVEMENT
    
    	if Input.is_action_pressed("move_left"):
            input_direction = -1
    		
    	elif Input.is_action_pressed("move_right"):
    		input_direction = 1
    	
    	else:
    		input_direction = 0
    
    	
    		
    	if input_direction == - direction:
    		speed.x /= 3
    		
    	if input_direction:
    		speed.x += ACCELERATION * delta
    		
    	else:
            speed.x -= DECELERATION * delta
    	
    	speed.x = clamp(speed.x, 0, MAX_SPEED)
    	
    	speed.y += GRAVITY * delta	
    	 
    	
    	velocity = Vector2(speed.x * delta * direction, speed.y * delta)
    	var movement_remainder = move(velocity)
    	
    	if is_colliding():
    		var normal = get_collision_normal()
    		var final_movement = normal.slide(movement_remainder)
    		speed = normal.slide(speed)
    		move(final_movement)	
		

you can skip this and post a code snippet to implement touchscreen response…
Looking Forward to Solution…

Arindam | 2018-06-08 04:39

Read again my answer, InputEventScreenTouch is a class, not the proper touch event, you cannot use it like that.

eons | 2018-06-09 04:50