#Type error

39 messages · Page 1 of 1 (latest)

pastel salmon
#

Hi peeps, godot is complaining that a property is not present on the inferred type "InputEvent" but may be present on a subtype:

#

But I first check if it is said subtype before checking for the button_index

#

so is this just GDScript acting up?

shrewd trellis
#

try casting it first:

if !mouse_event: return
print(mouse_event.button_index)```
pastel salmon
#

so if casting fails it just becomes false?

shrewd trellis
#

if it fails it will be null... nill... what'eva godot uses for that, but it will evaluate to false, yeah 🙂

pastel salmon
#

hm seems like a weird quirk of the language

#

why doesn't it just infer the type from the check

#

because thisll become quite verbal if you have several input types...

shrewd trellis
#

that's C++ I guess, was weird to me too coming from TypeScript, but must have good reason regarding memory management under the hood... am new to godot, so just guessing

pastel salmon
#

yea i also come from TS and new to godot

#

thats why I wanted static typing

#

but ig it be like that

#

this still fails the vibe check

tawdry dock
pastel salmon
#

oh mb

tawdry dock
#

so func _input(event) -> void: instead

pastel salmon
tawdry dock
#

ahh

#

right

pastel salmon
#

but ill use this quirky way instead then

#

until it gets fixed (i assume others have this issue as well)

tawdry dock
#

so it forces you to static type everything?

pastel salmon
#

ye

tawdry dock
#

maybe if you check the second condition within a second nested if loop it'll work without doing the other stuff? no entirely sure though

pastel salmon
#
func _input(event: InputEvent) -> void:
    var mouse_button_event: InputEventMouseButton = event as InputEventMouseButton
    var mouse_motion_event: InputEventMouseMotion = event as InputEventMouseMotion
    
    if(mouse_button_event):
        if mouse_button_event.button_index == MOUSE_BUTTON_LEFT:
            startingCoordinates = mouse_button_event.position
        if mouse_button_event.button_index == MOUSE_BUTTON_WHEEL_UP:
            self.zoom = clamp_cam(self.zoom + Vector2(scaling_factor, scaling_factor))
        if mouse_button_event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
            self.zoom = clamp_cam(self.zoom - Vector2(scaling_factor, scaling_factor))
    
    if (mouse_motion_event && isPressed):
        self.position += -(mouse_motion_event.position - startingCoordinates) * 0.2
#

its now this

#

which is alright ig

#

the if check is now just done by casting first

#

not the way i'd prefer it but so be it

tawdry dock
#

i haven't tested it out but maybe if you do

if (event is InputEventMouseButton):
if (event.button_index == MOUSE_BUTTON_LEFT):
...

It will type it automatically? If that's something you'd prefer over what you're using currently.

pastel salmon
#

already checked and it doesnt work

tawdry dock
#

I just remember doing that once but idk if i'm remembering correctly

#

oh okay nvm then

pastel salmon
#

¯_(ツ)_/¯

shrewd trellis
#

you could also flip it:

    var button_event: InputEventMouseButton = event as InputEventMouseButton
    ...
elif event is InputEventMouseMotion:
    var motion_event: InputEventMouseMotion = event as InputEventMouseMotion
    ...```
or send it to other functions:
```if event is InputEventMouseButton:
    _handle_button_event(event as InputEventMouseButton)
elif event is InputEventMouseMotion:
    _handle_motion_event(event as InputEventMouseMotion)```
pastel salmon
#

yea this is fine for now