This requires Diggsey's Function Arguments Library ( which ships with
TopGUI, but is also available
here without having to use TopGUI) and Matrix1.
As an example with, toggling works by calling
ToggleInt( NameOfVariable, OnValue )
ToggleFloat( NameOfVariable, OnValue )
ToggleByte( NameOfVariable, OnValue )
Once called, the variable will be given the value of OnValue if it was zero; otherwise if it is already on, it will become zero. So, with toggle actions, this could be used with the keyboard controls to turn auto-run on in an RPG or auto-pilot mode in a space flight game.
A = 1
ToggleInt(A,1)
Print A
-----------------------------
Outputs: 0
ToggleInt(A,1)
Print A
-----------------------------
Run again and it will outputs: 1
Red = Rgb( 255, 0, 0 )
Colour = 0
ToggleInt(Colour, Red)
Cls Colour
ToggleInt(Colour, Red)
Ink Colour, Colour
Text 0, 0, "Text"
-----------------------------
Results with a red screen with black text.
Colour's final value is 0
Equivalent of:
[b]Red = Rgb( 255, 0, 0 )
Colour = 0
If Colour = 0
Colour = Red
Else
Colour = 0
EndIf
Cls Colour
If Colour = 0
Colour = Red
Else
Colour = 0
EndIf
Ink Colour, Colour
Text 0, 0, "Text"
There is a significant difference between this and what would normally be used; here, we do not return the result of the function to the variable; it will change the value of the variable; which must be a regular local or global variable. In short, no nead for the equals sign with an assignment value, no need to write the name of the variable more than once.
CycleInt( NameOfVariable, StartValue, EndValue, Step )
CycleFloat( NameOfVariable, StartValue, EndValue, Step )
CycleByte( NameOfVariable, StartValue, EndValue, Step )
The cycle commands do something a little more interesting; they will shift the value of a variable from the start value to the end, and wrap round again. The rate at which the value is shifted is determined by the step.
This is good for cycling through actions or inventory item indexes; or creating a cycling progress bar.
Again, the point is there is no need to assign the result with the = sign, no need to write the name of the variable twice.
Dim Items$(4)
Items$(1) = "Key"
Items$(2) = "Paper"
Items$(3) = "Pen"
Items$(4) = "Pencil"
CurrentItem = 1
Do
Text 0, 0, Item$( CurrentItem )
CycleInt( CurrentItem, 1, 4, Spacekey() )
Wait 100
Loop
-----------------------------
Results with the current item being changed when the spacekey is pressed
Equivalent of:
Dim Items$(4)
Items$(1) = "Key"
Items$(2) = "Paper"
Items$(3) = "Pen"
Items$(4) = "Pencil"
CurrentItem = 1
Do
Text 0, 0, Item$( CurrentItem )
If Spacekey()
CurrentItem = CurrentItem + 1
If CurrentItem > 4 Then CurrentItem = 1
EndIf
Wait 100
Loop
This is the snippet without the argument library.
//=============================================================================
Function ToggleInt( intVar, iOnValue )
arg = argGetFirst()
If intVar
arg = argSetInteger(arg, 0)
Else
arg = argSetInteger(arg, iOnValue)
Endif
EndFunction
//=============================================================================
Function ToggleByte( byteVar as Byte, byteOnValue as Byte )
arg = argGetFirst()
If byteVar
arg = argSetByte(arg, 0)
Else
arg = argSetByte(arg, byteOnValue)
Endif
EndFunction
//=============================================================================
Function ToggleFloat( floatVar#, fOnValue# )
arg = argGetFirst()
If floatVar# > 0.0
arg = argSetFloat(arg, 0.0)
Else
arg = argSetFloat(arg, fOnValue# )
Endif
EndFunction
//=============================================================================
Function CycleInt( intVar, iStart, iEnd, iStep )
arg = argGetFirst()
arg = argSetInteger(arg, Wrap(intVar + iStep, iStart, iEnd ) )
EndFunction
//=============================================================================
Function CycleByte( byteVar as Byte, byteStart as Byte, byteEnd as Byte, iStep )
arg = argGetFirst()
arg = argSetByte(arg, Wrap(byteVar + iStep, byteStart, byteEnd ) )
EndFunction
//=============================================================================
Function CycleFloat( fVar#, fStart#, fEnd#, fStep# )
arg = argGetFirst()
arg = argSetFloat(arg, Wrap(fVar# + fStep#, fStart#, fEnd# ) )
EndFunction
Getting this to work with your UDT requires
a little extra effort modifying the functions per UDT.
Examples:
FlashText as Boolean
FlashText = 0
Do
Wait 100
ToggleInt( FlashText )
If FlashText Then Text 0,0,"Hey"
Loop
Dim Name$(4)
Name$(1) = "Fred"
Name$(2) = "Ted"
Name$(3) = "Paul"
Name$(4) = "Bill"
CurrentName = 1
Print "Second name after "; Name$( CurrentName ) ;" is ";
[b]CycleInt[/b](CurrentName ,1, 4, 2)
Print Name$( CurrentName )
//--------------------------------------
//Outputs:
//Second name after Fred is "Paul"