Forum
CS2D Scripts What's OOP?What's OOP?
2 replies 1
Examples in Lua:
OOP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Player = {posX = 0, posy = 0} function Player:new (o, posX, posY) o = o or {} setmetatable(o, self) self.__index = self self.posX = posX or 0 self.posY = posY or 0 return o end function Player:move(x, y) self.posX += x self.posY += y end p = Player:new(nil, 0, 1) p:move(2,5)
Non-OOP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function newPlayer(posX, posY) p = { posX = posX, posY = posY, } return p end function movePlayer(p, x, y) p.posX += x p.posY += y end p = newPlayer(0, 1) movePlayer(p, 2, 5)
1