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:
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
as in
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:
char.Level=10
and can be acessed and assigned to other variables by:
charlife= char.Life
charpos=char.Position
When objects are created they are usually assigned the much used variable o. As in
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:
and altered:
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:
*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
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.
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:
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:
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:
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:
If you assign
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.......