Page 1 of 1

Pickle Save Dict

Posted: Fri Jun 03, 2016 8:44 am
by decompile
Hey,

I currently worked on a script which edits a map so I can delete specific models which are in the zone.

However

my Dict looks like this:

Syntax: Select all

removeDict = {'mg_course_dev_server': {'deletelist': [], 'deleteCoords': [(Vector(-385.1864318847656, 3057.462646484375, 22.29857063293457), Vector(-144.25393676757812, 3600.712646484375, -31.96875), Vector(-468.97247314453125, 2856.05029296875, 43.889564514160156), Vector(0.0, -95.67604064941406, 0.0))]}}


When I want to save it via this script (which worked on 2.5)

Syntax: Select all

def saveDict():
fileStream = open(mapsPath, 'wb')
pickle.dump(removeDict, fileStream)
fileStream.close()


Im getting

Code: Select all

RuntimeError: Pickling of '_mathlib.Vector' instances is not enabled (http://www.boost.org/libs/python/doc/v2/pickle.html)

Re: Pickle Save Dict

Posted: Fri Jun 03, 2016 8:48 am
by Ayuto
You can work around that issue by converting the Vector instances to a tuple when you want to save it. Then, when you read it, you simply create Vector instances from the tuples.

Re: Pickle Save Dict

Posted: Fri Jun 03, 2016 9:33 am
by BackRaw
If you need some more help you could look at my SpawnPoints cookbook example. It uses JSON and list(), however the principle is the same: viewtopic.php?f=31&t=751 [needs to be updated, haven't looked into it for ages...]

Re: Pickle Save Dict

Posted: Fri Jun 03, 2016 2:34 pm
by decompile
Ayuto wrote:You can work around that issue by converting the Vector instances to a tuple when you want to save it. Then, when you read it, you simply create Vector instances from the tuples.


How would that look like

Re: Pickle Save Dict

Posted: Fri Jun 03, 2016 3:21 pm
by iPlayer
I believe like this way

Vector -> tuple

Syntax: Select all

tuple(my_vector)


tuple -> Vector

Syntax: Select all

Vector(*my_tuple)

Re: Pickle Save Dict

Posted: Fri Jun 03, 2016 4:27 pm
by decompile
Works, thank you

Re: Pickle Save Dict

Posted: Fri Jun 03, 2016 5:17 pm
by Ayuto
I have added pickle support for the mathlib classes, so pickling Vector instances will work with version 345:
https://github.com/Source-Python-Dev-Te ... 36c5f685e5

Re: Pickle Save Dict

Posted: Sat Jun 04, 2016 10:01 am
by decompile
Ayuto wrote:I have added pickle support for the mathlib classes, so pickling Vector instances will work with version 345:
https://github.com/Source-Python-Dev-Te ... 36c5f685e5


damn awesome, thank you!!

Re: Pickle Save Dict

Posted: Tue Oct 04, 2016 7:46 pm
by D3CEPTION
Ayuto, could you also add a "normalized" - method to mathlibs Vector module? right now, im always changing it locally, but always have to update it myself. would help if its added to sp too. the math is just : copy the vector and divide it by the native Vector.normalize(). thanks

Re: Pickle Save Dict

Posted: Tue Oct 04, 2016 10:41 pm
by satoon101