Jump to content

[Solved] To Classify or Not in LUA


Roland
 Share

Recommended Posts

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

AV MX Linux

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

AV MX Linux

Link to comment
Share on other sites

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

  • Upvote 1
Link to comment
Share on other sites

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.

  • Upvote 2

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

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.

  • Upvote 1
Link to comment
Share on other sites

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 :)

  • Upvote 2
Link to comment
Share on other sites

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.

  • Upvote 1

AV MX Linux

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...