Hello, i think that’s called OOP, so… Need help with that. I asked this question bc i am either bad at googling or no one asked about something similiar on this forum
A little bit of backstory: I am actually trying to replicate something I used to make in javascript. Here is a code snippet in javascript
const eventsContainer = new Object
eventsContainer.event1 = {
firstValue: 5,
secondValue: 35,
execute: function(){
return this.firstValue + this.secondValue
}
};
console.log(eventsContainer.event1.execute()); // 40
eventsContainer is basically a dictionary (speaking in lua language) with dictionaries, and this is a reference, basically a context of where this function located (or not really… that’s not the point). For example, if you just return this…
const eventsContainer = new Object
eventsContainer.event1 = {
firstValue: 5,
secondValue: 35,
execute: function(){
return this
}
};
console.log(eventsContainer.event1.execute()); // { firstValue: 5, secondValue: 35, execute: [Function: execute] }
… it returns an object of there this function located
I am planning to have something like this:
const eventsContainer = new Object
eventsContainer.event1 = {...};
eventsContainer.event2 = {...};
eventsContainer.event3 = {...};
// And this list can go on and on
Small and efficient. Love it!
Now the problem is i don’t know how to replicate it in lua! The closest I got in lua is:
local eventsContainer = {}
eventsContainer.event1 = {
firstValue = 5,
secondValue = 35,
execute = function()
return eventsContainer.event1.firstValue + eventsContainer.event1.secondValue
end
}
print(eventsContainer.event1:execute()) -- 40
But that’s not the solution. What if there are tens of these objects, i don’t want to type eventsContainer.eventX in every single function for every single ‘event’. Is there any way to get a key or something that can tell where this function is located so instead of typing eventsContainer.eventX I could type this or something. Parsing a value to a function is also not prefered (unless it’s intented by lua)
Thanks in advance! That’s a lot of text, sorry…