Slider

I was making a slider (parameter control) for a project and i can’t figure out how to set the value of the variable associated with the slider. It’s somewhat like this.

function setup()
    x = 100
    s = Slider(the variable i.e "x", initial value, final value,...)
end

function draw()
    s = Slider:draw()
end

Slider is a class with self.value as a variable which is the value of x to be set. How should I do it?

Lots of ways, here’s one that’s fairly adaptable to other similar circumstances.

In setup:

s = Slider({value = x})

In Slider:init:

function Slider:init(t)
    t = t or {}
    for k,v in pairs(t)
        self[k] = v
    end
end

I didn’t really understand that @Andrey_Stacey. You wrote self[k] = v but self is not a table. What does it mean? And shouldn’t it be self.t = t?

Yes, self is a table. Inside the Slider:init function then it refers to the specific instance of the class that you’ve called it on. When you write s = Slider({value = x}) then you create a new instance of the class Slider and call Slider:init on that instance. Then inside the code for Slider:init you refer to this instance via the alias self. Instances are essentially tables so you can use table methods to manipulate them. In particular, setting values.

In my code above, when you call Slider({value = x}) then the function Slider:init is called with self as the new instance and t as the table {value = x}. We want to transfer that table into self so we copy it across. That’s what the for loop does: for each key,value pair of t we set the same key of self to that value.

Try running the code and putting print(s.value) after the initialisation.

Thanks @Andrew_Stacey now i think im more clear about what classes actually do. So according to your code I’m setting the value of s.value equal to that of x. If i do something like this in the draw function.

function Slider:draw()
    self.value = self.value + 1
end

It will change the value of s.value but not that of x, correct? How should I make it change the value of x through the class, Slider? cause doing this.

function setup()
    x = 100
    s = Slider({value = x})
end

function draw()
    s:draw()
    print(x, s.value)
end

Gives the result 100,101 - 100,102 - …

To change x, you can do _G["x"] = variable

Oh thanks. So_G is table containing global variables right? So if I call something like

x = Walls(20,20)

where Walls is a class which has a variable self.Width then shouldn’t x.Width be a global variable too? If not is there any table which holds these variables?

Any variable that a) belongs to a table, or b) is not declared as “local” will be a global variable. All global variables are held in the _G table. So in your example, the class “Walls” is a global variable, and “x” will be a global variable. The variable “width” in the table “x” is not global, because it belongs to the table “x”. Does that make sense?

Your “a”, point says that any variable in a table is in_G and x is also a table and width is the variable in it. So shouldn’t it be there in_G too?

Ack, sorry, a) should have been “any variable not in a table”. Sorry about that. So “x” is global, but if “width” belongs to “x”, “width” is not global (though you access it through the global “x” when you do “x.width”). Essentially, _G is just another table, that variables go into when they aren’t local (or assigned to another table).

All that aside, I wouldn’t generally view it as good practice for an object to be messing with stuff in the global table. Rather I would say that it is for the program to update the global variable from the object’s information. So you could have, for example,

function Slider:draw()
    -- whatever this does
    return self.value
end

then in the draw function:

    x = s:draw()

@Andrew_Stacey: I completely agree.

Yes. But Slider:draw() , i mean ‘:’ if you want self to be known…

Thanks guys got it working.