Post tutorial RSS Level Editing Scripting by Prospero. Part. 3

Tut3. A bit of boring theory. This tutorial is about Python coding language. Variables and Functions.

Posted by on - Basic Client Side Coding

Tut3. A bit of boring theory.

Variables:
This is a variable:

a

so is this:

iamavariable

A variable can be (almost) any combination of Letters,Numbers or under_scores, up to 256 characters long. The only exception is that they must not be Python Keywords such as:

and / assert / break / class / continue / def / del / elif / else /
except / exec / finally / for / from / global / if / import / in /
is / lambda / not / or / pass / print / raise / return / try /while

Variables 'store' values about things. Assign a value by:

python code:
var1 = 1
var2 = 2

The values can be changed during the course of a program. Which is why they are called variables. They can store all the info about certain things in the game. e.g. When the Player is created, all the
info (or 'attributes') about he/she is usually assigned to the variable

python code:
char


as in

python code:
char= Bladex.Create Entity("Player1",..........etc)


This calls a function in the Blade engine which creates the Player and returns all the default values to the variable char.

the attributes can be reset by:

python code:
char.Life=2000
char.Level=10

and can be acessed and assigned to other variables by:

python code:
charlevel= char.Level
charlife=  char.Life
charpos=char.Position

When objects are created they are usually assigned the much used variable o. As in

python code:
o=Bladex.CreateEntity("Rock1",........etc)


Once the object is created the o variable is used by the next object to be created. Only the last o used will retain the values of the last object to be created. In practice you very rarely need to access an objects attributes during the course of a map, so it is safe to reuse the o.

If you do need to do this, any object/enemy can be re-assigned to a variable by:

python code:
rock1=Bladex.GetEntity("Rock1")


and altered:

python code:
rock1.Scale=2.0


It is nice to make the var names descriptive (orc1, box34, boss1,etc)
as it makes code easier to follow.

Functions:

Functions drive the whole gameplay flow. This is a function:

python code:
def Function1():
    *block of code here*

The code within the function is not executed when the file is loaded. You have to invoke or 'call' the function to execute it. This is done by a number of events in the game: operating a lever, trigger sector, death of enemy, etc

python code:
Function1()


When the function is called, the code within it is executed. Functions are reuseable and can be called as many times as you like.

Values can be passed to a function. e.g.

python code:
def HowHealthyAmI(entity):
    pers=Bladex.GetEntity(entity)
    lifeleft=pers.Life
    return lifeleft

health=HowHealthyAmI("Player1")

This function caller 'asks' the function the current life points of the charcter specified (Player1 in this case). The 'answer' is returned to the caller and assigned to the var health. Get the picture?

Clever bit is, it also works for any character:

python code:
orc1health=HowHealthyAmI("Orc1")
orc2health=HowHealthyAmI("Orc2")

If you try this on something that doesn't have the attribute 'Life', you will get an error.

Functions can pass any number of values (or 'arguments' as they are more correctly known. If a number of arguments are listed in the function, it will expect that many from the caller. You can use default arguments:

python code:
def WhatLevelAmI(entity="Player1"):
    pers=Bladex.GetEntity(entity)
    perslvl=pers.Level
    return perslvl

charlvl=WhatLevelAmI()

Calling this function without arguments returns the Player1 current Level to var charlvl. However if you do this:

python code:
enlvl=WhatLevelAmI("Zombie1")


it will return the Level of Zombie1. See what I mean?

* One small but important point here. Python counts from 0, not 1.
The level returned by the above function will be one less than the actual level of the character. If you need to know the actual Level put:

python code:
enlvl=WhatLevelAmI("Zombie1")+1


If you assign

python code:
char.Level=2


they will actually be at Level 3 in the game

This rule does not always apply, but is worth bearing in mind.

Ok, that's enough boring stuff. I don't intend to go into all the Python theory. This was just to clarify how things work on a basic level.

More practical stuff next.......

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Tutorial
Browse
Tutorials
Share
Related Games
Related Groups
Rebel Act Studios
Rebel Act Studios Developer & Publisher