Pickle Save Dict

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Pickle Save Dict

Postby decompile » Fri Jun 03, 2016 8:44 am

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)
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Pickle Save Dict

Postby Ayuto » Fri Jun 03, 2016 8:48 am

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.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Re: Pickle Save Dict

Postby BackRaw » Fri Jun 03, 2016 9:33 am

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...]
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Pickle Save Dict

Postby decompile » Fri Jun 03, 2016 2:34 pm

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
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Pickle Save Dict

Postby iPlayer » Fri Jun 03, 2016 3:21 pm

I believe like this way

Vector -> tuple

Syntax: Select all

tuple(my_vector)


tuple -> Vector

Syntax: Select all

Vector(*my_tuple)
Last edited by iPlayer on Fri Jun 03, 2016 4:53 pm, edited 1 time in total.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Pickle Save Dict

Postby decompile » Fri Jun 03, 2016 4:27 pm

Works, thank you
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Pickle Save Dict

Postby Ayuto » Fri Jun 03, 2016 5:17 pm

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
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Pickle Save Dict

Postby decompile » Sat Jun 04, 2016 10:01 am

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!!
User avatar
D3CEPTION
Senior Member
Posts: 129
Joined: Tue Jan 26, 2016 1:24 pm
Location: Switzerland

Re: Pickle Save Dict

Postby D3CEPTION » Tue Oct 04, 2016 7:46 pm

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
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Pickle Save Dict

Postby satoon101 » Tue Oct 04, 2016 10:41 pm

Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 25 guests