Page 2 of 2

Re: Cannot load more the one class object

Posted: Mon Jan 12, 2015 12:03 am
by savagedogg38
Foxcraft wrote:Hey savagedogg38,

I think it would be good to point out that you are using the SECS library to create your classes. Always try to give info like that. ;) Also, providing the .love file directly helps people play with it and get at it quicker.

Anyway, from the recent code and playing with it, I see two things wrong that I think changing should fix it up.

1. Remove "badie = {}" from badie.lua. By having that there, you are erasing "badie = class:new()" by telling badie to be something else.
2. "b = badie:init(someX, someY)" is setting b to nil. Make it not be nil and it won't be nil.

Now, you may be lost on how you're setting it to nil. Remember that when you do "x = someFunction()", you are telling x to be what value someFunction gives to it. Your init function is not giving b anything, and nothing is nil in Lua.

What you want to do is give the badie setup to b.

Code: Select all

function badie:init( x, y)
   self.x = x
   self.y = y
   self.dx = 1
   self.dy = 1
   self.alive = true

   return self -- this should do the trick
end
edit: Oh, the code has changed since I downloaded it.... :rofl:
that was all still very helpful. thank you.

Re: Cannot load more the one class object

Posted: Mon Jan 12, 2015 12:07 am
by Foxcraft
Oh, that's right. What I wrote may fix the nil issue, but because badie is being created beforehand, the same badie would be handed out each time. You have to be able to give a new instance each time.

So like bartbes said, as well as what your recent bit of code does, you have to make sure you're creating a new one each time.

Re: Cannot load more the one class object

Posted: Mon Jan 12, 2015 12:16 am
by savagedogg38
Thanks guys - you all gave me a lot a good information to wrap my head around so that's what I'm going to do for a bit. Thanks again. I'll refine my code after I get a grasp of how objects are created, but form what I understand so far, simply assignment a variable a table creates the object? So it is already created when I go to the initialization function? I'm thinking the function makes the object but that seems to be wrong form what I'm hearing. So I have been creating the object and then - well - erasing it... huh... again, a lot of good information here so I'm going to read it about a gazillion more times and play with the code you have given me. thanks. I'll let you know what I find out and what I come up with. thanks again.

Re: Cannot load more the one class object

Posted: Mon Jan 12, 2015 2:12 am
by Foxcraft
I'm still brushing back up in my Lua, but you do start a class setup with a table. But when you try to create something of that class (like b), you have to give it its own fresh, different table. The setmetatable and the __index setting work together to give the new object (b) access to what's in the class's (badie's) own table. The functions, default variables, all that. It's kind of like copying those, but not exactly.

The libraries for making classes try to simplify the setup for you as they can, making what you have to set up on your end work out a bit differently. Still, same idea.

You can read up on it from the free online version of Programming in Lua, in the Object-Oriented Programming chapter. It has a few sections to it, just hit the arrow for the next part of the chapter. It is a lot to wrap your mind around. Take all the time you need. :)

Re: Cannot load more the one class object

Posted: Wed Jan 14, 2015 10:50 am
by pedrosgali
I think Foxcraft has it right, from what I've seen badie:init() is not a constructor.
try a new function called badie:new()

Code: Select all

function baddie:new(x, y)
    local newBad = {}
    setmetatable(newBad, self)
    self.__index = self
    self:init(x, y)
    return newBad
end
Then call it as b = baddie:new(math.random(whatever), math.random(whatever))
This will make a new entity that is unique and properly linked to your class. I hope this helps and happy coding :)

Re: Cannot load more the one class object

Posted: Wed Jan 14, 2015 1:07 pm
by s-ol
pedrosgali wrote:I think Foxcraft has it right, from what I've seen badie:init() is not a constructor.
try a new function called badie:new()

Code: Select all

function baddie:new(x, y)
    local newBad = {}
    setmetatable(newBad, self)
    self.__index = self
    self:init(x, y)
    return newBad
end
Then call it as b = baddie:new(math.random(whatever), math.random(whatever))
This will make a new entity that is unique and properly linked to your class. I hope this helps and happy coding :)
If im not missing anything then it should rather look like this:

Code: Select all

baddie.__index = baddie
function baddie.new(x, y) -- ":"-syntax makes it harder to distinguish between "class" and "object" methods 
    local newBad = {}
    setmetatable(newBad, baddie)
    newBaddie:init(x, y) -- this was an actual error in your code
    return newBad
end

Re: Cannot load more the one class object

Posted: Wed Jan 14, 2015 1:09 pm
by bartbes
As mentioned, they're already using SECS, so there's little reason to not.. actually use it.