• Advertisement
Sign in to follow this  

Unity UI design with Lua

This topic is 4402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been using Lua lately, and I've found it to be pretty nice. I got the idea of designing the UI for my game in Lua - I would write some widget classes in C++, and use a script file to specify where on the screen they go. For example, right now I can do something like:
createTextLabel(frameName, "New Game", 0.5, 0.35, 1)
createTextLabel(frameName, "Load Game", 0.5, 0.45, 1)
createTextLabel(frameName, "Options", 0.5, 0.55, 1)
createTextLabel(frameName, "Credits", 0.5, 0.65, 1)
createTextLabel(frameName, "Quit", 0.5, 0.75, 1)
Where the two floats after the string are the relative X and Y positions, and the 1 is a flag saying that the text label should be centered at that X coordinate, as opposed to being left aligned. However, I seem to have run into a problem. It's nice that I can specify the layout of my widgets without having to recompile, but I can't figure out a clean way of specifying arbitrary behavior when a widget gets clicked. For example, if you click on any of the text labels, (text labels kind of double as buttons here) then you will get switched to another screen, or quit the game. When you are in the options menu, there will be some radio buttons and listboxes, and it will have an "OK" and a "Cancel" label, which will of course save changes and leave or discard changes and leave. In the UI for my previous game, (which I pretty much just hacked up at the last minute before our showcase) I wrote a bunch of "WidgetAction" classes, which just had a virtual function called "activate" that got called when the action's containing widget got clicked. Since C++ does not have anonymous classes like Java, I ended up with a few (not too many) classes for seemingly arbitrary actions, such as "ConnectToServerAction", "StartSinglePlayerAction". The entire UI was hard-coded, with all of the widgets placed and their actions bound in some setup function. Here are some options that I considered taking. Could someone please make some suggestions as to which way is best? 1) As an extra argument for all createXXXXWidget glue functions, specify an event that gets raised when the widget gets clicked. This seems simple, but the problem is that I would probably have to write a bunch of subclasses for events, and perhaps have them all be able to be constructed at runtime by a string containing their constructor arguments. Then again, I will probably be making a lot of event subclasses for other purposes anyway. 2) Instead of an event type as the extra argument, specify an anonymous Lua function that serves as a callback for when the widget gets clicked. To be honest, I'm not really sure how this would work out in terms of argument passing. Also, the contents of the anonymous functions would probably be more glue function calls, which means more glue functions would have to be written. 3) Write some WidgetAction classes, and add glue functions to allow constructing them from the script file. This doesn't seem too horrible I guess, although I would like some more flexibility in specifying behavior. 4) Write WidgetAction classes, and hard-code the entire UI, including screen placement. I'd rather not, but it does seem the simplest. (And statically typed, but whatever.) I guess there isn't necessarily a magic bullet for this problem, but I would appreciate some advice so that I can at least make an informed decision. Thanks! --EDIT-- Damn, I should really read the other posts in this forum. This looks like a good approach: http://www.gamedev.net/community/forums/topic.asp?topic_id=366651

Share this post


Link to post
Share on other sites
Advertisement
The problem seems to be that your internal actions are not properly exposed to Lua.

Could you, today, write a Lua script which does whatever needs to be done in response to a button, but manually?

If not, then you need to beef up the interface between your application and Lua, in some way.

If you can, then all you need to do is pass a Lua function as argument to the create function, and have the button instance call it when activating the button.

Share this post


Link to post
Share on other sites
Quote:

The problem seems to be that your internal actions are not properly exposed to Lua.

Could you, today, write a Lua script which does whatever needs to be done in response to a button, but manually?

If not, then you need to beef up the interface between your application and Lua, in some way.

If you can, then all you need to do is pass a Lua function as argument to the create function, and have the button instance call it when activating the button.


So do you mean that I should just add more glue functions, i.e. some functions to create WidgetActions? Just wanted to make sure.

Share this post


Link to post
Share on other sites
In my project (also using Lua, though I use tolua++ to generate the glue functions), I have a Signal/Slot mechanism to handle widget events. For instance, if I want to activate, say, a new screen when the user presses a button I do something like this:
 
Cpp
---

class MyScreen: public Screen {
public:
Slot slot_activate;

MyScreen() {
slot_activate.set_callback(&MyScreen::onActivate);
}

void onActivate() {
// activate screen
}

};

Lua
---

-- Connect event to my_screen class
my_button.signal_activate:connectTo(my_screen.slot_activate);

-- Connect event to lua function
my_button.signal_activate:connectLuaFunction("FunctionName");


You can read about signals and slots here: http://www.codeproject.com/cpp/ElmueSignalsandSlots.asp

This is just how I do it, there are many other ways. Hope this gives you some ideas.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Now

  • Advertisement
  • Similar Content

    • By Chamferbox
      Chamferbox, a mini game asset store has just opened with some nice game assets, 
      Here you can find a free greek statue asset 

      Also check their dragon, zombie dragon and scorpion monster out:



      They're running the Grand Opening Sale, it's 30% off for all items, but for gamedev member, you can use this coupon code:
      GRANDOPEN
      to get 50% off prices What are you waiting for, go to
      http://chamferbox.com
      and get those models now!

      View full story
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064

      View full story
    • By DevdogUnity

      Ho ho ho
      Tis the season of Christmas surprises, and we have a awesome one for you! 🎅  
      Sponsored by all your favorite Unity Asset Store developers, Nordic Game Jam, Pocket Gamer Connects, and co-hosted by Game Analytics, we (Joris and I – Devdog) are launching the second edition of our yearly Christmas Giveaway Calendar for all Unity game developers!
      You can already now sign up right here.
       
      So what’s this all about?
      For the past weeks, we’ve been collecting sponsored gifts related to Unity (asset vouchers, product keys, conference tickets etc.), and throughout each day of December leading up to Christmas Day on the 25th, we will be sending out these sponsored gifts as early gamedev Christmas presents via e-mail to hundreds of lucky winners.
      The total prize pool is at $35,000, with over 1200 presents donated by the awesome sponsors!
       
      Merry Christmas from Devdog, Game Analytics, and every single one of the sponsors.

      View full story
    • By sveta_itseez3D
      itSeez3D, a leading developer of mobile 3d scanning software, announced today a new SDK for its automatic 3D avatar generation technology, Avatar SDK for Unity. The Avatar SDK for Unity is a robust plug-n-play toolset which enables developers and creatives to integrate realistic user-generated 3D avatars into their Unity-based applications. SDK users can allow players to create their own avatars in the application or integrate the SDK into their own production processes for character design and animation.
      “Virtual avatars have recently become increasingly popular, especially in sports games and social VR apps. With the advance of VR and AR, the demand to get humans into the digital world is only increasing”, said Victor Erukhimov, itSeez3D CEO. “Our new Avatar SDK for Unity makes it super-easy to bring the avatar technology into any Unity-based game or VR/AR experience. With the Avatar SDK for Unity now every developer can bring face scanning technology into their games and allow players to create their own personalized in-game avatars, making the gameplay much more exciting and immersive.”
      Key features of the Avatar SDK for Unity:
      Automatic generation of a color 3D face model from a single selfie photo in 5-10 seconds (!). Works best with selfies, but can be used with any portrait photo.
      Shape and texture of the head model are unique for each person, synthesized with a deep learning algorithm crafted by computer vision experts
      Head models support runtime blendshape facial animations (45 different expressions)
      Generated 3D heads include eyes, mouth, and teeth
      Algorithms synthesize 3D meshes in mid-poly resolution, ~12k vertices, and ~24k triangles
      Six predefined hairstyles with hair-recoloring feature (many more available on request)
      Avatar generation API can be used in design-time and in run-time, which means you can allow users to create their own avatars in your game
      Cloud version is cross-platform, and offline version currently works on PCs with 64-bit Windows (support for more platforms is coming soon)
      Well-documented samples showcasing the functionality.
       
      Availability
      The Avatar SDK for Unity is offered in two modes - “Cloud” and “Offline”. The “Cloud” version is available at http://avatarsdk.com/ and the “Offline” version is available by request at support@itseez3d.com.
      ###
      About itSeez3D
      At itSeez3D, we are working on the computer vision technology that turns mobile devices into powerful 3D scanners. itSeez3D has developed the world's first mobile 3D scanning application that allows to create high-resolution photorealistic 3D models of people's' faces, bodies and objects. The application is available for iOS and Windows OS mobile devices powered with 3D cameras. In 2016 the company introduced Avatar SDK that creates a realistic 3D model of a face from a single selfie photo. To learn more about itSeez3D scanning software and 3D avatar creation technology, please visit www.itseez3d.com and www.avatarsdk.com.

      View full story
    • By khawk
      Unity has posted the Unity Austin 2017 playlist on YouTube. From their tweet:
      View the full playlist at https://www.youtube.com/playlist?list=PLX2vGYjWbI0TI_C4qouDw7MSSTFhKJ6uS or below:
      .

      View full story
  • Advertisement