120 likes | 271 Views
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.
E N D
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 functionBoomUpdate(me) if( GetLifetime(me) == 8) then --Create 8 'Boom' effects in random directions fori = 0,7 do child = CreateObject(me,"Boom1") dir = i * (math.pi * 2 / 8) SetVelocity(child, math.cos(dir),math.sin(dir)) end end end
A Lua function in C++? No wai! 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);
Calling Lua Function in C++ //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);
The Game Loop • General Overview • 2 Main ways to do updates • By system • By object • Both have their advantages and disadvantages • Coupling runtime list creation
Enter Component Objects • What is it? • Variant of Messaging System • Abstract Idea • Implementation left up to programmer • Several articles on topic: • GDMag • Game Programming Gems
Component Object Overview • 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
A Simple Example 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 test int 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"); }
Event Command Calls Lua //subscribers is an std::map<std::string,Subscriber*> //Many, if not all, components derive from Subscriber void Component::Event(std::string eventName) { //Iterate through the list for( std::list<Subscriber*>::iteratori = subscribers[eventName].begin(); i != subscribers[eventName].end(); ++i) { //Call the function lua_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); } }
The Lua Function functionGunnerCollision(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
In Summary • Basically localized messaging system • Can be implemented various ways • Lua method seems to be fairly practical • Questions? Comments?