Forum

> > CS2D > Scripts > What's OOP?
Forums overviewCS2D overview Scripts overviewLog in to reply

English What's OOP?

2 replies
To the start Previous 1 Next To the start

old What's OOP?

The Dark Shadow
User Off Offline

Quote
Yo, What's OOP? Can you show me an example to compare the difference between non-OOP and OOP?

old Re: What's OOP?

ohaz
User Off Offline

Quote
Object Orientend Programming (OOP) is a programming paradigm where (almost) everything is handled as if it were an object that you can interact with. It's a huge thing, so it would be better to read books about it.

Examples in Lua:

OOP:
1
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
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)
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview