Structuring Games without OOP

Hello all,

I am new to Corona and currently laying out my first game.
My background is in ObjectiveC with several native apps on the AppStore.

I am wondering how you guys structure your games internally since LUA doesn’t support OOP.

As a practical example, let’s take an arcade space shooter.
In other languages, I’d create a class for all objects that move across the screen and interact, namely the player, enemies and obstacles.
That class would have the properties “health”, “damage”, etc and the methods to handle them.

Then the classes “Player”, “Enemy” and “Obstacle” would inherit from it and add their specific functionality.
In the end, all events only trigger class methods or create new objects from classes.

While researching on Google, I found that LUA supports inheritance by manipulating the metatable. There are also a few frameworks that slap OOP onto LUA.

However, since LUA is not object oriented by design, I am figuring most games developed in Corona get along without this paradigm.
Thus the question - how do you structure your games internally?

Do events simply call functions that set up the complete entity?
What has worked best for you?

Thank you for taking the time to share your experiences.

–0x90 [import]uid: 10292 topic_id: 3422 reply_id: 303422[/import]

Personally, I am going to follow the MVC model, with soft OOP only when needed.

According to my current paper designs: my display objects know how to draw themselves, keep info about their state and message the controller about significant events that could have an effect on their outer world. The controller handles all the significant events it listens and asks the model about how it should alter the game environment according to the new game state. The model runs my algorithms and return the higher-level gameplay state for the next frame-set (e.g. level is cleared, all lives are lost, increase difficulty, inform about a new highscore etc). Then, the controller updates the HUD, manages scene transitions and cares about dump object creation/removal, all as a follow up of the model return values. Finally, the game returns to a user-input waiting mode, in which my objects know how to be displayed until something significant happens again (aka some environment or user event is triggered).

All these, are still on paper and I am currently experimenting with parts of the code and the infrastructures of the whole mechanism. Most of the functionality is simply modules and functions. Some functions return a table/object that has self-modifying functions injected in it. Currently I don’t need inheritance, as my objects are super-simple and they just use the same event handlers instead of inheriting from a “class”.

OOP was coming very naturally when I was writing similar things in Objective-C. However, I had the feeling that it was somewhat of an overkill for objects with simple functionality. Same things in Lua also come naturally: Everything is simply event-driven and this makes objects being even more minimalistic in code, as their behavior is outsourced to shared event-handlers. Usually, an object is just its texture and its listeners: a dressed up Automaton with an individual set of responses!

But, wait a minute! Isn’t this what *object* oriented programming is meant to be?
Not exactly?
Ok, right: Let’s call it… " Automaton Oriented Programming" - “AOP” :slight_smile:

Well, AOP sounds me as a more appropriate “orientation” for *game* programming!

[import]uid: 7356 topic_id: 3422 reply_id: 10314[/import]

you can implement classes to some extent… see what I’m doing here
http://developer.anscamobile.com/forum/2010/11/05/class-based-display-objects

and read these:
http://lua-users.org/wiki/SimpleLuaClasses
http://class.luaforge.net/

I’m no expert at these things but it seems you can replicate some of the functionality you need.

I’m using a BallActor “class” which includes my display object but not my physics at the moment. It’s the first bit of Lua i’ve done so I don’t know if I’m doing it exactly right but it’s achieving the results i need for now
[import]uid: 6645 topic_id: 3422 reply_id: 10316[/import]

Thank you both for your input.

@Magenda -> I’d love to see some sample code for what you’re doing.

@jmp909 -> Thanks for the links. Will check out the class libraries. It seems you are looking for the same general “hey guys, is this how you do it?” advice I am seeking. Basically, we’d need a more senior corona dev to answer the question

“Do you use OOP through some library in your game projects or create them as function-driven programs?”

It’s really about experience values.
With native Apps and objC, not using strict OOP will approximately double the development effort in my experience (especially when it comes to changing code or debugging).
But the figures might be completely different when developing games in LUA using Corona. [import]uid: 10292 topic_id: 3422 reply_id: 10342[/import]

@Magenda -> I’d love to see some sample code for what you’re doing too also your AOP idea of constructor with listeners in your objects.

I’m wanting to do a similar thing with inheritance so I can create enemies with similar functions with different looks and deatimations etc.
Cheers

Chris [import]uid: 9457 topic_id: 3422 reply_id: 11100[/import]

MiddleClass is meant to be good for inheritance, although google around you’ll find some others
[import]uid: 6645 topic_id: 3422 reply_id: 11114[/import]

0x90 check this article: http://blog.anscamobile.com/2010/04/lua-the-lingua-franca-of-iphone-games/

it mentions a game. you could probably learn a lot looking at the source in that

hint: the iphone IPA file you download is just a zip of the package contents
[import]uid: 6645 topic_id: 3422 reply_id: 21511[/import]

http://developer.anscamobile.com/code/object-oriented-sample-game-framework

EDIT: just noticed this is a necropost [import]uid: 12108 topic_id: 3422 reply_id: 21534[/import]