Is it possible to create complete scripts in python ?

For questions about using Classic.

Post » Wed Jun 01, 2011 6:54 pm

Is it possible in construct to create special scripts in python and pass to one of functions (or classes) in the script one or more of the construct objects as a parameter ? or send a new object from the script to construct ?
B
2
G
1
Posts: 13
Reputation: 476

Post » Thu Jun 02, 2011 12:25 am

[quote="constructnub":2zom5ljq]Is it possible in construct to create special scripts in python and pass to one of functions (or classes) in the script one or more of the construct objects as a parameter ? or send a new object from the script to construct ?[/quote:2zom5ljq]
Do you have an example of what you are trying to do? I'm not sure why you would need to pass Construct objects as parameters to Python functions since you can already access objects in local event sheets or globally if object is Global.

On the flip side, how would Construct be able to operate on a new Python object? I guess I don't fully understand the question.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Thu Jun 02, 2011 10:27 am

The question wasn't why ? I asked is it possible ?
B
2
G
1
Posts: 13
Reputation: 476

Post » Thu Jun 02, 2011 11:43 am

Yes
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Jun 02, 2011 1:15 pm

Thanks
B
2
G
1
Posts: 13
Reputation: 476

Post » Fri Jun 03, 2011 12:10 am

I'm curious more for my knowledge... ;-) Always interested in how people are using Python and if there are more efficient ways to do stuff.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Fri Jun 03, 2011 2:05 am

In my opinion, using Python makes sense whenever complex custom data structures, inheritence or reusable code is needed. For the latter it wouldn't make much sense to "hardcode".

[code:3jh1c5m5]from math import *

class Magnet(object):

def __init__(self, centerX=0, centerY=0, radius=200):
self.center = (centerX, centerY)
self.radius = radius

def lerp(self, a, b, x):
return a + (b - a) * x

def attract(self, construct_object):
a = self.center[0] - construct_object.X
b = self.center[1] - construct_object.Y
c = max(self.radius * 0.5, min(self.radius, sqrt(pow(a, 2) + pow(b, 2))))
f = self.radius / c - 1
construct_object.X = lerp(self.center[0], construct_object.X, f ** System.TimeDelta)
construct_object.Y = lerp(self.center[1], construct_object.Y, f ** System.TimeDelta)[/code:3jh1c5m5]

The script could now be saved, e.g as magnet.py, to ...\Data\Python\ and then be reused in every project with every object that has X and Y properties:

+ Start of layout
-> Script[code:3jh1c5m5]from magnet import *

myMagnet = Magnet(300, 300, 224)[/code:3jh1c5m5]

+ Always
-> Script[code:3jh1c5m5]myMagnet.attract(Sprite)[/code:3jh1c5m5]

Clean and simple :)

(Please don't test if the code is actually working, it was just a quick written example to show the principle)
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat Jun 04, 2011 4:18 am

Very nice example! I'll have to remember this for easy pseudo-plugin type work...
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Mon Jun 06, 2011 7:01 am

I'm trying to use python as more as possible.
Python is cool. For example, "execfile(FILENAME)" can load python script into event sheet, even if the cap file has been exported to exe file.
It means someone can customize the game by change the python script without cap file.
B
97
S
22
G
178
Posts: 4,120
Reputation: 104,047


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: GameOverBeast and 2 guests