Defining GUI control event responses using Lua in C++

BarakatX2
  • Defining GUI control event responses using Lua in C++ BarakatX2

    I'm using Irrlicht(C++) for a 3D engine, and it has a built in GUI system. The GUI events are something like this:

    switch(eventtype)
    case button_pressed:
        stuff
    case editbox_edited:
        stuff

    So far I have begun writing a system that allows me to create the GUIs with Lua scripts. It was very easy to make it so it creates the GUI controls from Lua, but I can not think of a good way to define what should happen when control events occur from Lua too. Here's an example of what I have so far:

    Lua:

    local button = create_button("button label",10,10,100,50)
    toggle_control(button,false)
    

    C++:

    static int CreateButton(lua_State* L)
    {
     int n = lua_gettop(L);
     core::stringw label = lua_tostring(L,1);
     core::stringw tt((n>=5?lua_tostring(L,6):""));
     int x = lua_tonumber(L,2);
     int y = lua_tonumber(L,3);
     int w = lua_tonumber(L,4);
     int h = lua_tonumber(L,5);
     core::rect r(x,y,x+w,y+h);
     unsigned int id = NextID();
     if(r.isValid())
     {
      gui::IGUIButton* b = device->getGUIEnvironment()->addButton(r,0,0,label.c_str(),tt.c_str());
      cl.insert(ControlPair(id,b));
      lua_pushnumber(L,id);
     }
     else
      lua_pushnumber(L,-1);
     return 1;
    }

    Please ignore the security issues with the code (it is a very early version). So my question is this: What are some good ways of defining the control events from a script interpreter in C++?

  • I'm not familiar with Irrlicht, but you could pass a function to the CreateButton that should get called on an event that deals with that button. You could either pass a string, and do a lookup in the global table for a function with that name at run time, or store a pointer to the lua function itself (which should allow you to do lambdas).

    In the past I've done a convention-over-configuation thing, where the game automatically picks up lua functions that fit a callback style. Eg:

    createButton("myButton", "label text", 10, 10, 100, 50)
    
    ...
    
    function onEvent_myButton(eventParams)
        -- process event
    end
    

    The game parses the global table, looking for onEvent functions and parsing the function name to see if it matches your new button name. It hooks that up as a callback.

    For some reason our designers found this a more friendly approach.

Tags
c++ gui lua
Related questions and answers
  • from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've...; for(char i= '0'; i!= ' ';swall++) i= buff[buff.size()-1][swall+1]; int sspace= 0; int I= swall+1; for(char i= '0'; i!= 'X';sspace++, I++) i= buff[buff.size()-1][I...{ // cwall is negative swall--; cwall++;}} for(int w= 0; w <swall; w++) buff[buff.size()-1].push_back('X'); // cspace

  • language, in my case Lua. On the other hand I want to use a component based design for my entities, and here starts my questions: 1) Should I define my componentes in C++? If I do this in C++ won't I...) Where should I place the messaging system for my game engine? Lua? C++? I'm tempted to just have C++ object to behave as servers, offering services to lua business logic. Things like physics system... them in lua. Now I have clear I'm going to write them in C++ and expose everything needed. Ussually only interfaces are exposed to lua, so I have come across that to export properties to be published

  • areas of concern I have and wanted to get some opinions on the proper way others have solved them or would solve them. 1. Cyclical Dependencies When I was doing the game in C#, I didn't really have...I've been working on a 2d RPG for awhile now, and I've come to realize I've made some bad design decisions. Theres a few things in particular that are causing me problems, so I was wondering what... ago, decided to switch to C++. I wanted to get a good handle on C++ since it's been awhile since I used it heavily, and figured an interesting project like this would be a good motivator. I've been

  • ;format,0xFF, 0xFF, 0xFF) ); ... Button button(m_screen,0,0,50,50,255,0,0) ... ... Button::Button(SDL_Surface* screen,int x,int y,int w,int h,int R, int G, int B) { SDL_Rect box; SDL_Surface...; box.y = y; box.w = w; box.h = h; ButtonSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, box.w,box.h, 32, rmask, gmask, bmask, amask); if(ButtonSurface == NULL) { LOG_MSG("Button::Button Button failed"); } SDL_FillRect(screen,&box,SDL_MapRGB ( ButtonSurface->format, R, G, B )); //ut.ApplySurface

  • I am trying to create a 2D platformer (Mario-type) game and I am some having some issues with handling collisions properly. I am writing this game in C++, using SDL for input, image loading, font..., so that's worth explaining. coords[0] represents the x position (left) of the object (where it starts on the x axis). coords[1] represents the y position (top) of the object (where it starts... I need to start with. What I need is an easy way to both detect which side the collision occurred on and handle the collisions properly. So, basically, if the player collides with the top

  • I'm writing a game engine which is going very fine. However, I'm now posed with handling textures. My Engine is in 2D, for simplicity reasons mostly to get a good idea of how to work with OpenGL. I do this in C++, and I have a hierarchical set-up when it comes to classes. There's a base class and some other classes derive from that. So now I'm onto the part where I want to load up some textures... have to use glGenTextures to allocate a texture and that I then swap betwe--.. I actually have no clue. What's a good way to keep track of all the textures you loaded into the memory and switch

  • I am quite new to OpenGL, I have managed after long trial and error to integrate Nehe's Cel-Shading rendering with my Model loaders, and have them drawn using the Toon shade and outline...? Something unnecessary that maybe you guys could spot? Both MD2 and 3DS loader have an InitToon() function called upon creation to load the shader initToon(){ int i... (1, &shaderTexture[0]); // Get A Free Texture ID ( NEW ) glBindTexture (GL_TEXTURE_1D, shaderTexture[0]); // Bind This Texture. From Now On It Will Be 1D

  • i think i just found the solution. 1) the problem is that backbuffer surface and source surface are of different formats - that is why exception was thrown. 2) the image path needed double slash "C\\......." ;) ================================= I have been googling for a while but still cant come up with a solution to the problem - D3DXGetImageInfoFromFile function just doesn't load any images...(D3D_SDK_VERSION))) return false; std::wstring wsPath = L"C:\wood.bmp"; // path to the image D3DXIMAGE_INFO Info; if (FAILED(D3DXGetImageInfoFromFile(wsPath.c_str(), &Info

  • My engine uses a component-based entity system internally, and I want to bind it to Lua for scripting. Now, I want to save people who write scripts for it typing work. In C++, to set the position of an entity, you'd do the following: pEntity->GetComponent< CPoint >()->SetPos( Vector( X, Y ) ); That means, if I'd bind it to Lua 1:1 you'd also have to: ent:GetComponent( CP_POINT ):SetPos( 123, 456 ) But let's be honest, would you want to type so much just to set an entities' position? I don't think so, that's why I "hid" the component system from Lua: Right now, what you do

Data information