Architecture Question

Discussion about using Moai SDK - post questions, issues, and ideas here.

Moderators: naturally, seebs, ezraanderson

Architecture Question

Postby christian » Sat Jun 29, 2013 12:19 pm

Hi guys,

i am new to MOAI and start learning the API and LUA scripting..
I am into making business simulation games and nearly finished my next Project: Oil Imperium!
The UI of my games are mostly static, the business logic is the bigger part (NPC AI, Rules, ... )
I just have three minigames in the game, that i realized with Core Animation..

I coded for IOS and only used Objective-C until now. But i don't want to remain on IOS only - so i choose
MOAI as cross-platform framework.
Switching to MOAI needs a different approach to create bigger apps than using the OOP Style in Objetive-C..

I want to invest the time to rewrite my current game, because the code isn't very clean and
i want to offer Oil Imperium on other platforms.

Can you help me with an advice, how to start? Is a MVC architecture useful?
Do you have probably any experiences to share?

cheers
christian
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby btatarov » Sat Jun 29, 2013 12:31 pm

I adopt the MVC pattern in my game programming but it's more a matter of choice than a mandatory thing. You can read about OOP in Lua and choose a way to design your class hierarchy. Libraries like lua midleclass may be useful as a start. I use the Flower framework form makotok and it has a really nice class defining implementation. Good luck!
Unwind... rewind...
User avatar
btatarov
 
Posts: 206
Joined: Thu Sep 27, 2012 4:14 pm

Re: Architecture Question

Postby christian » Sat Jun 29, 2013 1:13 pm

Thank you for the flowers ;)
I'll try the MVC with OOP way. Good to hear that it's just a matter of choice.
Let's see if i am really getting quicker finishing a game with such a framework..
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby rhoster » Sat Jun 29, 2013 5:03 pm

Hey Christian,

I wrote a post a while ago that has a template for creating an objected-oriented, inheritance style implementation. Hope it helps:

lua-package-implementation-template-t1847/
User avatar
rhoster
 
Posts: 242
Joined: Sat May 26, 2012 3:59 pm
Location: San Francisco, CA

Re: Architecture Question

Postby christian » Sun Jun 30, 2013 10:49 am

Hi rhoster,

this guide is cool!
This is the kind of advice a user of the moai project needs!

thank you
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby rhoster » Sun Jun 30, 2013 5:24 pm

No problem! :) Glad to be of assistance.
User avatar
rhoster
 
Posts: 242
Joined: Sat May 26, 2012 3:59 pm
Location: San Francisco, CA

Re: Architecture Question

Postby JDance » Mon Jul 01, 2013 12:18 am

I think this wiki entry is pretty wise on the subject: http://getmoai.com/wiki/index.php?title ... ame_in_Lua

I use classes in my game to contain things, but it's less OOP than obj-C, Java. Don't create a complex class hierarchy with inheritance since you don't need that in a dynamic language like lua.

Say you have a scenemanager that you tell "SceneManager.pushScene(scene)". In a OOP language that scene would need to inherit some base class. But in lua you can just throw in anything that has the functions/fields needed for the SceneManager to do it's job. Like if it has a function scene.onShow then SceneManager can call that when the scene is added, but if it doesnt then it doesnt get called.

Like:
Code: Select all
  1. function C:_setActive(scene)

  2.         self.topScene = scene

  3.  

  4.         if self.topScene.onAdd then

  5.                 self.topScene:onAdd()

  6.         end

  7. end



Hmm and I usually name "private" functions with an underscore like _setActive in the example above. That way I know they arent used outside the current class.
User avatar
JDance
 
Posts: 462
Joined: Mon Oct 08, 2012 12:11 am
Location: http://skyturns.se

Re: Architecture Question

Postby christian » Mon Jul 01, 2013 4:29 am

Hi JDance, i read the article and tried to code a little like that.
It's a bigger changeover for me to think in such a flat and componentized architecture.
Anyway - this i the way its ment to code using moai and lua, so i learn to use it..
I think it's worth the time regarding the expectations of moai benefits and using flower is allready a great help
and reference creating modules.

Thank you for the example and i'll keep asking ;)

christian
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby JDance » Mon Jul 01, 2013 5:27 am

christian wrote:Hi JDance, i read the article and tried to code a little like that.
It's a bigger changeover for me to think in such a flat and componentized architecture.
Anyway - this i the way its ment to code using moai and lua, so i learn to use it..
I think it's worth the time regarding the expectations of moai benefits and using flower is allready a great help
and reference creating modules.

Thank you for the example and i'll keep asking ;)

christian


Code as you like and learn by doing! I've restructured my game many times while learning lua!

This is a good and very short intro to all of luas features: http://tylerneylon.com/a/learn-lua/
User avatar
JDance
 
Posts: 462
Joined: Mon Oct 08, 2012 12:11 am
Location: http://skyturns.se

Re: Architecture Question

Postby christian » Mon Jul 01, 2013 12:55 pm

Thx, i needed such a tutorial too :)
Great support here!

This is my approach to define the data structure and let data
access from the Logic modules:

module company.lua
Code: Select all
  1. module(..., package.seeall)

  2.  

  3. --globals

  4. NUMBER_OF_COMPANIES = 4

  5. FOO = 1

  6. BAR = 2

  7. SUPER = 3

  8. EXTRA = 4

  9.  

  10. -- variables

  11. local data = {}

  12.  

  13.  

  14. -- init company

  15.   data[FOO] = { name = "Foo" }

  16.   data[BAR] = { name = "Bar" }

  17.   data[SUPER] = { name = "Super" }

  18.   data[EXTRA] = { name = "Extra" }

  19.  

  20. -- functions

  21. function get_company(number)   

  22.         if data[number] then

  23.               return data[number]

  24.         end

  25. end

  26.  



module TestView.lua
Code: Select all
  1. module(..., package.seeall)

  2.  

  3. local companies = require "company"

  4.  

  5. local company = companies.get_company(companies.BAR)

  6. print("company called " .. company.name)



What is the approach to express dependencies?
Like:
There are a few companies, each contains an office, that can be choosen by the player.
So the office is not fix at data initialization..
In Ojective-C, i created a strong property with an Instance of Office, that lives in the Company instance..
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby JDance » Mon Jul 01, 2013 11:57 pm

Hmm well you don't really need modules or anything like that

Code: Select all
  1. local companies = {} -- list

  2.  

  3. companies["foo"] = {

  4.  name = "foo",

  5.  owner = "happycamper"

  6. }

  7. companies["bar"] = {

  8.  name = "bar",

  9.  owner = "randomduded"

  10. }

  11.  

  12. local company = companies.foo

  13. company.office = {

  14.  printerCount = 500

  15. }



A data structure in lua is really simple!

If you want to work object oriented with methods on your objects then you could set up companies and offices to be used like classes, like the description of classes in my previous link.

Edit: It's a bit frustrating with the lack of rules in LUA. It has taken like 8 months for my code style to even out a bit. But I still change it up as I find new things. I have restructured and made changes to my class system many times now :)
User avatar
JDance
 
Posts: 462
Joined: Mon Oct 08, 2012 12:11 am
Location: http://skyturns.se

Re: Architecture Question

Postby ibisum » Tue Jul 02, 2013 1:34 am

I think anyone coming to Lua to use it as a traditional language can do that. But! You can also use it, Lua-style and get good results too - i.e. really just use Tables for everything and forget all the OOP you think you need (you don't!). ;)
;
--
Email: ibisum@gmail.com
IRC: torpor on FreeNode, see you in the #moai channel
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info/
User avatar
ibisum
 
Posts: 1501
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Architecture Question

Postby christian » Tue Jul 02, 2013 11:56 am

I see.
I'll try every advice i get here!
No i try to quit thinking in the OOP way :)

My example was just the getter and setter approach i normally use for data access.
If i understand you correct, i put all my data into one lua file and create dependencies like you proposed:

dependency of office to company:
Code: Select all
  1. local company = companies.foo

  2. company.office = { printerCount = 500 }



correct?
This will get a huge class due to a lot of data in a business simulation game..

How will i go on?
Now i need another lua file to code the rules for the gameplay like:

Among others i need a bank data pool, that belongs to a player (a player is also a little data pool with name etc.)
The bank has e.g. money or invested capital (that must be stored separately each month).
When the player, does an investment,
- i do have to check if there is enough money,
- if yes i raise the investment data for the current month.
Using the OOP way i created a Bank class, containing the data and provide with functions like:
[code=objective-c]
-(BOOL)do_investment:(int)money {
...
if(self.money > money) {
self.money -= money;
self.investement[MAY] += money;
}
...
}
[/code]

Would you create logic modules now, that access the big data pool lua file with loadfile(...) e.g.
and provide a similar function or is there a better way, doing it the lua style..

christian
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby JDance » Wed Jul 03, 2013 2:07 am

Dont't have all your data in one file, you can split things up any way you like. I just wanted to illustrate you don't need classes or get/set to create a datastructure in lua. It's all just tables. You can definitely create a Bank class, I would do that. Create a class for Company etc as well, and then do something like

Main file
Code: Select all
  1. local Company = require("company") -- in file company.lua you have some kind of class/module structure

  2.  

  3. local companies = {}

  4. companies.foo = Company:new("foo") -- different class systems have different ways of creating new objects

  5.  

  6. companies.foo:installPrinters(500) -- call a method on your created company



company.lua
Code: Select all
  1. local C = {}

  2. function C:new(companyName)

  3.  local obj = { name = companyName }

  4.  self.__index = self                    

  5.  return setmetatable(newObj, self)

  6. end

  7.  

  8. function C:installPrinters(printerCount)

  9.  self.printers = {}

  10.  ....

  11. end

  12.  

  13. return C -- returns the class to require

User avatar
JDance
 
Posts: 462
Joined: Mon Oct 08, 2012 12:11 am
Location: http://skyturns.se

Re: Architecture Question

Postby christian » Wed Jul 03, 2013 4:28 am

I see, tank you again :)
So you would say: keep creating classes (called modules here) and create new instances when needed - the OOP Style.
I am incredible interested if this IS the Lua Style like ibisum mentioned, or is this an approach to use lua
with a coding style that is similiar to languages like C++, C# or Objective-C?
My intend is to use Lua in combination with MOAI and/or flower framwork from the beginning on like its ment to use.
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany

Re: Architecture Question

Postby rhoster » Wed Jul 03, 2013 9:29 pm

I would say that while you don't have to - I have found great benefit in being able to have a package like oop system and it has create a good code pattern. The reality is that lua lets you do things however you want. If you prefer doing it in an object like fashion, there isn't anything wrong with that and it can help with the organization. If you want to ditch it, you can do that as well.
User avatar
rhoster
 
Posts: 242
Joined: Sat May 26, 2012 3:59 pm
Location: San Francisco, CA

Re: Architecture Question

Postby ibisum » Thu Jul 04, 2013 3:01 am

I'll do it as quickly as I can in simple Lua style:

Code: Select all
  1.  

  2. -- usually I get things like this from a utility.lua, but I'll put it here so you can refer to it without googling:

  3. function deepcopy(orig)

  4.         local orig_type = type(orig)

  5.         local copy

  6.  

  7.         if orig_type == 'table' then

  8.                 copy = {}

  9.                 for orig_key, orig_value in next, orig, nil do

  10.                         copy[deepcopy(orig_key)] = deepcopy(orig_value)

  11.                 end

  12.                 setmetatable(copy, deepcopy(getmetatable(orig)))

  13.         else -- number, string, boolean, etc

  14.                 copy = orig

  15.         end

  16.  

  17.         return copy

  18. end

  19.  

  20. -- This is how all companies should operate at the start of business..

  21. local ideal_company = { printers=1, cash=6,

  22.         invest=function(self, investor)

  23.                 self.cash = self.cash + investor.investment

  24.                 investor.moneybags = investor.moneybags - investor.investment

  25.                 print("\t\tka-chaing! " .. investor.name)

  26.         end,

  27.         dividend = function(self, investor)

  28.                 local payback = investor.investment * 10

  29.                 self.cash = self.cash - payback

  30.                 investor.moneybags = investor.moneybags + payback

  31.                 print("\t\twaaah! " .. investor.name)

  32.         end,

  33.         buyprinter = function(self)

  34.                 -- buy a printer

  35.                 self.cash = self.cash - 1

  36.                 -- and some ink

  37.                 self.cash = self.cash - 6

  38.                 self.printers = self.printers + 1

  39.                 print("\tPCLOADLETTER")

  40.         end

  41.         }

  42.  

  43. local investors ={ { name="Harry", investment=1, moneybags=6 }, { name="Harrier", investment=2, moneybags=24} }

  44. local companies = {}

  45.  

  46. -- I leave the reasoning for deepcopy for you to explore later ..

  47. companies["ABC Ltd"] = deepcopy(ideal_company)

  48. companies["Hairs&Nuts Inc."] = deepcopy(ideal_company)

  49.  

  50. -- lets see how we use it for 2 years

  51. for month = 1, 24 do

  52.         print("Month: ", month)

  53.         for i,iv in pairs(investors) do

  54.                 print(" Investor: " .. iv.name .. " moneybags: " .. iv.moneybags)

  55.                 for c,cv in pairs(companies) do

  56.  

  57.                         print (" Company " .. c )

  58.                         if iv.moneybags <= iv.investment then

  59.                                 cv:dividend(iv)

  60.                         else

  61.                                 cv:invest(iv)

  62.                         end

  63.                         if (cv.cash > ideal_company.cash) then

  64.                                 cv:buyprinter()

  65.                         end

  66.                         print ("  => $", cv.cash, "  ", cv.printers, " printers ..")

  67.                 end

  68.                 print("==")

  69.         end

  70.  

  71.         print("")

  72. end

  73.  

  74.  

;
--
Email: ibisum@gmail.com
IRC: torpor on FreeNode, see you in the #moai channel
Got a MOAI snippet? Please consider adding it to http://moaisnippets.info/
User avatar
ibisum
 
Posts: 1501
Joined: Mon Oct 17, 2011 1:11 am
Location: Vienna, Austria

Re: Architecture Question

Postby christian » Sun Jul 07, 2013 5:10 am

Ok guys,
now i have an idea what is possible and how it's meant to use..

thx a lot for your time!

christian
User avatar
christian
 
Posts: 8
Joined: Sat Jun 29, 2013 11:56 am
Location: germany


Return to Moai SDK

Who is online

Users browsing this forum: MSNbot Media and 0 guests

x