Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - [ARGS] Toggle or cycle through values directly

Author
Message
Chris Tate
DBPro Master
14
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 11th Jan 2012 21:07 Edited at: 20th Jan 2012 22:53
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:


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:


This is the snippet without the argument library.



Getting this to work with your UDT requires a little extra effort modifying the functions per UDT.

Examples:





Diggsey
16
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 11th Jan 2012 23:18
Nice to see my library is being useful

[b]

Login to post a reply

Server time is: 2022-12-01 12:29:24
Your offset time is: 2022-12-01 12:29:24