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.

DarkBASIC Professional Discussion / MAKE ARRAY FROM MEMBLOCK and MAKE MEMBLOCK FROM ARRAY - Why it doesn't work properly????

Author
Message
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 9th Jun 2019 01:45
The situation is the following:

I want to store an array into a memblock, save the memblock into a file, then reload the file, retrieve the memblock and turn it back into an array.

The example works like this:
First it makes an array, fills it with random values, then transform the array into a memblock and writes the memblock to a file and show the array count to ensure that the values were correctly inserted.
The second part does the inverse, reloading the file, reading the memblock, transforming it into an array and showing once again the array count to ensure it worked properly.

The issue is: in the end, array is EMPTY!!!! WHY?????



God is real - except if declared as integer.
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 9th Jun 2019 03:27
Another approach to the same problem and it doesn't work as well.



Why? It saves the data to the file, but I can't rebuild the array! HELP!!!
God is real - except if declared as integer.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 9th Jun 2019 15:54 Edited at: 9th Jun 2019 16:03
This will not work with string variables because of the way Dark Basic handles strings. When you create a string variable, Dark Basic will store the string data, but the variable itself will only be a pointer to that data. So when you convert the array data to a memblock or save an array, you get the pointer and not the actual string data.
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 9th Jun 2019 16:22 Edited at: 9th Jun 2019 16:46
Ooooh, my goodness!
Thanks, mr. WickedX, for one more helpful explanation.
In the help files there is too little info about these commands, what made it impossible to me to find this out by myself. Thanks to you,
now I know.
How could I be so dumb?
Strings are like the ones in C, just 1D arrays of char variables. Now I understand...

Since I use RNGs for everything, I just need to replace the strings by integers and, if I need to store a string, I can just store its seed number and re-generate it on load. Nothing too complicated, and thanks to you, it is simpler than I ever thought.

It's all I need to produce savegames that no n00b will be able to edit using Notepad. Now I can make my game as big as I want, because the player won't need to restart it all over every time he or she decides to resume playing.

After almost 2 gallons of coffee and an entire night without sleeping, waiting for a solution I couldn't find by myself, at least it came. Thank you once again, WickedX, you ROCK!

EDIT: I rewrote the code and guess what? It saves but don't load back the array...


EDIT: Well, I got it working.
It doesn't work with dynamic arrays. I need to dim it with the final size it will be and reload the memblock to an array with the same size. No queues, stacks or lists.
Is there any way to change the size of the array directly inside the memblock?
God is real - except if declared as integer.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 9th Jun 2019 18:24 Edited at: 9th Jun 2019 18:59
Something like this?

SaveArray


LoadArray
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 10th Jun 2019 10:01
Oh, great, bro! Thanks a lot!
BTW, can I use UDT arrays or just normal ones?
God is real - except if declared as integer.
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 10th Jun 2019 19:22
Aren't there built in save array / load array commands? (Don't have the docs handy at the moment)

I'm pretty sure you shouldn't even need to bother with memblocks for this
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 11th Jun 2019 01:22
Yes, user defined types work just fine. Save Array and Load Array will give “array type invalid” error with user defined type arrays.
Starshyne Emir
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location: Porto Alegre, southern Brazil
Posted: 11th Jun 2019 02:14
@Ortu, yes, there are Save and Load array functions.
The problem is: 1) they don't work with user-defined types, 2) the file can be edited easily from outside the program and 3) the file is entirely used to save the array.
With memblocks, I can hide the data I want to save in a way the common user won't be able to hack and cheat in my game.

Now I have what I need. Thank you all for the gentle and quick feedback.
God is real - except if declared as integer.

Login to post a reply

Server time is: 2024-04-20 09:23:51
Your offset time is: 2024-04-20 09:23:51