1. This forum is ARCHIVED! Visit the new Cloud Sixteen forums, codename Eden, at https://eden.cloudsixteen.com. These forums will remain archived for a few months before being closed down, so try to encourage plugin developers to begin moving their content across to the new forums.
Dismiss Notice
Hi Guest, you need a Steam account to register and post on these forums. Login with Steam at the top of the forums to get started!
Dismiss Notice
Hi Guest, do you want to buy HL2RP or another Clockwork schema? Visit the Cloud Sixteen Store to get started!

Create object within a function?

Discussion in 'Help' started by SomeSortOfDuck, Apr 4, 2013.

  1. I'm a little new to OOP in Lua and I'm having trouble creating an object within a function. Outside of a function I can do it easy as pie.

    Code:
    Duck = Player
    But when I try to do it in a function, it doesn't seem to work. In the Create method, it tries to make a new Player object (I tried id = self instead of id = Player as well). It doesn't work, and I get Duck is a nil value when I try to use the print method. I'm doing all of this to just test things, I know I don't need an object and whatnot to print, but I'm trying to get better in OOP.


    Code:
    Player = {
    	Create = function(self, id)
    		id = Player
    	end,
    	Print = function(self, tp)
    		print(tp)
    	end;
    }
    
    Player:Create("Duck")
    Duck:Print("Test")
    The code below works, but what if I want to make an object that isn't called 'Duck'?

    Code:
    Player = {
    	Create = function(self)
    		Duck = self
    	end,
    	Print = function(self, tp)
    		print(tp)
    	end;
    }
    
    Player:Create()
    Duck:Print("Test")
    What would be the solution to this? Or is there a better way? Google hasn't been of much help :(

    EDIT: I think it might be cause I'm using a string instead of a table @[email protected]
     
  2. alexgrist

    alexgrist Guest

    Take a look at how Clockworks meta tables work in the Command library :)
     
  3. Woooo

    Thanks a ton, got it working ;D. Decided to scrap this project and work on a different one.

    Code:
    Test = Game.player:New("Duck")
    print(Test:GetName())
    Output
    Code:
    Duck
    Really pointed me in the right direction :D
     

Previous Readers (Total: 0)