Karuzo

[OOP] attempt to get length of self

Recommended Posts

Hey Guys,

I'm on OOP atm and i have a problem that mta can't get the length of self.apps, it says it's a nil value.

This is the code:

local x, y = guiGetScreenSize() 
local w,h = 400,600 
local px, py = x/2-w/2,y/1.5-h/1.5 
  
phone = {} 
phone.__index = phone; 
  
function phone:new() 
    local data = {} 
    data.apps = { -- {string name, int line, int row, string appIcon} 
        {"placeholder", 0, 0, nil} 
    } 
    data.open = false 
    data.keyToOpen = "F2" 
    data.keyState = "down" 
    data.network = "Freeroam" 
    data.batterycharge = 100 
    data.curApp = "main" 
    data.background = nil 
    data.renderTarget = dxCreateRenderTarget(w, h, true) 
    data.maxApps = 12 
    setmetatable(data, self) 
    addEventHandler("onClientRender",root,data.render) 
    return data 
end 
  
function phone:render() 
    --render 
end 
  
function phone:newApp(name, icon) 
    if #self.apps < device.maxApps+1 then 
        local line, row = self:getFreeAppPosition() 
        table.insert(self.apps, {name, line, row, icon}) 
    end 
end 
  
phone:newApp("Settings", nil) 
  
  
function phone:getFreeAppPosition() 
    for index, value in pairs ( self.apps ) do  
        if index == #self.apps then 
            local name, line, row, icon = unpack(self.apps[index]) 
            if line < 4 then 
                return ( (line or 0) + 1 ), ( (row or 0) ) 
            elseif line == 4 then 
                return ( 1 ), ( (row or 1) + 1 ) 
            else 
                return false 
            end 
        end 
    end 
end 
  
phone:new() 

Share this post


Link to post

It's quite obvious, heh.

function phone:new() 
    local data = {} 
    --blahblahblah 
    return data 
end 
--blahblah 
phone:new() 

"data" isn't processed anywhere outside of that function scope if you do it like that. Consider deleting the "local" prefix inside of that function block, or adding it to the last line, like:

local data = phone:new() 

or, let that table exist outside of any block, so its scope is limited to the whole script file.

Share this post


Link to post

Just wondering, is OOP really necessary for MTA?

Are there any big advantages?

Share this post


Link to post
Just wondering, is OOP really necessary for MTA?

Are there any big advantages?

This is a very basic look at what oop is, you sould research a little about this topic. It is a very powerful tool to have.

Share this post


Link to post

Alright, but I was more thinking about actual advantage for MTA.

Everything so far was possible without this, and I don't think anyone is now rewriting his scripts.

So its most useful for new projects.

But since MTA's community isn't really growing, actually losing more and more people, I'm not sure if this was actually necessary.

Share this post


Link to post

I'm creating a new gamemode for my server right now, and i'm planning on doing everything in oop. It is definitely useful to me. You should learn a couple of other programming languages to see the benefits of oop, i recommend you to try Python, it is very easy to learn and it is object oriented.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.