OOP

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Davïd
Prole
Posts: 12
Joined: Sun Jan 29, 2017 4:12 pm

OOP

Post by Davïd »

Hello,

I'm totaly new to Lua love/2D and I did like some explanation about the following code.

Here is a first class :

Code: Select all

local AtlasScheme = {}

--[[
Atlas constructor
@return object Atlas
]]

function AtlasScheme:new()
    
    self.__index = self
    
    return setmetatable(
        {
            mapsByName = {} -- Map object by name @var table{string name, object Map}
        },
        self
    )
    
end

--[[
Add map to mapsByName
@param string name
@param object Map
]]

function AtlasScheme:addMap(name, map)
    
    self.mapsByName[name] = map

end

--[[
Link map together
@param ... string arg (mapName) 
]]

function AtlasScheme:linkMap(...)

end

--[[
@return AtlasScheme
]]

return AtlasScheme
And another one :

Code: Select all

local Atlas = {}

--[[
Atlas constructor
@param object atlasScheme
@return object Atlas
]]

function Atlas:new(atlasScheme)
    
    self.__index = self   
    
    return setmetatable(
        {
            mapsByName = atlasScheme.mapsByName -- Map object by name @var table{string name, object Map}
        },
        self
    )
    
end

--[[
Load map
@param string name 
@return object Map
]]

function Atlas:loadMap(name)
    
    local Inspect = require('libraries.inspect.inspect')
    print(Inspect(self.mapsByName))
    
    self.mapsByName[name]:load()

end

--[[
Get map
@param string name 
@return object Map
]]

function Atlas:getMap(name)
    
    return mapsByName[name]

end

--[[
@return Atlas
]]

return Atlas
And I'm trying to do is to pass an atlasScheme instance to atlas as as dependency :

Code: Select all


local Atlas = require('classes.data.Atlas')
local AtlasScheme = require('classes.data.AtlasScheme')
local Map = require('classes.data.Map')

-- dependencies

local atlasScheme = AtlasScheme:new()
local atlas = Atlas

 -- Create atlas scheme    
        
    atlasScheme:addMap('Village', Map:new('truc'))
    atlasScheme:addMap('Forest', Map:new('bidule') )
    atlasScheme:addMap('Mine', Map:new('chouette') )
    atlasScheme:linkMap('village', 'Forest')
    
    --  Create map from scheme
    
    atlas:new(atlasScheme) 

And it return an error "attempt to index field 'mapsByName (a nil value)'

I figured out that the issue is here :

Code: Select all

local atlasScheme = AtlasScheme:new()
-- local atlas = Atlas -- nope
local atlas = Atlas:new() -- instead would work
But I don't get the difference between atlas = Atlas:new() and atlas = Atlas + atlas:new()

I apologize for my French skills in english and thank's for any enlightenment. :awesome:
Last edited by Davïd on Mon Aug 14, 2017 3:09 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: POO

Post by zorg »

Hi and welcome to the forums!

From the code i read, i don't think that's where your issue is; you define :loadMap in atlasScheme, but attempt to use :addMap, which would produce that kind of an error. Is there code you're not sharing or may that be the mistake? :3

(Also i'm pretty sure you wanted the thread title to be Object-Oriented Programming, or "OOP"; but it's fine like this too.) :awesome:
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Davïd
Prole
Posts: 12
Joined: Sun Jan 29, 2017 4:12 pm

Re: OOP

Post by Davïd »

Lol, I changed topic name. POO Is french acronym but is sound like shit here right :awesome:

Here's my code (last line was missing)

Code: Select all

    
    atlasScheme:addMap('Village', Map:new('truc'))
    atlasScheme:addMap('Forest', Map:new('bidule') )
    atlasScheme:addMap('Mine', Map:new('chouette') )
    atlasScheme:linkMap('village', 'Forest')
    
    --  Create map from scheme
    
    atlas:new(atlasScheme)    
    
    -- Load map
    
    atlas:loadMap('Village') -- Return error
    


Don't think there is an error here ?
Post Reply

Who is online

Users browsing this forum: No registered users and 76 guests