Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to make function that uses colon operator?

edited February 2017 in Questions Posts: 121

I've never understood how to use the colon operator, but I want to start using it. Edit: I know i have to make a table to hold the methods now, oops. I thought the colon meant that self was passed in from the variable (see m=map()) but ahh I'm not quite understanding what I'm doing.

I want it to be so i can call m=map(), then map.coords = {vec2(x,y)} and so on like m=mesh() but I'm confused on how that is even done in the first place. Umm, thats basically the same as if i did map:init, isn't it? I just want to remove the init method part.

But i noticed if i print(mesh()), it will actually say mesh: (0xwhatever, length=0), whereas with a table it just goes table:whatever (is thats the id?) so I am curious how I can get my own functions to print like that, if it is possible. I really should have waited a few hours before posting this, i haven't given up yet and just end up editing it. Sorry id delete it, if i could figure out how.

-- original post code
function map()
    local t = {coords={},
            tints={},
            radiuses={}}
    return t
end

function map:coord( i, x, y, z )
    if x and y and z then
        self.coords[i] = vec3(x,y,z)
    else
        return self.coords[i]
    end
end

function setup()
        m=map()
        m.coords = {vec3(100,100,1), vec3(200,100,1), vec3(50,50,1)}
        print( m:coord(1) )
end

Here is the second post code. I haven't made much progress, since the method still gives an error I'm not sure what I'm doing wrong

map = {}
function map:init()
    local t = {coords={},
            tints={},
            radiuses={}}
    return t
end

function map:coord( i, x, y, z )
    if x and y and z then
        self.coords[i] = vec3(x,y,z)
    else
        return self.coords[i]
    end
end

function setup()
    m=map:init()
    m.coords = {vec3(100,100,1), vec3(200,100,1), vec3(50,50,1)}
    print(m.coords[1])
    print( m:coord(1) )

end

Comments

  • edited February 2017 Posts: 237

    @xThomas To use the colons, say:

    Map=class()
    
    function Map:init()
          --initial code
    end
    
    function Map:coords()
          --Do your code here
    end
    

    Then in the setup, say:

    function setup()
           m=Map()--This will trigger the init function
           m:coords()--call the coords function
    end
    
  • SimeonSimeon Admin Mod
    Posts: 4,958

    @xThomas the colon operator is shorthand for a function that takes the instance as the first parameter.

    function Map.coords(map, i, x, y, z)
        -- "map" takes the place of "self" here
    end
    
    function Map:coords(i, x, y, z)
        -- Here the "map" object calling this method is passed implicitly
        -- as "self"
    end 
    

    You can handle custom printing for your class by implementing the __tostring method:

    function Map:__tostring()
        print("Map")
    end
    
  • Posts: 121

    @CamelCoder that works. I don't know why or how that works. Knowing that is the main goal this week to learn. (Also not being so dependent on using class for these things, I'd like to learn the underlying syntax making everything work) I've looked at class a couple times but I don't know enough about how simple things like the colon operator or metatables work to know how class() works. maybe I should make a new thread?

    @Simeon Still getting error 'attempt to call a nil value method coord'

    map = {}
    function map.init()
        local t = {coords={},
                tints={},
                radiuses={}}
        return t
    end
    
    function map.coord( self, i, x, y, z )
        if x and y and z then
            self.coords[i] = vec3(x,y,z)
        else
            return self.coords[i]
        end
    end
    
    function map:__tostring()
        print("Map")
    end
    function setup()
        m=map.init()
        m.coords = {vec3(100,100,1), vec3(200,100,1), vec3(50,50,1)}
        print(m.coords[1])
        print( m:coord(1) )
    
    end
    
    
  • @xThomas it doesn't seem like you have much experience with lua. Not that that's a bad thing, you just need to learn. All you have to say is:

    Map=class()
    

    Instead of:

    Map={}
    

    The class() function sets up a meta table which allows the colon operator

Sign In or Register to comment.