Login Screen Example Code

Monkey Forums/Monkey Beginners/Login Screen Example Code

ewancordiner(Posted 2017) [#1]
Hi, was just wondering if anyone had any example code for a login screen on monkey. e.g. something that would read and write from a text file and be able to load data according to the persons login. Or if there was a fixed login and once a user had logged on if the data loaded. Has anyone attempted this? I am trying to have a look into BRL however I do not have mass amounts of time to learn it so as a result of this I think I will struggle.


Gerry Quinn(Posted 2017) [#2]
Well, you could always just use the basic SaveState and LoadState functions to store all data for everyone. You obviously need to devise a format that will work. Let's say the player name is an alphabetic string, and the data is a string of five 0/1 values that say whether they bought a particular item, followed by the level they reached. The string could look like:

"Tommy 00101 14 Angela 10001 17 Peter 00100 4"

You'd make functions to save all current players into the string when the program closes, and to load them when it starts. And of course you need something to add new players while the program is running.

That's about the simplest approach you can use. Write your load and save functions together - some might even put them in the same function. Loading might look like:

Local str:String = LoadState() ' the string above
Local parts:String[] = str.Split( " " ) ' [ "Tommy", "00101", "14", "Angela", ... ]
Local nPlayers:Int = parts.Length() / 3
players = New PlayerData[ nPlayers ]
For Local i:Int = 0 Until nPlayers
players[ i ] = New PlayerData()
players[ i ].name = parts[ 3 * i ]
players[ i ].itemData = parts[ 3 * i + 1 ]
players[ i ].level = Int( parts[ 3 * i ] + 2 )
Next

Just an outline and you'd probably change it, but you get the idea


ewancordiner(Posted 2017) [#3]
That would work I think, however I am thinking perhaps not a register screen but if one user were to have a username and password that they would have to type in every time they went on to the game, and when this was inserted correctly it would load their data. However, I am not sure as of yet how to get arrays working and also how extensive the save/ loadstate is. Because I would need to save and load 'Username,Password,Variables and if they've bought them(the example you used of the 00110010 etc.), the total amount of money accrued. I'm sure you get the point, how in depth is the save and loadstate and where does it save/ load it to exactly?


Gerry Quinn(Posted 2017) [#4]
It's just a string - where it gets stored depends on the target. It can be pretty large, though - I'm not sure of the limits but as far as I know it can be at least a megabyte on every target.

You can store the passwords in it too. The data for every user can be loaded at startup, but the game will only let them in if they type their password. As for arrays, if you are going to have more than one user you're going to need them whatever you do!


ewancordiner(Posted 2017) [#5]
I was thinking glfw3 as the target, would I need to make a text file for that or would it save within the program? Sorry to be a bother.


Gerry Quinn(Posted 2017) [#6]
SaveState() and LoadState() work for any target, so long as you are happy to have a single save file in the form of a string. I'm not sure where it gets stored. Probably a folder relative to where the target .exe is.

You could alternatively use SaveString() and LoadString() with a resource path, to have control over where it is saved.

Or you can use the functions in the BRL modules to save and load arbitrary filetypes.


ewancordiner(Posted 2017) [#7]
https://drive.google.com/drive/folders/0B4SZhjiGhAlAQURmZVhvOWRqYUU?usp=sharing
Sorry, this is the very final product within my coursework and before I handed it in I just wanted to know why it wasn't working, as it was working fine earlier. Thankyou :) (The monkey 0.9) returns a memory access error which appears to stem from angelfont.


Phil7(Posted 2017) [#8]
No error here. When does it occur? I am building with Version 87b, so maybe that is the difference.


ewancordiner(Posted 2017) [#9]
Angelfont doesn't appear to be working which I think is why, I will try updating.