LUA and the game loop

Preview:

DESCRIPTION

LUA and the game loop. GAM 250 Summer ‘07 Lecture 2. Disclaimer Pishclaimer. Lua. What is it? Scripting Language Built (natively) for C/C++ Why do we care? Externalizes game code – No compiling A part of “Data Driven” code Easy code integration. A Lua Function. - PowerPoint PPT Presentation

Citation preview

GAM 250 Summer ‘07 Lecture 2

Disclaimer Pishclaimer

What is it? Scripting Language Built (natively) for C/C++

Why do we care? Externalizes game code – No compiling

A part of “Data Driven” code Easy code integration

function BoomUpdate(me)

if( GetLifetime(me) == 8)

then

--Create 8 'Boom' effects in random directions

for i = 0,7

do

child = CreateObject(me,"Boom1")

dir = i * (math.pi * 2 / 8)

SetVelocity(child,

math.cos(dir),math.sin(dir))

end

end

end

int GetLifetimeC(lua_State *L)

{

Object * sprite = reinterpret_cast<Object *> (lua_tointeger(L,1));

lua_pushinteger(L,sprite->lifetime);

return 1;

}

//Elsewhere, in a registering function

lua_register(L, "GetLifetime", GetLifetimeC);

//Within an update function…

// Grab the function name ('BoomUpdate')

lua_getglobal(L, (sprite.name + "Update").c_str());

if( lua_isfunction(L, -1))

{

lua_pushinteger(L,reinterpret_cast<int>(&sprite));

lua_pcall(L, 1, 0, 0);

}

else

lua_pop(L,1);

General Overview

2 Main ways to do updates By system By object Both have their advantages and

disadvantages Coupling runtime list creation

What is it? Variant of Messaging System Abstract Idea

Implementation left up to programmer Several articles on topic:

GDMag Game Programming Gems

Game uses “Components” Objects Attach, or Subscribe, to

Component Updates/Messages All information retrieved from Components

Everything might be a component Objects are themselves components Most fundamental idea uses raw data (void *)

Needs more OO We’ll leverage Lua to handle messages

void TestObjectCollision(PhysicsComponent &me, PhysicsComponent &you){

//Do some testing... Such as (take note)://This works quite well and is fast!Vectori vel = me.velocity + you.velocity;

//First part of X testint xDist = (you.bbox.x1 + you.pos.x) - (me.bbox.x2 + me.pos.x);if(xDist > vel.x)

return;//Continue test for three other conditions

//If all fail, there's a collision. Update component or send collision message//You store the information in the components (might wanna add time of collision,// point of collision, etc)me.collider = &you;you.collider = &me;me.Event("Collision");you.Event("Collision");

}

//subscribers is an std::map<std::string,Subscriber*>//Many, if not all, components derive from Subscribervoid Component::Event(std::string eventName){

//Iterate through the listfor( std::list<Subscriber*>::iterator i = subscribers[eventName].begin();

i != subscribers[eventName].end(); ++i){

//Call the functionlua_getglobal(L, (i->name + "Collision").c_str());if( lua_isfunction(L, -1)){

//Push the object and the component onto the function parameters

lua_pushinteger(L,reinterpret_cast<int>(*i));lua_pushinteger(L,reinterpret_cast<int>(this);lua_pcall(L, 2, 0, 0);

}else

lua_pop(L,1);}

}

function GunnerCollision(me, component)

--Just take damage like normal

you = GetCObject(component)

power = GetPower(you)

--Subtracts health based on power and armor.

--Also detects damage vs healing, etc.

TakeDamage(me, power)

end

Basically localized messaging system Can be implemented various ways

Lua method seems to be fairly practical

Questions? Comments?

Recommended