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.

Newcomers DBPro Corner / question about arrays!

Author
Message
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 27th Jan 2013 23:58
Some of you may have seen my other post about saving and loading games from a file, so player could save his game. Could you also do this with arrays?

I know you can store specific info into an array. I know there are commands to add to and delete from an array.

So why couldn't you make a function that saves specific game info into arrays and/or UDT's such as an array or udt to store all the active player characters in a game and an array to save the locations and one to hold stats? Then when the player saves the info is stored in it's respective array.

To me this sounds like it can be done and I am going to play around with some code.

If this can be done would it work with the exe file so the player could leave the game, come back later load the game and continue from where he left off?

This would be pretty cool!
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 28th Jan 2013 05:31
I do something similar all the time, however only for saving arrays to memblocks/banks to be sent over a network.

It is a matter of personal choice.

You cannot save a UDT array to file, but you can convert it to a memblock and save the memblock to file.

If you change the structure of your UDT, you must create a function that converts old UDT files to the new. The structure of the array must be the same as the one on file.

Saving a UDT to file should be faster than working with text based files. The parsing process is far easier to work with, no need to parse fields one by one.

Some of the advantages of text files is that you can make changes more easily, and could write test files out in a text editor or database.

If you intend to use any other program, database or language to interact with your game, then text files are far easier to interpret; but they also need encryption.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Jan 2013 07:45
See if this helps you.
http://dbcodecorner.com/darkbasic/fileaccess_tutorial.pdf

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Nytus Sermus
11
Years of Service
User Offline
Joined: 17th Jan 2013
Location:
Posted: 29th Jan 2013 06:22
Late is better than never i guess, but the short answer your looking for is yes. You can actually read and write any data you want through combining arrays with read/write of files. The only stipulation is that you dont mix up any of the data (which index you read/write) and that you apply all of the values back to thier respective properties when you re-load it.

You could be done already.....stop trying to re-invent the wheel.
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 29th Jan 2013 15:44
I may be misunderstood here. I know you can read and write to files. I was wondering if you can do it without writing to a file. Could you make a game save function that would store all the right data to array's so that when the player comes back later to play the game he can hit load game and have all the right data loaded back and he can continue where he left off. Maybe use array's and a game save udt?

I am just curious and throwing ideas out there. I think this is a cool idea, all the game save data could be stored within the game, no external files except media files. I am pretty sure it can't be done with media since that is physical stuff, images, textures ect... but your save game data is just that data, strings and numbers. Character names, locations, stats, items, magic etc so why can't this stuff be stored within the game as a game save then be loaded back when the player wants to play again?

I have been around a while but have only been learning for a short while but I think this can be done but if someone can explain why it can't be done I would be very interested to know why not!

Then again I am talking about a text game here not 2D or 3D but I think it would work there too because the only thing you are saving to the array's is mostly character/player data and when the 2D/3D game initializes it is going to load the media anyway from the media file and it could also load that character/player data from the array's/UDT's
zeroSlave
15
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 29th Jan 2013 16:11 Edited at: 29th Jan 2013 16:38
Quote: "I am pretty sure it can't be done with media since that is physical stuff, images, textures ect... but your save game data is just that data, strings and numbers. Character names, locations, stats, items, magic etc so why can't this stuff be stored within the game as a game save then be loaded back when the player wants to play again?"


Technically, all the data (Character names, locations, stats, etc.) is also "physical" stuff. Models, images, and textures are data just like a save file. DBP just knows what to do with an image when loaded. It's got a specific "array" that it stores all the information of the image into internally so that it can be used by other DBP functions.

An image's data structure could be set up such as:

Which would be stored in an array such as imageArray(ImageWidth, ImageHeight)

Sample psuedo code:


A model's data structure could be:

Which would be stored in an array such as modelArray(VertexCount)

Sample psuedo code:

(This is not the exact data format for an existing 3d model or image type [does not include image bitdepth, limb names, etc.], but it should get the idea across. Look into memblocks if you are interested in file structure. Although it can seem extremely abstract, it will get you a better idea of how data is stored and then opened into memory for use by your application.)

A save game data structure could be:


Once the program is compiled, you will not be able to apply any internal changes to variables. (I mean, I guess you could with a hex editor or something, but that seems impractical) You will want to do this with an external file loaded. If you don't read/write to an external file, you will be stuck using static variables for everything in the game. You will need to load an external file so that you can fill in all the data for a loaded game with dynamic information.

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 29th Jan 2013 18:00
Ok so why not put everything you want to save into a udt called game save and have that udt loaded up the next time you load the game. I have been learning about udt's and saving stuff you want to change in the udt with a function and an array's. I am not sure what can be changed and when but when you are playing a game (I am an RPG guy) your hit points and stats change.

So you have played 2 hours now you want to quit, you are in the game exe so you hit an option to save the game why can't you have your pos., hitpoints and all other stats. be saved in a game save udt with functions and array's before the game exits then when you start again the load function loads up all that info in the save game udt and you start where you left off?

I am working on some code to try something like this. It may be a while even a few days but if I get something working the way I want or think it should I'll post it here.
zeroSlave
15
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 29th Jan 2013 18:58 Edited at: 30th Jan 2013 02:50
I'm not a genius when it comes to data (nor am I the most verbose), but this is my understanding:

A UDT [b][/b](user defined type) is like a struct in C++. It is just a data structure. Some other data types are integers, booleans, floats, dwords, etc. Each data type has a different amount of space it uses. Floats and Integers use 4 bytes of data. A double stores 8 bytes. A user defined type may take up 32 bytes or more/less!

An array is a way to store multiple variables that are one data type into a 'single' variable. RAM can essentually be viewed as a single dimension array. So can a harddrive.

UDT's and arrays don't really store the information. They are actually a series of memory pointers, pointing to where the data is stored in your random access memory.(RAM) Once you close the game out, all the memory resources for it would be released, clearing out all the information. If you don't save the dynamic information (hitpoints, experience points, items in inventory) to an actual physical file, (a file on a Hard-drive/disc/flash drive) you will lose the data.

Your harddrive stores all the information(including the executable, the models, the images, the save data(HP, XP, str, int, wis, etc).



Your program loads the file from the harddrive then stores it in RAM and grabs the memory address for the specific information.
hp information is the integer '100' which is stored at address 0x03FF

Your program retrieves information from RAM by memory address pointers to utilize the data.
int myProgramHPVariable = an integer stored at 0x03FF

You exit your program and with it, (ideally) the RAM being used by your program is released.
the data at 0x03FF is now 'null'

If the file is not loaded from the harddrive and placed in RAM:
Your program retrieves information from RAM by memory address pointers to utilize the data.
int myProgramHPVariable = an integer stored at 0x03FF
IT's A NULL VALUE!!!! (or even information being stored by another application!)

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2013 19:05
Quote: "I may be misunderstood here. I know you can read and write to files. I was wondering if you can do it without writing to a file. Could you make a game save function that would store all the right data to array's so that when the player comes back later to play the game he can hit load game and have all the right data loaded back and he can continue where he left off. Maybe use array's and a game save udt?"


That doesn't make any sense. Only way something like that could work is if you never closed the game in the first place. Once the game is closed, there is only 1 way to save a game state and that is to write data to a file. And technically, anything can be saved. If you've edited physical media, the current animation frame, or random numbers, you can save them all. But you have to write that information to a file.


Quote: "So you have played 2 hours now you want to quit, you are in the game exe so you hit an option to save the game why can't you have your pos., hitpoints and all other stats. be saved in a game save udt with functions and array's before the game exits then when you start again the load function loads up all that info in the save game udt and you start where you left off?
"

You can, but why would you create a UDT specifically for saving data? When you read it back in, you'll just have to copy that data from the UDT back to the other game variables anyway. You'd be creating yourself an unnecessary step.




"You're not going crazy. You're going sane in a crazy world!" ~Tick
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 29th Jan 2013 20:30 Edited at: 29th Jan 2013 20:42
You can just store your data in the UDT array, forget variables.

Only use non UDT-variables for testing. Use UDTs as much as possible, take advantage of autocomplete and field compile checks.

Never store the same info in more than one place in the same program, unless the additional place provides faster or easier access/transmission required for something to work.

You must save game data to file because the UDT is in RAM. When your game ends, Windows wants to claim that RAM back for other programs.

Anti-virus software will think you are creating a virus if you attempt to alter your .EXE program data.

Save the game and load the game like so.


Fairly simple. Do the same with all UDTs. Consider file blocks
if you want to be tidy.

Simply keep all of your data in one place; in your main UDT arrays. Place objects like so
. If you need to perform a series of calculations and you notice a drop in speed, save a few frames per sec by storing a local UDT like so:
.

This is the easy way, but simple tools offer limited extras. You'll have to keep your game simple to effectively work with loads of UDT files. If it gets complex, text files are more organized; no need to recreate the files every time you change the structure of your UDT; plus with UDTs all information which may not be important still gets saved.

Dragon slayer
18
Years of Service
User Offline
Joined: 3rd Nov 2006
Location: Bourbonnais Illinois
Posted: 29th Jan 2013 20:50
I was just thinking way outside the box, I see your point. I am looking at the tutorial Phaelax posted a few posts up. Very good, helpful and easy to understand

Login to post a reply

Server time is: 2024-11-23 13:15:59
Your offset time is: 2024-11-23 13:15:59