How to use one Input for multiple functions ?

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

Im trying to make a map/note system, when I press E TextureRect of a map is visible on screen and when I press E again the map is hidden. So far I got it to work but for hiding I need to use a diffrent input.

My code:

#for showing map on screen
if Input.is_action_just_pressed("map") and has_map == true:
	Map_image.visible = true
	map_is_visible = true

#for hiding the map
if Input.is_action_pressed("interact") and Map_image.visible == true and map_is_visible == true:
	Map_image.visible = false
	map_is_visible = false
:bust_in_silhouette: Reply From: jgodfrey

From the minimal code posted, I’m not sure what the map_is_visible variable is gaining you as it’s really a duplicate of the Map_image.visible property. So, I’d suggest eliminating that extra complication (unless you have other code that requires it).

Beyond that, it seems that you need to know whether a map is actually available or not (via the has_map variable).

Without knowing further details, I’d probably try something like this:

if Input.is_action_just_pressed("map_toggle") and has_map:
    Map_image.visible = !Map_image.visible

Essentially, that checks a single map_toggle input, and as long as has_map is true, will simply toggle the visible property to the opposite of what it is currently. So, if it was visible, that will make it invisible (and vice-versa).

Maybe that’s helpful?

Thanks, that worked :slight_smile:

Maciu01 | 2022-07-19 19:22