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
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
@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
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.