Creating a Class with persistant values between all instances.

I’ve managed to stump myself. I’m trying to find a way to set a value in a class that persists and can be updated by any instance of the class. I can do this if write my own version of class but I’d like to figure out how to do this from within Codea’s classes.

exemple of what I’m trying to do.

FooBar = class()
local instanceCount = 0

function FooBar:init()
instanceCount = instanceCount + 1
end

function FooBar:getCount()
print(instanceCount)
end

--This is in Main setup()
foo = FooBar()
bar = FooBar()

FooBar:getCount()
-> want it to print 2

`.

I use the feature quite often. Here is how i do it:

FooBar = class()

FooBar.instanceCount = 0

function FooBar:init()
self.instanceCount = self.instanceCount + 1
end

function FooBar:getCount()
print(self.instanceCount)
end

--This is in Main setup()
foo = FooBar()
bar = FooBar()

FooBar:getCount()
-> want it to print 2

I havent verifyed. I use it usually with more complex things like tables. Does it work with single value too? Mmmm… It should, i think.

i think is not working @Jmv38 . not make the count

I had tried that but can’t seem to get it working.

Sorry you are right. But this work:

FooBar = class()

FooBar.globals = {}
FooBar.globals.instanceCount = 0

function FooBar:init()
self.globals.instanceCount = self.globals.instanceCount + 1
end

function FooBar:getCount()
print(self.globals.instanceCount)
end

function setup()
--This is in Main setup()
foo = FooBar()
bar = FooBar()

FooBar:getCount()
end
FooBar = class()

FooBar.instanceCount = 0

function FooBar:init()
FooBar.instanceCount = FooBar.instanceCount + 1
end

function FooBar:getCount()
print(FooBar.instanceCount)
end

--This is in Main setup()
foo = FooBar()
bar = FooBar()

FooBar:getCount()
-> want it to print 2

I was wrong because i use it usually with tables, not simple vars

@briarfox?

now is working :slight_smile:

Or you could use a local variable:

cl = class()

local var = 1

function cl:init()
end

function cl:setvar(v)
    var = v
end

function cl:getvar()
    return var
end

I think it’s meant more as a counting var to see how much objects of a class exist…

So your code’s fine, but for the class itself, i think it’s better to use ‘self’
and in the init the adding, so what briarfox has posted should work :wink:

@Andrew_Stacey you probably have the fastest solution => best one.

@Jmv38 @Andrew_Stacey @stevon8ter @Luismi Am I missing something with the question. @Briarfox had a local variable in his original post. Here’s his example that does what he asked for. There are 2 instances of FooBar created, and that’s what he wanted to print.


function setup()
    foo = FooBar()
    bar = FooBar()
    
    foo:getCount()
    bar:getCount()
end

FooBar = class()
    local instanceCount = 0

function FooBar:init()
    instanceCount = instanceCount + 1
end

function FooBar:getCount()
    print(instanceCount)
end

Odd, I couldn’t get local to work.

I just used count for the example. I’m trying to creat a table called Scene(“Name”) that loads up all scenes for a project and allows the user to set the currently viewable scene. I got tired of recreating scenes or appstates on every project. Trying to create a more elegant solution.

@Briarfox I’m not sure why it didn’t work. Even when I added FooBar:getCount() to my example an ran it, that also printed 2.

Well then I typed the example out from what i wanted to do, I’m guessing I messed something up in the code I was working on :stuck_out_tongue: Oh well, thank you all :slight_smile: