Buttons access like an array
Posted
#1
(In Topic #227)
Trainee
I am writing a game where one of the elements is a 5 x 5 array. The elements in this array are placed by the player and are represented as buttons on the play field. I would like to refer to these button as field[x,y] where x and y number 1 to 5 (I will also be using x=0 for a stack of oncoming play items, so x=0 to 5 and y = 1 to 5 where x will represent distance left to right across the screen, and y= up and down:
|0,1| |1,1||2,1||3,1||4,1||5,1|
|1,2| |1,2||2,2||3,2||4,2||5,2|
Can I name my buttons field[0,1] or something like that, and be able to display something on button 1,2 with : x=1
y=2
field[x,y].text = "[$]"
Which would set the label on the button to [$]
Thanks for your help - sorry if this is not for getting help..
Posted
Expert

I'm a VB.net developer and came from a background of VB6 (many many years ago…)
You are going to love Gambas and the learning curve is not too steep .
The function you need is the 'Group' property that you will see in all ( I think ?) GUI components.
If you click on the 'Group' property and expand the property information display on the left hand side of the screen you will see some very basic information.
In my example , I have set a group of buttons group name to ButtonArray.
In the GUI editor; when you click on any button in this group, Gambas automatically opens the code editor and instead of the Click function for the selected button you will get the group name click function.
so.. ButtonArray_Click().
This traps the click event of any of the buttons whose group name the same so you can code your requirements for them.
To do this , you use the Last function.
Last provides the control in the group that was last activated…by a click event in this case.
The attached example shows some of the properties that are accessible.
Use the checkbox to turn off the message with the clicked button data
I've also included a sub routine that deals directly with (any) object's properties
You can GET or SET any object property with this and it is quite powerful.
Thing to Note# - The property name is case sensitive so if you write one that does not seem to work, check the name in the property for the control in the GUI editor and use the name exactly as displayed - took me a while to work that out. *sigh*
This example is to quickly prepare a form for validation after the user has entered data.
It will go through every control in a container ( normally a Frame or a Panel ) and then ensure that the identified controls are set to what I need.
The Trim(text) function is really handy as it ensures no extraneous spaces are included.
Code (gambas)
- ' The control may be a container so it may have children - do a recursive search
Any questions, let me know.
Button Array Example
Cheers - Quin.
I code therefore I am
I code therefore I am
Posted
Trainee
Anyone—
I went to start my project and now I have a question already - my OS is KDE (devuan to be exact) should I choose - a gtk flavor or QT? I am not sure which KDE runs on, QT perhaps. There is also a general "graphical application" is that more general?
Posted
Guru

Anyone—
I went to start my project and now I have a question already - my OS is KDE (devuan to be exact) should I choose - a gtk flavor or QT? I am not sure which KDE runs on, QT perhaps.
If you are using KDE then Gambas will default to using QT4 or 5 as KDE is built using QT. If you look at menu item Project>Properties and click on Components you will see that Gambas has selected gb.gui which will switch between QT and GTK as necessary.
There is also a general "graphical application" is that more general?
The "graphical application" is the one you will probably use most of the time, well I do anyway!
I have also put together a bit of code for your 'Field' idea, see attached.
Posted
Trainee
Next question - I have looked around at the variable declarations, I will need some global variables but I don't know where to declare them - I have tried above all my 'public sub' subroutines - I have tried in each subroutine and Gambas just says "Unexpected Dim"
- hindsight note edit- I have tried both with DIM and without and both where you see it and inside the form.open…
This is my FMain.class file:
' Gambas class file
Dim Static Public GameLevel As Integer
Dim Static Public Iconstring[50] As String
Public Sub PushStack()
' add a new item to the top of the stack - dropping the bottom most away
BtnStack1.text = BtnStack2.text
BtnStack2.text = BtnStack3.text
BtnStack3.text = BtnStack4.Text
Btnstack4.text = BtnStack5.text
BtnStack5.text = Iconstring[Rand(1, GameLevel * 7)]
End
Public Sub SetupGame()
GameLevel = 1
BtnField11.text = "…"
BtnField12.text = "…"
BtnField13.text = "…"
BtnField14.text = "…"
BtnField15.text = "…"
BtnField21.text = "…"
BtnField22.text = "…"
BtnField23.text = "…"
BtnField24.text = "…"
BtnField25.text = "…"
BtnField31.text = "…"
BtnField32.text = "…"
BtnField33.text = "…"
BtnField34.text = "…"
BtnField35.text = "…"
End
Posted
Trainee
Posted
Regular

waspentalive said
…Next question - I have looked around at the variable declarations, I will need some global variables but I don't know where to declare them - I have tried above all my 'public sub' subroutines - I have tried in each subroutine and Gambas just says "Unexpected Dim …
Generally, you declare a Global variable outside of any routine using "Public" not "Dim" for example:-
If you only need access from other routines in the same class you can use "Private". For example if you have written all your code that needs access to this variable in the FMain.class, then this will also work:-
…but this Private variable will not be accessible if you create a second class unless you access it like:-
Code (gambas)
- x = myClass2.myNumber
I think in VB6 you could create a static variable within a simple routine. This was handy where you wanted a local variable that was persistent. I'm not sure that this works in Gambas.
Posted
Trainee
Public button15_click()
dobuttonclick(Button15, button10, button20)
end
Public dobuttonclick(center as button, top as button, bottom as button)
if top.text = bottom.text then
center.text = "Matches"
else
center.text = "No Match"
end if
end
So here there are lots of buttons, some are above others, some are below. at some point the user clicks a center button of a set of 3. The button above and the button below are compared and the text is set properly of the button in the center (the one that was pressed).
I know I will hate the answer because I have already coded a bunch of buttons in my app and this would have simplified stuff a lot, I could have one block of code that all the buttons call just referring to different buttons as references.
Posted
Guru

It might be easier to help you if you posted the code here so that we can see what you are trying to achieve.
Public button15_click()
dobuttonclick(Button15, button10, button20)
end
Yes this can be done but the other item with 'center.text' etc I'm not sure. Please post the code.
Posted
Trainee
Tiles -
This is what I have so far… I need to implement the rest of my btnfield routines
There is also my main.form - I don't know how to show that to you yet.
It's a game - you have a stack of icons you can draw from. You place these in a 5x5 grid.
When 2 adjacent elements on the grid contain the same icon - they both clear and you get points.
There are also other bonuses you can earn for more points.
You can also drop the next icon for free, but if you drop a 2nd time (or third…) it costs you 1/2 your current points.
As you progress the body of Icons in the stack grows (the number of kinds of icons) , making it harder to pair them up.
You will notice that the BtnFieldxx stanzas are all pretty much alike.
— edit —
Because the btnFieldxx stanzas are alike excepting which buttons they reference I was thinking it might be better to extract that code into a "DoButtonField" subroutine where the neighbor buttons are specified and which of them are edgebonus checks as well -
Code (gambas)
- ' Gambas class file
- ' Declarations for global variables
- ' add a new item to the top of the stack - dropping the bottom most away
- BtnStack1.text = BtnStack2.text
- BtnStack2.text = BtnStack3.text
- BtnStack3.text = BtnStack4.Text
- Btnstack4.text = BtnStack5.text
- BtnStack5.text = Iconstring[Rand(1, GameLevel * 7)]
- ThisRound = ThisRound - 1
- ThisRound = 49
- GameLevel = GameLevel + 1
- txbMessage.text = "Level UP!"
- endgame()
- Case 1
- SetupGame()
- Case 2
- GameLevel = 1
- GameScore = 0
- DestroypPenalty = False
- lastfilled = 0
- ThisRound = 49
- BtnField11.text = "..."
- BtnField12.text = "..."
- BtnField13.text = "..."
- BtnField14.text = "..."
- BtnField15.text = "..."
- BtnField21.text = "..."
- BtnField22.text = "..."
- BtnField23.text = "..."
- BtnField24.text = "..."
- BtnField25.text = "..."
- BtnField31.text = "..."
- BtnField32.text = "..."
- BtnField33.text = "..."
- BtnField34.text = "..."
- BtnField35.text = "..."
- BtnField41.text = "..."
- BtnField42.text = "..."
- BtnField43.text = "..."
- BtnField44.text = "..."
- BtnField45.text = "..."
- BtnField51.text = "..."
- BtnField52.text = "..."
- BtnField53.text = "..."
- BtnField54.text = "..."
- BtnField55.text = "..."
- PushStack()
- PushStack()
- PushStack()
- PushStack()
- PushStack()
- Iconstring[0] = "..."
- Iconstring[1] = "]$["
- Iconstring[2] = "]@["
- Iconstring[3] = "]#["
- Iconstring[4] = "]:["
- Iconstring[5] = "]+["
- Iconstring[6] = "]=["
- Iconstring[7] = "]-["
- Iconstring[8] = ")$("
- Iconstring[9] = ")@("
- Iconstring[10] = ")#("
- Iconstring[11] = "):("
- Iconstring[12] = ")+("
- Iconstring[13] = ")=("
- Iconstring[14] = ")-("
- Iconstring[15] = "}$\{"
- Iconstring[16] = "}@\{"
- Iconstring[18] = "}#\{"
- Iconstring[19] = "}:\{"
- Iconstring[20] = "}+\{"
- Iconstring[21] = "}=\{"
- Iconstring[22] = "}-\{"
- Iconstring[23] = "[$]"
- Iconstring[24] = "[@]"
- Iconstring[25] = "[#]"
- Iconstring[26] = "[:]"
- Iconstring[27] = "[+]"
- Iconstring[28] = "[=]"
- Iconstring[29] = "[-]"
- Iconstring[30] = "($)"
- Iconstring[31] = "(@)"
- Iconstring[32] = "(#)"
- Iconstring[33] = "(:)"
- Iconstring[34] = "(+)"
- Iconstring[35] = "(=)"
- Iconstring[36] = "(-)"
- Iconstring[37] = "\{$}"
- Iconstring[38] = "\{@}"
- Iconstring[39] = "\{#}"
- Iconstring[40] = "\{:}"
- Iconstring[41] = "\{+}"
- Iconstring[42] = "\{=}"
- Iconstring[43] = "\{-}"
- Iconstring[44] = "|$|"
- Iconstring[45] = "|@|"
- Iconstring[46] = "|#|"
- Iconstring[47] = "|:|"
- Iconstring[48] = "|+|"
- Iconstring[49] = "|=|"
- Iconstring[50] = "|-|"
- SetupGame()
- PushStack
- DestroypPenalty = True
- countofempty = 0
- GameScore = 2 * GameScore
- txbMessage.text = "BONUS - you cleared the board"
- endgame
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 15
- txbMessage.text = ""
- BtnField15.text = BtnStack1.Text
- PushStack
- MatchCount = 0
- 'north
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField11.text = "..."
- 'south
- MatchCount = MatchCount + 1
- BtnField14.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- BtnField25.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField55.Text = "..."
- BtnField15.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 14
- txbMessage.text = ""
- BtnField14.text = BtnStack1.Text
- PushStack
- MatchCount = 0
- EdgeBonus = 1
- 'north
- MatchCount = MatchCount + 1
- BtnField15.text = "..."
- 'south
- MatchCount = MatchCount + 1
- BtnField13.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- BtnField24.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField54.Text = "..."
- BtnField14.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 13
- txbMessage.text = ""
- BtnField13.text = BtnStack1.Text
- PushStack
- MatchCount = 0
- 'north
- MatchCount = MatchCount + 1
- BtnField14.text = "..."
- 'south
- MatchCount = MatchCount + 1
- BtnField12.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- BtnField23.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField53.Text = "..."
- BtnField13.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 12
- txbMessage.text = ""
- BtnField12.text = BtnStack1.Text
- PushStack
- MatchCount = 0
- 'north
- MatchCount = MatchCount + 1
- BtnField13.text = "..."
- 'south
- MatchCount = MatchCount + 1
- BtnField11.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- BtnField22.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField52.Text = "..."
- BtnField12.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 11
- txbMessage.text = ""
- BtnField11.text = BtnStack1.Text
- PushStack
- 'north
- MatchCount = MatchCount + 1
- BtnField12.text = "..."
- 'south
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField15.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- BtnField21.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField53.Text = "..."
- BtnField11.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 25
- txbMessage.text = ""
- BtnField25.text = BtnStack1.Text
- PushStack
- 'north
- MatchCount = MatchCount + 1
- EdgeBonus += 1
- BtnField21.text = "..."
- 'south
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField24.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- BtnField35.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField15.Text = "..."
- BtnField25.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 24
- txbMessage.text = ""
- BtnField24.text = BtnStack1.Text
- PushStack
- 'north
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField25.text = "..."
- 'south
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField23.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField34.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField14.Text = "..."
- BtnField24.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 23
- txbMessage.text = ""
- BtnField23.text = BtnStack1.Text
- PushStack
- 'north
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField24.text = "..."
- 'south
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField22.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField34.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField13.Text = "..."
- BtnField23.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
- MatchCount = 0
- EdgeBonus = 1
- txbMessage.text = "That is not empty"
- txbMessage.text = "Multi-Click Bonus = 100"
- GameScore = GameScore + 100
- lastfilled = 22
- txbMessage.text = ""
- BtnField24.text = BtnStack1.Text
- PushStack
- 'north
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField23.text = "..."
- 'south
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField21.Text = "..."
- 'east
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField32.Text = "..."
- 'west
- MatchCount = MatchCount + 1
- 'EdgeBonus += 1
- BtnField12.Text = "..."
- BtnField22.Text = "..."
- DestroypPenalty = False
- GameScore = GameScore + 100 * MatchCount * EdgeBonus
- EmptyOrFull
Posted
Guru

In your File Manager locate your program folder, right click on it and select 'compress'. Save the file using 'tar.gz'.
Click on the 'Attachments' tab and click on 'Add files' button. Locate your compressed file and upload. Select 'display in line' and that's it.
I'll have a look at your program but it would be better if you could upload the whole program.
<IMG src="http://www.cogier.com/gambas/fileload.png">
</IMG>When adding code to the forum use the 'gb' button rather than the '</>' button as it will make your code look much better: -
Using code: -
Code
Public Sub TextBoxEntry_KeyPress(Optional bTrigger As Boolean)Using 'gb': -
Posted
Trainee
This is licensed under GNU v3 - find the latest copy of the GNU Public License at The GNU General Public License v3.0 - GNU Project - Free Software Foundation
Partially complete Tiles game
Posted
Guru

I have got the code down from 1170 lines to 275. I have added a logo. You can now resize the form and the font size will automatically resize. There is now a 'Edge Bonus' banner. When a matching pair is found you can now see the pair before they disappear. Note the use of 'Group' 'AllFieldButtons' for all the 'BtnField' buttons.
Things to watch for in your code: -
Only use 'Quit' in command line programs. In GUI programs use 'Me.Close' to close the form.
You can use 'Inc' (Increase) or 'Dec' (Decrease) to add or subtract 1 from a value.
So 'x = x + 1' can be just 'Inc x'
There is no need to 'Dim x as Integer' and then set it value to 0, it's 0 already. You can do this if you want: -
'Dim x as Integer = 15'
If I have missed the point with some part of the program please let me know or if you want an explanation of any of the code let me know.
Posted
Trainee
Posted
Guru

If it is earlier than 3.11.0 then you need to update. The 'Spring' feature was add in this version.
Close Gambas.
Copy the code below.
Code
sudo add-apt-repository -y ppa:gambas-team/gambas3 && sudo apt-get update && sudo apt-get -y install gambas3 Paste in the code by right clicking in Terminal and selecting Paste.
Press [Enter]
Enter your password and press [Enter] (You may not see anything happening when you type your password but if you input it correctly all will be fine).
Lots of text will be created…….
When the above is finished restart Gambas.
This will give you access to the Gambas repository and update your version of Gambas to 3.12.2. It will also keep you updated when there are further updates.
Posted
Trainee
but not till my weekend (tues/wednesday)
Posted
Guru

However, here is the same code with no 'Springs'.
1 guest and 0 members have just viewed this.

