tjheldna Posted April 3, 2013 Share Posted April 3, 2013 Hi All, I've been a big C++ guy from the start, but I want to learn how to use lua. Most I've found pretty straight forward however I'm trying to implement classes and inheritance and have hit a wall with inheritance. What I'm trying to do is have an InventoryItem class and a GunItem which inherits. Maybe I'm a little off today, don't know, I just can't seem to understand from examples from the net and I don't know if they work in LE. Does anyone know of a simple tutorial for this? or have an example handy? Cheers!!!! Quote Link to comment Share on other sites More sharing options...
Daimour Posted April 3, 2013 Share Posted April 3, 2013 There are tons of approaches to make inheritance in Lua. First of all you need to decide - "do you really need inheritance?" Or you just need similar behave for group of objects? In Lua you can override properties and methods of objects "on the fly". And you don't need "polymorphism" because Lua has no types in common and you don't have to deal with type-casting. The easiest way (IMO) to create similar-behave objects - to make factory-functions for each type of objects. function CreateInventoryItem(picture, weight) local obj = {} obj.picture = picture obj.weight = weight return obj end function CreateGunItem(picture, weight, damage) local obj = CreateInventoryItem(picture, weight) obj.damage = damage return obj end 2 Quote Link to comment Share on other sites More sharing options...
tjheldna Posted April 3, 2013 Author Share Posted April 3, 2013 That's easy, thanks Daimour! I'll try it soon. Quote Link to comment Share on other sites More sharing options...
Admin Posted April 3, 2013 Share Posted April 3, 2013 You can assign functions to objects just like you can add members. Quote 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.