lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]





On Mon, Aug 4, 2014 at 7:34 PM, Littlefield, Tyler <tyler@tysdomain.com> wrote:
On 8/4/2014 12:32 AM, Andrew Starks wrote:


On Sunday, August 3, 2014, Littlefield, Tyler <tyler@tysdomain.com> wrote:
Hello all:
I'm working on this project:
http://github.com/sorressean/aspen
and want to seriously revamp scripting. All of my game-objects are objects, so I'd like to wrap these in Lua and provide inheritance so that I just need to add methods and they can override etc. I have a few ideas and I'm hoping people can help with some solutions:
1) I'd love to have garbage collection actually free up the object.
2) Inheritance: I never deal with multiple inheritance, so that's not an issue.

I'm having a really hard time writing this, however. Are there any examples that show how to pull this off the right way? I'd ultimately be happy with two classes, one of which inherits the other so I can see how best to do that in the API.

I'd greatly appreciate any information/help. TIA,

--
Take care,
Ty
http://tds-solutions.net
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.
 

 Maintaining objects that live in both Lua and C / C++ is hard. At the API intersection, we've had more success by keeping the system language abstractions simple and dumb. We ended up doing extra work to unwind the fanciness from C++, only to remake the-same-but-different abstractions in Lua. 

If you can make your C binding feel more procedural-y and less object-y, then your Lua objects will be extremely simple to make. If you're like me, perhaps the biggest problem is over-doing the object features and the interface. Lua almost begs you to write your own object system and it's hard to know when to stop, until you've done it a few times.

Hello:
Thanks a lot for the info. I'm actually scrapping the current system and trying to rework what I have. Basically right now i have one main issue I'd like to solve:
I have two objects (many more, but this will do): Entity and Player. Entity is any in-game object, it holds name, contents etc. Player is pretty self explanitory. Right now, Entity has methods like GetName, GetLocation etc. Player inherits that, so I need some way of modeling that: allowing a user to call GetName on a player. I planned on using inheritance for that because it frees me from writing the chain of inheritance methods over for every new object. How have people handled this in the past? If there's a cleaner way (short of using classes), I'd love to do it that way. My second issue really was that I could set up these methods and attach them to userdata somehow, but I'd need to somehow model inheritance with those. E.g: is GetName being called on a player? If so, there would be some way to look up that Player inherits entity and that feels really messy to me.

Thanks,


Don't know if that helps. I imagine you're not going to re-write tons of C code. But consider just simplifying things at the binding/intersection between the two. 

-Andrew 


-- 
Take care,
Ty
http://tds-solutions.net
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.

There are some serious experts on this list that might be better able to guess at what is tricky about your problem. I'm not one of those, but I think that perspective is uniquely valuable, as well...

From your explanation, it's difficult for me to know what is difficult for you, other than dealing with the domain sheer that happens between static OOP and Lua.

In Lua, "inheritance", like introspection, is so trivial that it reduces to almost nothing:
```
local entity = {}
---make an entity object here. Have methods that are from lua/C, etc.

local new_player = function(args)
   local self = setmetatable({}, {
      -- if you want to do something more complex than simply forward 
      -- missing fields from self on to entity, then make this a function.
      __index = entity, 
      __newindex = function(t, i, v)
          --catch stuff here, call internals, do slightly different stuff because you're a playa, make magic happen...
      end,
 })

   --do more object stuff.

   return self
end
```

Lua cares not about "types" or "classes". So, multiple inheritance can look as crazy as:

```
local self = setmetatable({},{
    __index = function(t, i)
        return templateA[i] or templateB[i] or templateC[i] -- etc.
    end
})
```

But I think that there may be something more helpful: 

Lua can be a new scripting engine for you. However, consider that eventually, you could begin to use it to actually do the game logic, entities,p layers, ai, etc. C++ is would be where you interface to hardware and do heavy duty calculations. Your C++ code would turn into "C+" code. [1]

That's a big picture view that is most likely impractical. I bring it up because if you live in that world for a bit, you can see how shifting the boundary can make crossing that boundary a simpler problem to solve. Building abstractions on top of abstractions (C++ classes)  is much harder than building abstractions on top of primitives.

And this is where others are better at telling you how to solve *this* problem, because you probably need to ship something, given the code you've got. :) 

I'm just here to tell you that yeah, that is kind of hard. People do it all of the time, but you might want to migrate your project to a more Lua-centric approach, as you have the opportunity to refactor.

-Andrew

[1] I wish I could credit this properly. I met a gentleman that made data transformation software that uses Lua. He wrote an excellent post on his company's migration from Python to Lua. Then he wrote eloquently on the pros of C++ and its cons, most of which he attributed to inexperienced but clever programmers who use too much of the language. It was a great read and if I hind it, I'll post it here.