Jump to content

Blender 3D: Noob to Pro/Intro-GE-Source

From Wikibooks, open books for an open world

Mission Statement: The purpose of this section is to show a programmer how to make sense of the blender game engine source code. It is not intended for those who cannot code in C++. If you're one of the people who have a basic understanding, I'm being careful to use keywords that if you google, should provide enough information to understand.


When you press P, several things are sent to the GE before anything can be done. What might these be?

StartKetsjiShell(struct ScrArea *area,
		      char* scenename,
		      struct Main* maggie1,
		      struct SpaceIpo *sipo,
		      int always_use_expand_framing)


Blender uses a lot of structs and classes. You need to have a working understanding of these as well as inheritance before we can move on. For a refresher: http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/inheritance.html

The SCA_IObject class source/gameengine/GameLogic/SCA_IObject.cpp

and SCA_IObject.h


This class encapsulates a lot of other things, so we can start here and move downwards to the things that it controls. First off it uses vectors to store all the different Sensors, Controllers and Actuators that are linked to the object. A vector is like an array, but it is dynamic and has built in functions.
We will begin by examining how sensors are added.


typedef std::vector<SCA_ISensor *> SCA_SensorList;
SCA_SensorList m_sensors;

The first line here is a typedef so it just tells us that we can create a vector of SCA_ISensor by using the name SCA_Sensor list.
The second line creates the vector with the name, m_sensors


void SCA_IObject::AddSensor(SCA_ISensor* act)<br />
{   m_sensors.push_back(act);   }

This function allows the parents of an SCA_IObject to add new sensors by calling: SCA_IObject.AddSensor(keyboard); pseudocode

So now you know how Objects in the GE create sensors (and actuators and controllers, its the same :) So lets look at how Sensors are grabbed! Since they are dynamically added the program can't make assumptions about how many are there or what they will be. Thus there is this helper function:

1''SCA_ISensor* SCA_IObject::FindSensor(const STR_String& sensorname)
2{
3	SCA_ISensor* foundsensor = NULL;
4
5	for (SCA_SensorList::iterator its = m_sensors.begin();!(its==m_sensors.end());its++)
6	{
7		if ((*its)->GetName() == sensorname)
8		{<br />
9			foundsensor = (*its);
10			break;
11		}
12	}
13	return foundsensor;
14}''

Put simply, you give this the name of a sensor and it returns a pointer to the sensor.

for the newbies: I know you learned all about classes when i asked you to, but just in case:) the first word, "SCA_ISensor*" this tells us that the function will return a pointer to a SCA_ISensor. And based on the functions arguments, we know that when you call FindSensor you must give it a string between the parenthesis.
in line 3 you create an empty pointer which you will use to store the sensor that you find. Then you loop and go through each sensor in the vector of sensors (remember, it was named m_sensors) until you find the one that matches the name. If you don't find one, you return NULL.


void SCA_IObject::Suspend(void) What this does is it loops through all of the sensors and sends each one a "suspend" call. What this does is change a variable named, "m_suspended" which makes stops the sensors from running when they are called with their Activate() function.