Is it possible to create complete scripts in python ?

This forum is currently in read-only mode.
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • 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 ?

  • 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 ?

    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.

  • The question wasn't why ? I asked is it possible ?

  • Yes

  • Thanks

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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".

    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)
  • Very nice example! I'll have to remember this for easy pseudo-plugin type work...

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)