Multiple conditions to prompt different actions in the same function

Godot Version



I’m trying to grasp some of the basics, but this question has really eluded me so far: I want to make a contextual button that does different things at different times. Specifically, clicking it will show a screen and change the button text, clicking the button again will show a different screen and change the button text again, etc.

Think of it like the “next” button for a slideshow- it progresses through the slides one at a time before looping back to slide 1. Right now I have a singular _on_pressed function and a way to emit signals marking the “slide number” of the screen currently being shown, I just can’t figure out how to mix the two. What my end goal is:

func _on_pressed():

if pagenumber==1

signal turn to page number 2

if pagenumber==2

signal turn to page number 3

Sorry if this is a pretty basic question but I truly can not figure this out after reading the documentation and searching the forums.

Quick addition: When I tried this out, I added a print command to see what the pagenumber variable was. On click, it printed two variables (the old page number and the new page number) for some reason

Had to use elif and not if for the conditions after the first, my bad.

For this sort of thing I would use match instead which is GDScripts equivalent of a switch statement:

GDScript reference — Godot Engine (latest) documentation in English

It’s true, a man will seldom answer, but will never miss an opportunity to correct.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.