How to create a table of records?
- Juan Carlos
- Posts: 891
- Joined: Mon Sep 06, 2010 1:02 pm
How to create a table of records?
I need to do a table records with 10 names with their 10 scores and their 10 money earn, but the condition to order the new records is first the high score and when the new record has the same score to use the money for the order, and this with each new score, I have done the code to save and open the records inside a table with this orden hallfametable={"name", "points", "money"..
until the 10 names to save at most.
Some idea, thank you for your help.
until the 10 names to save at most.
Some idea, thank you for your help.
Re: How to create a table of records?
I think you need an extra row called "id"
Which will be increment in every new entry
If size/complexity is not an issue sqlite has all these natively
Which will be increment in every new entry
If size/complexity is not an issue sqlite has all these natively
Christos
- Juan Carlos
- Posts: 891
- Joined: Mon Sep 06, 2010 1:02 pm
Re: How to create a table of records?
I not want use plugins because this add more kb to executables.
Re: How to create a table of records?
I would use two temporary arrays sorted by Sort()
One array = money
One array = score
Then would sort them in a third temporary aatay based in these indexes (my two arrays)
One array = money
One array = score
Then would sort them in a third temporary aatay based in these indexes (my two arrays)
Christos
Re: How to create a table of records?
I would do this with a custom sort function, like this for example:
Code: Select all
Function p_SortFunc(a, b)
If a.points = b.points
; If points are equal, sort by money
Return(a.money > b.money)
Else
; Otherwise sort by points
Return(a.points > b.points)
EndIf
EndFunction
Function p_AddScore(name$, points, money)
; Insert the new score in the table:
InsertItem(hallfametable, {name=name$, points=points, money=money})
; Sort the table with a custom sort function:
Sort(hallfametable, p_SortFunc)
; If there are more than 10 entries, remove the last one:
If ListItems(hallfametable) > 10 Then RemoveItem(hallfametable, -1)
EndFunction
; Create an optimized list, not that it would matter with a small table, but anyway :)
hallfametable = CreateList()
; Fill it with example values
For Local i = 1 To 10
p_AddScore("player" .. i, 100*Rnd(3), 50*Rnd(3))
Next
; Print the table
For Local i = 0 To ListItems(hallfametable) - 1
NPrint(hallfametable[i].name, hallfametable[i].points, hallfametable[i].money)
Next
WaitLeftMouse()
- Juan Carlos
- Posts: 891
- Joined: Mon Sep 06, 2010 1:02 pm
Re: How to create a table of records?
Thank you very much jPV, I thank you so much your code example because I tryed several times to do a table records for my bizarre games and always I found the problem how order the table with the new record points.
- Juan Carlos
- Posts: 891
- Joined: Mon Sep 06, 2010 1:02 pm
Re: How to create a table of records?
Hi jPV I have a problem with my prototipe table and your fantastic example, I have read the documentation but my table isn't possible display it, I have some novel with the programming oriented object as your hallfametable.points here is the example:
Perhaps my table with name, points and money is bad though, I have this doubt.
Again thank you for your help.
Code: Select all
hallfametable={"Joe", 100, 200, "Juan", 85, 545, "Lucia", 78, 123, "Ines", 45, 289, "Rene", 21, 127,
"Megan", 20, 521, "Manuel", 15, 234, "Rodrigo", 6, 13, "Laura", 5, 56, "Jack", 4, 129}
Saltoy=40
SetFont(#SANS, 24)
SetFontColor(#YELLOW)
SetFontStyle(#ANTIALIAS)
;Print the table
For Local i=0 To ListItems(hallfametable) - 1
TextOut(30, Saltoy, hallfametable[i].name)
TextOut(210, Saltoy, hallfametable[i].points)
TextOut(340, Saltoy, hallfametable[i].money)
Saltoy=Add(Saltoy, 30)
Next
Again thank you for your help.
Re: How to create a table of records?
You have put all information in one table, which will be more difficult to handle and doesn't work with my example. It will be easier to handle everything if you put each score (that contain name, points and money) into its own table inside the one main table. The whole thing is nested tables then...
This would be compatible with my example:
Or you could create the initial table using the p_AddScore function I made:
p_AddScore("Joe", 100, 200)
p_AddScore("Juan", 85, 545)
p_AddScore("Lucia", 78, 123)
...and so on...
Then each score with all its information is a single item in the hallfametable, and you can remove a whole score easily, for example RemoveItem(hallfametable, 2) would remove Lucia's score totallly.
If you don't like string indices, you could do this also like:
But then you have to use hallfametable[j][0] for the name, hallfametable[j][1] for the points, and hallfametable[j][2] for money. It would be shorter code, but harder to remember and read...
So, I would prefer use string indieces where you can just use hallfametable[j].name (or hallfametable[j]["name"]) just like in my example... it makes it easier to understand the code afterwards.
This would be compatible with my example:
Code: Select all
hallfametable={
{name="Joe", points=100, money=200},
{name="Juan", points=85, money=545},
{name="Lucia", points=78, money=123},
{name="Ines", points=45, money=289},
{name="Rene", points=21, money=127},
{name="Megan", points=20, money=521},
{name="Manuel", points=15, money=234},
{name="Rodrigo", points=6, money=13},
{name="Laura", points=5, money=56},
{name="Jack", points=4, money=129}
}
p_AddScore("Joe", 100, 200)
p_AddScore("Juan", 85, 545)
p_AddScore("Lucia", 78, 123)
...and so on...
Then each score with all its information is a single item in the hallfametable, and you can remove a whole score easily, for example RemoveItem(hallfametable, 2) would remove Lucia's score totallly.
If you don't like string indices, you could do this also like:
Code: Select all
hallfametable={
{"Joe", 100, 200},
{"Juan", 85, 545},
{"Lucia", 78, 123},
...and so on...
}
So, I would prefer use string indieces where you can just use hallfametable[j].name (or hallfametable[j]["name"]) just like in my example... it makes it easier to understand the code afterwards.
- Juan Carlos
- Posts: 891
- Joined: Mon Sep 06, 2010 1:02 pm
Re: How to create a table of records?
Again thank you for your help jPV, it's difficult for me understand the table work and it is very necessary to dominate to make adventure or RPG games for the tables with names, energy, skills, etc.
Thank you and I'll try to get success with this Hall of Fame, where I'll put your name in first place.
Thank you and I'll try to get success with this Hall of Fame, where I'll put your name in first place.
Re: How to create a table of records?
Yeah, learing to understand tables and their possibilities is very useful and important thing to do. It makes coding so much easier (and shorter) when you learn to utilize the tables in all kinds of situations. And remember that Hollywood can store all kind of data into tables.. makes things fun It may feel difficult at first, but it is quite simple after all when it opens to you.
And btw. if you do the hiscore table in my way (you keep it limited to 10 and sorted when adding etc), writing and reading it from the disk can be very simple, just write and read the whole table at once with ReadTable() and WriteTable(), for example. Just mentioning because it sounded that you did more work there originally...
And btw. if you do the hiscore table in my way (you keep it limited to 10 and sorted when adding etc), writing and reading it from the disk can be very simple, just write and read the whole table at once with ReadTable() and WriteTable(), for example. Just mentioning because it sounded that you did more work there originally...