Object Manipulation with Python

New releases and general discussions.

Post » Sat Aug 21, 2010 8:37 am

Alright, I'm creating a lot of objects from within Python, and I was wondering if there was a more reliable way to keep track of all of them than just keeping a running track of the index of the last one. (Sprite[5]) or whatever, as I create them.

I noticed there was an object type in the script editor, and I tried messing with it to store an actual reference to each object in a list, but no dice.

I tried something like this:
Bob = object()
Bob = Create("Sprite", 1, System.MouseX, System.MouseY)
Bob.Angle = 5

I need to store a bunch of these in a list/array...so some actual concrete references would be nice..."create" creates one, but does not hold it as the event sheet does, so references would rock. I'm trying to write a custom data structure to hold a bunch of line objects and describe them in a relationship.
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Sat Aug 21, 2010 10:07 am

I don't think u need the object()

Also, I believe the py plugin which allows you to use picking data in python might solve this problem for you,
viewtopic.php?f=2&t=5305&p=42100&hilit=pyfix+plugin#p42100
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sat Aug 21, 2010 6:26 pm

Well if I try just Bob = System.Create it gives me a NoneType error when I try to set the angle. No error on the actual creation, heh...

I'd seen the object picking fix, but I don't think that will help me in this particular case, since everything will be handled within the python.

Is there some way to do this?
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Sat Aug 21, 2010 10:10 pm

I was annoyed that the pyfix plugin didn't work for this, so I made you another one:
http://dl.dropbox.com/u/1013446/pycreate.rar
(plug and example cap included)
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun Aug 22, 2010 1:57 am

The normal return value of System.Create is None, which is python's null value.

All of my past attempts to store a direct, persistent reference to a single instance of a multi-instance Construct object have failed. Many of the python methods for Construct objects don't work properly, if they are even there, and work at all. It definitely needs some work.

As far as the index number goes, they are practically scrambled every time an instance is destroyed, so they can't be counted on to point to the same instance under all circumstances. Here's the output of a quick test with my PyShell.cap:

[code:4py0sf3d]>>> ls = []

>>> for i in range(5):
ls.append(System.CreateByName('Sprite',1,i*36+16,16))


>>> ls
[None, None, None, None, None]

>>> for i, s in enumerate(Sprite):
print i, s.uid

0 1
1 3
2 4
3 5
4 6
5 7

>>> Sprite[2].uid
4

>>> Sprite[2].Destroy()

>>> for i, s in enumerate(Sprite):
print i, s.uid

0 1
1 3
2 7
3 5
4 6

>>> Sprite[2].uid
7[/code:4py0sf3d]

I'm afraid that this affects your above plugin's function as well, Lucid, though I really like the idea.

Not saying that there isn't a better way that I haven't found, but the only way I know of to reliably reference an instance is to store it's uid, as you might do with events. Then, you can use something like so:

[code:4py0sf3d]def rotateInstance(obj, uid, deg):
for i in obj:
if i.uid = uid:
i.angle += deg[/code:4py0sf3d]

... which kind of sucks. I keep hoping that I stumble across a clever way to eliminate that loop. ;)

That said, I still find Python quite useful for many things in Construct, but you may need to interface it with normal events for some things. Construct's function object can help with that.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sun Aug 22, 2010 2:42 am

[quote="Silent Cacophony":1bi7045e]
I'm afraid that this affects your above plugin's function as well, Lucid, though I really like the idea.
[/quote:1bi7045e]
if he only needs store the objects python index temporarily this should work fine, which I assumed was the case given his bob example
however, if he needs to store many instances long term, you can already manipulate large arrays of objects using python with s
also, btw arsonide, if you just want to create the object and assign it all the various dimension, position, information etc, I could just add that to the create function, so you don't have to worry about storing instance data at all
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun Aug 22, 2010 3:17 am

[quote="lucid":2f08wjfd]if he only needs store the objects python index temporarily this should work fine, which I assumed was the case given his bob example
however, if he needs to store many instances long term, you can already manipulate large arrays of objects using python with s[/quote:2f08wjfd]

True, it would work fine as long as you don't use the reference past the point that an instance may be destroyed, which could be never in some cases. This would be great for setting up initial values, as you mentioned.

Thanks for the tip on your S plugin. I had been using python for my complicated data structure needs. That comment just opened my eyes to some new possibilities. I'll be checking S out, soon.

Seems as though one (or both) of those two solutions pretty much covers it, hopefully. :)
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Tue Aug 24, 2010 5:33 pm

With the the latest version of Construct(0.99.93) you can reference a newly created object in python like so:
[code:27wv8267]System.Create('Sprite',1,0,0)
objRef=SOL.Sprite #this gets the reference of the new Sprite.

objRef.x=400
objRef.y=300
objRef.angle=30
objRef.skewy=20[/code:27wv8267]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Wed Aug 25, 2010 8:07 am

What dowry do you require in order for me to marry you, R0J0hound?
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Wed Aug 25, 2010 8:41 am

[quote="R0J0hound":1w1dg6o9]With the the latest version of Construct(0.99.93) you can reference a newly created object in python like so:
[code:1w1dg6o9]System.Create('Sprite',1,0,0)
objRef=SOL.Sprite #this gets the reference of the new Sprite.

objRef.x=400
objRef.y=300
objRef.angle=30
objRef.skewy=20[/code:1w1dg6o9][/quote:1w1dg6o9]
Excellent! Any caveats to this? I assume you can do

[code:1w1dg6o9]
System.Create('Sprite', 1, 0, 0)
objRef = SOL.Sprite
objRef.x = 300
System.Create('Sprite', 1, 0, 0)
objRef = SOL.sprite
objRef.x = 600
[/code:1w1dg6o9]
B
2
S
1
G
3
Posts: 71
Reputation: 1,142

Next

Return to Construct Classic Discussion

Who is online

Users browsing this forum: No registered users and 3 guests