Impress macro does not update text of box

I am trying to create an interactive presentation to teach spelling. A picture is given, and students click the colored triangles to navigate between choices to complete the word. This slide is named Question1, and the box whose text should change is named Variable1. As the students press the triangles, the text will change between hard-coded strings. In this example, the answer is church and the choices are “th”, “wh”, “ph”, “ch” and “sh”.

When the green triangle is pressed, Question1Plus is called via the Interactions feature. It increments the index by one then calls Question1UpdateDisplay which will update the text. Question1Minus does the opposite while also calling the updater. Both functions are designed to wrap around if the index were to go out of bounds. However when I press F5 and enter presentation mode, nothing happens even when I spam both buttons. What is wrong with my code?

The following code is in question.odp > Standard > Module1. No other code exists anywhere and no other shapes are interactable.

REM *** Initializer ***
Sub Question1Init

	oDoc = ThisComponent
	oSlideList = oDoc.getDrawPages()
	oSlide = oSlideList.getByName("Question1")
	oItem = oSlide.getByName("Variable1")

	REM *** Defines the array ***
	Dim choices()
	choices = Array("th", "wh", "ph", "ch", "sh")

	REM *** Saves the boundary indices as constants ***
	Dim choicesLow, choicesHigh As Integer
	choicesLow = LBound(choices())
	choicesHigh = UBound(choices())

	REM *** Defines the index to be manipulated and initializes it to the first index ***
	Dim choicesIndex As Integer
	choicesIndex = choicesLow

End Sub

REM *** This function will update the displayed text ***
Sub Question1UpdateDisplay

	oItem.getText().setString(choices(choicesIndex))

End Sub

REM *** This function is executed when pressing the up button ***
Sub Question1Plus

	REM *** Increase index ***
	choicesIndex = choicesIndex + 1

	REM *** Wrap around if out of bounds ***
	If choicesIndex > choicesHigh Then choicesIndex = choicesLow

	REM *** Calls the update function ***
	Call Question1UpdateDisplay()

End Sub

REM *** This function is executed when pressing the down button ***
Sub Question1Minus

	REM *** Decrease index ***
	choicesIndex = choicesIndex - 1

	REM *** Wrap around if out of bounds ***
	If choicesIndex < choicesLow Then choicesIndex = choicesHigh

	REM *** Calls the update function ***
	Call Question1UpdateDisplay()

End Sub

Always add
Option Explicit
at begin of the module to be sure all variables are declared.

It is not clear how you transport an information from one subroutine to another. It seems that you assume variables are global?

2 Likes

@Regina When I switch all instances of Dim to Global I get Global not allowed within a procedure error.

REM *** Initializer ***
Option Explicit
Sub Question1Init

	Global oDoc as Object
	Global oSlideList as Object
	Global oSlide as Object
	Global oItem as Object
	
	oDoc = ThisComponent
	oSlideList = oDoc.getDrawPages()
	oSlide = oSlideList.getByName("Question1")
	oItem = oSlide.getByName("VariableStr1")

	REM *** Defines the array ***
	Global choices()
	choices = Array("th", "wh", "ph", "ch", "sh")

	REM *** Saves the boundary indices as constants ***
	Global choicesLow, choicesHigh As Integer
	choicesLow = LBound(choices())
	choicesHigh = UBound(choices())

	REM *** Defines the index to be manipulated and initializes it to the first index ***
	Global choicesIndex As Integer
	choicesIndex = choicesLow

End Sub

REM *** This function will update the displayed text ***
Sub Question1UpdateDisplay
	oItem.getText().setString(choices(choicesIndex))
End Sub

REM *** This function is executed when pressing the up button ***
Sub Question1Plus

	REM *** Increase index ***
	choicesIndex = choicesIndex + 1

	REM *** Wrap around if out of bounds ***
	If choicesIndex > choicesHigh Then choicesIndex = choicesLow

	REM *** Calls the update function ***
	Call Question1UpdateDisplay()

End Sub

REM *** This function is executed when pressing the down button ***
Sub Question1Minus

	REM *** Decrease index ***
	choicesIndex = choicesIndex - 1

	REM *** Wrap around if out of bounds ***
	If choicesIndex < choicesLow Then choicesIndex = choicesHigh

	REM *** Calls the update function ***
	Call Question1UpdateDisplay()

End Sub

When I remove Question1Init and place the variables outside I get Expected: Sub error.

REM *** Initializer ***
Option Explicit
	Global oDoc as Object
	Global oSlideList as Object
	Global oSlide as Object
	Global oItem as Object
	
	oDoc = ThisComponent
	oSlideList = oDoc.getDrawPages()
	oSlide = oSlideList.getByName("Question1")
	oItem = oSlide.getByName("VariableStr1")

	REM *** Defines the array ***
	Global choices()
	choices = Array("th", "wh", "ph", "ch", "sh")

	REM *** Saves the boundary indices as constants ***
	Global choicesLow, choicesHigh As Integer
	choicesLow = LBound(choices())
	choicesHigh = UBound(choices())

	REM *** Defines the index to be manipulated and initializes it to the first index ***
	Global choicesIndex As Integer
	choicesIndex = choicesLow

REM *** This function will update the displayed text ***
Sub Question1UpdateDisplay
	oItem.getText().setString(choices(choicesIndex))
End Sub

REM *** This function is executed when pressing the up button ***
Sub Question1Plus

	REM *** Increase index ***
	choicesIndex = choicesIndex + 1

	REM *** Wrap around if out of bounds ***
	If choicesIndex > choicesHigh Then choicesIndex = choicesLow

	REM *** Calls the update function ***
	Call Question1UpdateDisplay()

End Sub

REM *** This function is executed when pressing the down button ***
Sub Question1Minus

	REM *** Decrease index ***
	choicesIndex = choicesIndex - 1

	REM *** Wrap around if out of bounds ***
	If choicesIndex < choicesLow Then choicesIndex = choicesHigh

	REM *** Calls the update function ***
	Call Question1UpdateDisplay()

End Sub

How can I fix this so that syntax does not break and the variables are accessible only to every function within this Module?

You need to learn about parameters and scope of variables, e.g in LibreOffice help:
https://help.libreoffice.org/24.2/en-US/text/sbasic/shared/01020300.html?DbPAR=BASIC#bm_id3149456

Do you have looked in BASIC tutorial?
https://wiki.documentfoundation.org/Documentation/BASIC_Guide
and a link to external tutorial:
https://www.pitonyak.org/oo.php