Cannot load more the one class object

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.
savagedogg38
Prole
Posts: 9
Joined: Sun Jan 11, 2015 1:01 pm

Re: Cannot load more the one class object

Post 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.
User avatar
Foxcraft
Prole
Posts: 49
Joined: Sat Mar 22, 2014 8:56 pm

Re: Cannot load more the one class object

Post 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.
savagedogg38
Prole
Posts: 9
Joined: Sun Jan 11, 2015 1:01 pm

Re: Cannot load more the one class object

Post 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.
User avatar
Foxcraft
Prole
Posts: 49
Joined: Sat Mar 22, 2014 8:56 pm

Re: Cannot load more the one class object

Post 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. :)
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Cannot load more the one class object

Post 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 :)

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Cannot load more the one class object

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Cannot load more the one class object

Post by bartbes »

As mentioned, they're already using SECS, so there's little reason to not.. actually use it.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest