Roland Posted August 24, 2015 Share Posted August 24, 2015 Coming from the pure C++ world I have a question for you who used LUA for a while.In C++ I'm quite used to make classes of most things to keep things apart. Now I wonder if this approach will hold in LUA also considering speed. Or in an other way. Is there high speed penalties for accessing external class methods in Lua compared to simple functions or is the difference negligible Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Skrakle Posted August 25, 2015 Share Posted August 25, 2015 It's one of the main reasons why i went from lua to c++ given the complexity of my game. I have so many classes and structs that for me, it would have been a nightmare to do all this in lua. As for speed penalties, i don't know, i never did any benchmark tests but i'm guessing that there might be. Quote Link to comment Share on other sites More sharing options...
Roland Posted August 25, 2015 Author Share Posted August 25, 2015 LUA is kind of nice while developing as you so quickly can change things in code and see the result. But as you say I'm a bit worried about making complex class structure in LUA. I know its possible but really not designed for. I might do it this way then. First make the code part I'm working on in LUA and then move it over to C++ ( Code conversion from LUA to C++ is no big deal ). In fact you could still have the editor variables in LUA and quite easy access them from C++. I will make some test with such a system where a C++ class uses the editor LUA variables and all other things are in C++. Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Lunarovich Posted August 25, 2015 Share Posted August 25, 2015 In fact, according to this chapter in PIL, Lua is designed explicitely to cater for classes creation. The point is, Lua gives you the tools to create classes and lets you design the class system to your likings. Situation is somewhat similar as in JS (as far as I know prototype-based languages). You could try this excellent Lua class implementation. Of course, there are other implementations as well... 1 Quote Link to comment Share on other sites More sharing options...
Roland Posted August 25, 2015 Author Share Posted August 25, 2015 Thanks @Lunarovich for the link. That seems like a nice thing. Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Lunarovich Posted August 25, 2015 Share Posted August 25, 2015 You're welcome! Just for the record, in order to "require 'middleclass'", as specified in docs/tutorial on github, you'll have to uncheck "lua sandbox" in the options. 1 Quote Link to comment Share on other sites More sharing options...
Josh Posted August 25, 2015 Share Posted August 25, 2015 The way I see it, each script is like a class, but the classes can all relate to each other without requiring a hierearchical structure. Some classes may have a TakeDamage() functions, others may not. You just check to see if a function exists and call it if it does. In Leadwerks 2 we implemented a funny derived class system in Lua, trying to emulate C++, and I definitely would not do that again. 2 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Roland Posted August 25, 2015 Author Share Posted August 25, 2015 Yes Josh. I'm getting into it now. Script "class" is a really nice thing to deal with. Cudos on that. Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Genebris Posted August 25, 2015 Share Posted August 25, 2015 I only made a small class once when I wanted to have the same RPG functions such as TakeDamage and IncreaseSkill the same for NPCs and player. So I created object of this class inside NPC script and Player script. But other than that it's just like Josh said, script is a class and each model on the scene with this script is an object of this class. 1 Quote Link to comment Share on other sites More sharing options...
Lunarovich Posted August 25, 2015 Share Posted August 25, 2015 An object script as a class concept does alleviate a need to use classes extensively. What I like about LE is the fact that, unlike Unity for example, you can have a script which is totally unrelated to any game object. What is more, you can "import" or "require" it from Main.lua script or from any object script. That said and if you keep in mind that in Lua functions are first-class citizens, you can hava a simple table of useful functions and dynamically assign them or remove them from entities via "self.entity.func_name = global_table_name.func_name" or "self.entity.func_name = nil". That's quite a duck typing, kind a strange to c++ only users, but it's a great technique for game development, IMO. In this way you can have a "multi-class" entity or make entity dynamically change class. Really neat, if you ask me 2 Quote Link to comment Share on other sites More sharing options...
Roland Posted August 25, 2015 Author Share Posted August 25, 2015 Thanks for your comments Lunarovich. Yeah.. I'm getting into it now and like what i see. Have now coded a Player (class) which can pick up Pickable (class) items and handle them.The items picked up is subclasses of the Pickable (class) and handles the action taken on pickup them self. This way I can avoid tons of 'IF' statements and a terrible mess in my Player to get things done with the objects he touches, My conclusion so far is that LUA is a very nice way to approach the game development. Again. Thanks all for your comments. I now how and when to use classes in LUA. This is great and I will continue using LUA as far as possible. 1 Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.