Noob at classes: how to instantiate?

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.
Post Reply
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Noob at classes: how to instantiate?

Post by togFox »

I'm not instantiating my class correctly:

Code: Select all

inputperceptron = {weight = {}  -- there will be one weight per hidden p
                }

function inputperceptron:new(o)
    o = o or {}
    setmetatable(o,self)
    self.__index = self

    -- set weight[1] and [2] and [3] to random values for this perceptron
    self.weight[1] = math.random(1,10) 
    self.weight[2] = math.random(1,10) 
    self.weight[3] = math.random(1,10) 
    return o
end

p1 = inputperceptron:new()
p2 = inputperceptron:new()

print(p1.weight[1],p1.weight[2],p1.weight[3])
print(p2.weight[1],p2.weight[2],p2.weight[3])
print(p1.weight[1],p1.weight[2],p1.weight[3])
p1.weight[1] = p2.weight[2] but they should be random.

p2 is overwriting p1 meaning I'm instantiating a single instance of .weight when I should be instantiating a new instance of .weight with each inputperceptron. :( How to fix? I'm sure it's a simple syntax thing.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Noob at classes: how to instantiate?

Post by MrFariator »

This is a simple mishap with the 'self' keyword.

Code: Select all

inputperceptron = {}
inputperceptron.__index = inputperceptron

function inputperceptron:new(o)
    o = o or {}
    setmetatable(o,self)
   
    o.weight = {}
    -- set weight[1] and [2] and [3] to random values for this perceptron
    o.weight[1] = math.random(1,10) 
    o.weight[2] = math.random(1,10) 
    o.weight[3] = math.random(1,10) 
    return o
end
Or alternatively (if you prefer to use the 'self' keyword for the object you're instantiating)

Code: Select all

inputperceptron = {}
inputperceptron.__index = inputperceptron

function inputperceptron:new(o)
    local self = o or {}
    setmetatable(self,inputperceptron)

    self.weight = {}
    -- set weight[1] and [2] and [3] to random values for this perceptron
    self.weight[1] = math.random(1,10) 
    self.weight[2] = math.random(1,10) 
    self.weight[3] = math.random(1,10) 
    return self
end
In your original code, you were basically using the 'self' keyword to modify the class itself, rather than the class instance you were creating. So in the end all objects you would get with :new() had a reference to the exact same 'weight' table.
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Noob at classes: how to instantiate?

Post by togFox »

Thanks. I'd have never worked that out. My grasp of lua classes needs work!
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Noob at classes: how to instantiate?

Post by 4vZEROv »

Also keep in mind that there is no class nor intance in Lua, everything are tables and data inside.
Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests