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 / Write a type to a file

Author
Message
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 10th Feb 2010 01:18
How can I write the contents of a typed variable?


Of course, I know it doesn't work, but is there a way to do it???

The fastest code is the code never written.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 10th Feb 2010 01:33
I think you have to just write each value to the file. i would suggest making a function to do it for you.



New Site! Check it out \/
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 10th Feb 2010 01:49
That stinks!

The fastest code is the code never written.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 10th Feb 2010 02:32
Why does that stink?

It means you can control the format, such as compression, encryption and how the file is layed out.

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 10th Feb 2010 06:51
He just means it stinks that he has to write it all out. It is kind of a pain to write out every single object in the type. Esspecially if he has a large type which lots of objects.

New Site! Check it out \/
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 10th Feb 2010 08:06
Quote: "It is kind of a pain to write out every single object in the type. Especially if he has a large type which lots of objects."

IMO that's just lazyness. It's easy enough just to copy the type declaration and insert the bits needed. it'll only take a few seconds to write that function, hardly earth shattering.

My signature is NOT a moderator plaything! Stop changing it!
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 10th Feb 2010 14:55
Quote: "It's easy enough just to copy the type declaration and insert the bits needed."

What do you mean??? I don't know how to do that.

The fastest code is the code never written.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 10th Feb 2010 15:00
I mean copy the variables from the declaration, then paste them into a function to write them.

My signature is NOT a moderator plaything! Stop changing it!
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 10th Feb 2010 15:11
That seems retardedly long if I have a HUGE type.... I know HOW to do that, but I was hoping DBP would be like most languages and allow you to define the save format.... If it's not possible, then I guess I'll have to do it the hard way.

The fastest code is the code never written.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 10th Feb 2010 15:11
Nope, hard way it is. It is a basic language after all! lol

My signature is NOT a moderator plaything! Stop changing it!
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 10th Feb 2010 15:13
Thanks for the input...

The fastest code is the code never written.
Masqutti
14
Years of Service
User Offline
Joined: 8th Jan 2010
Location: insanity
Posted: 10th Feb 2010 16:00
If you saved the data to an array that hasn't user-defined type it would be easy to save things with for next.

It makes your code unreadable tho..

like array(100,10) , where's 100 is number of the enemies and the second dimension is the attributes of a single enemy..

then just


There's a way to put it. I wouldn't use it tho as you can do errors so easily..

hmmmh.. that didn't compile
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 11th Feb 2010 00:37 Edited at: 11th Feb 2010 00:38
If you download IanM's matrix1utility plugin pack, it's possible to write a function to work on ANY UDT array.

First, write out the size of the array to the file (the value returned by 'array count').

Then, loop through each item in the array, and use 'get array item ptr' to get a pointer to each one. Inside this loop, have another loop which goes from 1 to the value returned by 'get array field count'.

This lets you loop through each field in the UDT of the array element. You can use 'get array field offset' for each of these fields, and add it to the item pointer found earlier. This gives you a pointer to the actual field.

Then use 'get array field type', and make a select statement to check for each type. The values returned are shown below:

0 = Integer
1 = Float
2 = String
3 = Boolean
4 = Byte
5 = Word
6 = Dword
7 = Double float
8 = Double integer

Depending on the value returned, use 'peek XXX' (XXX = the name of the data type from the above list). And pass in the field pointer. Then write out this value to the file. The only exception is if it returns '2' (string). For strings, use 'peek string(peek integer(pointer))' instead of 'peek string(pointer)'.

When you have done all that, your code will be able to save any array to a file. To read it back in do the following:

First, read in the first integer (the array size) and resize the array to that value. Then do exactly the same as before, but instead of writing to the file, read the values from the file, and instead of using 'peek XXX' use 'poke XXX'.

For strings, use 'free peek integer(pointer)' to free the old string, and then use 'poke integer pointer,alloc string(value))' to set the new value.

Login to post a reply

Server time is: 2024-04-26 16:48:31
Your offset time is: 2024-04-26 16:48:31