0 Favourites

Python - A quick look

This forum is currently in read-only mode.
  • Okay so first off what is python:

    'Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive. Its use of indentation for block delimiters is unusual among popular programming languages.' - wikipedia

    Okay so basically, its a programming language, which means that you gotta type stuff.

    First, insert a Sprite onto the screen. Then switch to the event sheet editor. To insert a 'script' block, right click and select 'insert script'

    <img src="http://dl.dropbox.com/u/939828/Tutorial/Script2.PNG">

    Lets start with a really simple script

    <img src="http://dl.dropbox.com/u/939828/Tutorial/Script.PNG">

    Sprite.X += 1[/code:2dnrrrmr]
    
    When you run it, that sprite will move to the right 1 pixel per frame. Basically this script block of python is run each frame, just like events are. In fact, you can place a python script anywhere in the event sheet just like a regular event (ie. You can have a python script as a sub event)
    
    Switch back to the layout editor, hold control and make a second instance of 'Sprite'. In fact, make a couple (press enter as you drag instead of constantly dragging and dropping) and now run the application.
    
    You will notice only the first instance moves, none of the others do.
    
    Python only evaluates one instance (in this case, the first).
    
    If you want to move other instances, you need to refer to them in an array notation. Eg:
    
    [code:2dnrrrmr]Sprite[0].X += 1;
    Sprite[1].X += 1;[/code:2dnrrrmr]
    
    Or...and this is a better way...take advantage of python for loop:
    
    [code:2dnrrrmr]for s in Sprite:
    	s.X += 1[/code:2dnrrrmr]
    
    This means it will loop each instance of Sprite, and move it along the x axis by 1 unit per frame.
  • Right so we've had a quick look at how to get something up and running in python. Now lets take a close look at the python language itself.

    awesome = 10[/code:3o8cahs5]
    This declares the variable 'awesome' as 10
    
    [code:3o8cahs5]face = "hillarious"[/code:3o8cahs5]
    This declares the variable 'face' as a string 'hillarious'
    
    [code:3o8cahs5]
    variable = 5
    if variable == 5:
    	Text.text = "cool"
    [/code:3o8cahs5]
    If you were to run this code, you would need to put a text object on the layout called 'Text'. But basically, if variable == 5 means:
    
    [i]If the variable is equal to 5, run the code which is indented below'[/i]
    
    Important things to notice is the indentation, the fact each 'instruction' is on a separate line, and that the if statement has a : at the end... also take note that python uses two equal signs for 'is equal to' like java and C++.
    
    [code:3o8cahs5]for n in [1,2,3,4]:
    	ListBox.Add(n)[/code:3o8cahs5]
    To make this code work, you need to have a list box on the screen. Basically, for loops always loop over something. In C++ you might be familiar with writing: [i]for( int x = 0; x < 10; x++)[/i]...but python instead loops over all the elements in a set.
    
    If you need to loop all the numbers from 0 to 100, its a waste of time to write [0,1,2,3,4,5.... so theres a handy function to remember - range:
    
    [code:3o8cahs5]for n in range(0,10,2):
    	ListBox.Add(n)[/code:3o8cahs5]
    
    This will output 0, 2, 4, 6, 8 ... but not 10...so in C++ its the equivilent of writing:
    
    [i]for (int n = 0; n < 10; n+= 2)[/i]
    
    However, the python version is a bit shorter and probably easier to read! However it does take a bit of getting used to.
  • Lets now take a look at how Construct handles multiple blocks of script.

    Lets say we create two scripts, one that sets a variable, and one that adds that variable to our ListBox:

    <img src="http://dl.dropbox.com/u/939828/Tutorial/Script3.PNG">

    awesome = "Scirra"[/code:3tbzidv8]
    [code:3tbzidv8]ListBox.Add( awesome )[/code:3tbzidv8]
    Right now what we'll notice happens is that the List box adds the line 'Scirra' when you press the button. This might seem pretty obvious, but for clarification, variables declared inside a script block are 'global' in that they are available to other scripts as well. This doesn't just include variables, it also includes classes and functions.
    
    Okay lets have a look at how to declare a function!
    
    <img src="http://dl.dropbox.com/u/939828/Tutorial/Script4.PNG">
    
    [code:3tbzidv8]def MyFunction(param):
    	return param + " without my pants"[/code:3tbzidv8]
    Basically what we have done is declared a function 'MyFunction' which takes a single parameter called 'param'. Note that we dont declare the type of variable the function takes. It then returns the combination of whatever is in 'param' and the string " without my pants"
    
    [code:3tbzidv8]ListBox.Add( MyFunction("scirra") )[/code:3tbzidv8]
    Here we add a line to the list box. Instead of adding "scirra" or a variable, we call the function MyFunction("scirra"), telling python that we want to evalulate 'MyFunction' with param = "scirra". The result is it adds "scirra without my pants" to the list box
  • Okay a quick thing to know about functions. Any variable that is declared within a function can only be used in the function. eg:

    <img src="http://dl.dropbox.com/u/939828/Tutorial/Script5.PNG">

    This script wont work and you'll get an error about an undeclared variable. This is due to a common thing in programming languages called 'variable scope'.

    Well this is all I can be bothered writing for now

  • thanks for this tutorial, i was looking for some python tutorials here....hope enough users bother you to write more ...

  • Fantastic information. It is much appreciated.. please keep it coming!!

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Having been using Python for years, I didn't think that this thread was going to be of any interest to me, but showing specifically how Construct handles Python script blocks is very useful.

    This is going to be useful for many other people here who probably are fluent in Python, but haven't really looked at it because of the uncertainty of how well it works with Construct.

    I look forward to anything else "Pythoners" (!?) should know.

    BTW, does this mean Python is 100% bug-free in Construct now?

    Krush.

  • to use python with picking:

    http://www.scirra.com/forum/viewtopic.php?f=2&t=5305&p=42100#p42100

    it doesn't take much effort to learn python, either, especially if you know c++, or one of the major nonscripting languages.

    A few days ago I only knew enough python to be able to test the pyfix plugin., now I'm using almost exclusively python triggered by conditions, and I've grown completely comfortable with python. almost anything you need to know is thoroughly explained by the first google search result on it.

    being able to write real functions, to consolidate complicated series of actions, or create expressions, which can then be used in other expressions, enables you to exponentially increase complexity and readability.

    Also, being able to simply copy and paste my code to new caps in invaluable.

    for the more advanced users, especially, python is definitely worth learning

    combined with 's', I was able to rewrite by keyframe based editor I had originally been developing a plugin specifically for. I've gotten further along in less time. And this new version is much more robust and powerful. It's like the speed of event based creation, with the power of c++ plugin development

  • And now a cool chain/snake example for python

    http://dl.dropbox.com/u/939828/Python%20Chain.cap

    Tomorrow I will explain how it works.

  • That's awesome Davo

  • And now a cool chain/snake example for python

    http://dl.dropbox.com/u/939828/Python%20Chain.cap

    Tomorrow I will explain how it works.

    Welp, I guess I do need to install Python to run scripts after all...?

    An error occured in python, but construct is unable to obtain any error information without StringIO.pyc[/code:nfi8wwdi]
    
    Um, also "occurred" is misspelled in that error, and Python and Construct should be capitalized.  There's errors in your error so you can error while you error
  • that is very awesome indeed davo

  • Atleast you don't get error from error after having error which has errors? Anyways it's cool example and tbh I think I really need to start looking into Python the code is just so short and clean compared to same thing done in events (atleast for me xD)

  • Any way to use Sprite[1] with regular events?

    Its sooo much easier to pick from multiple instances... when you have them indexed like that.

  • Okay lucid convinced me to install Python and give learning it another go. After reading Davo's post I was able to move just three instances in the snake .cap with a loop I made:

    for n in [8, 12, 16]:
    	Body[n].X += 1[/code:28n06wce]
    
    It only took me four tries to get it right without errors!  A proud boy am I!  
    
    I still don't see how this is going to be useful to me personally yet since I don't relly make anything all that complex, but I'm willing to read more Python tuts if anyone wants to make them.
    
    PS that snake thing is pretty neat, David.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)