1. Indentation is important.
In GML an if loop can be all of the following:
if a==0
{
doSomething()
}
if a==0 {doSomething()}
if a=0 doSomething()
etc...
In Python there is mostly 1 valid way:
if a==0:
doSomething()
- '==' is necessary, contrarily to GML.
- How much stuff is in the if loop is determined by the indentation.
With that last sentence I mean:
if a==0:
a = 1# This is inside the if loop
b = 1# This too
print a, b# This isn't
All of this is also valid for while loops.
2. Variables aren't declared.
GML 'var' does not exist in python. Variables are automatically deleted after every script, I think. Except for all the self variables (Read more in point 4).
3. Functions, not scripts.
In Python you can't simply write stuff in a script. You make a function, with 'def':
def Multiplier(a, b):
c = a*b
return c
Inside the brackets you specify the number of arguments as well as the variable they should be put into.
4. Classes in Python are a bit different from objects in GML, but overall very similar:
There are no built-in variables like x or y in a default Python class.
You define such a class by:
class MyClass():
self.x = 0
self.y = 0
def Move(self, newX, newY):# New 'Method'. Function inside a class
self.x = newX
self.y = newY
class1 = MyClass()# Make an Instance of MyClass
a, b = 1, 1
while True:# Infinite while loop, obviously
class1.Move(a, b)# Notice we don't give anything for the "self" variable.
a += 1
b += 1
Classes have a default 'constructor' event, the same thing as the GMK 'Create'. The name of this is '__init__'.
So:
class MyClass():
def __init__(self):
self.x = 0
...
Inheritance of course works. Just put inside of the class brackets the names of the classes you want to inherit from.
class MyClass():
def __init__(self):
self.x = 0
self.y = 0
class MyClass2(MyClass):# Inherit everything from MyClass, in this case the self.x and self.y
def Move(self, newX, newY):# New 'Method'. Function inside a class
self.x = newX
self.y = newY
class1 = MyClass2()# Make an Instance of MyClass
a, b = 1, 1
while True:# Infinite while loop, obviously
class1.Move(a, b)# Notice we don't give anything for the "self" variable.
print class1.x, class1.y
a += 1
b += 1
5. Python has infinitely better lists.
Forget ds_list. Here come real lists.
myList = []
myList[0] = 1
# myList == [1]
myList.append(2)
myList.append("Hello!")
# myList == [1, 2, "Hello!"]
myList.append([4, 5, 6])
# myList == [1, 2, "Hello!", [4, 5, 6]]
for a in range(len(myList)):# Normal syntax of a for loop: "for a in range(start, end, step):" len() == size()
print myList[a]
I think that like covers most of the basics.