lua and the game loop
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 PresentationTRANSCRIPT
![Page 1: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/1.jpg)
GAM 250 Summer ‘07 Lecture 2
Disclaimer Pishclaimer
![Page 2: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/2.jpg)
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
![Page 3: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/3.jpg)
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
![Page 4: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/4.jpg)
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);
![Page 5: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/5.jpg)
//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);
![Page 6: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/6.jpg)
General Overview
2 Main ways to do updates By system By object Both have their advantages and
disadvantages Coupling runtime list creation
![Page 7: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/7.jpg)
What is it? Variant of Messaging System Abstract Idea
Implementation left up to programmer Several articles on topic:
GDMag Game Programming Gems
![Page 8: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/8.jpg)
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
![Page 9: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/9.jpg)
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");
}
![Page 10: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/10.jpg)
//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);}
}
![Page 11: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/11.jpg)
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
![Page 12: LUA and the game loop](https://reader035.vdocuments.site/reader035/viewer/2022062519/568150f3550346895dbf0d41/html5/thumbnails/12.jpg)
Basically localized messaging system Can be implemented various ways
Lua method seems to be fairly practical
Questions? Comments?