1 / 12

LUA and the game loop

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.

malory
Télécharger la présentation

LUA and the game loop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LUA and the game loop GAM 250 Summer ‘07 Lecture 2 Disclaimer Pishclaimer

  2. 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

  3. 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

  4. 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);

  5. 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);

  6. 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

  7. 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

  8. 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

  9. 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"); }

  10. 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); } }

  11. 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

  12. In Summary • Basically localized messaging system • Can be implemented various ways • Lua method seems to be fairly practical • Questions? Comments?

More Related