Function in a dictionary, how do you get values from a dictionary in that function?

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…

I don’t know what those scripts mean in javascript but it sounds like you might need recursion?
Example:

function searchNestedDicts(dict, targetKey, targetVal)
	for key, value in dict do
		if key == targetKey and value == targetVal then
			return dict
		end
		if typeof(value) == table then
			searchNestedDicts(value, targetKey, targetVal)
		end
	end
end

No.

I need something like this

local eventsContainer = {}
eventsContainer.event1 = {
	firstValue = 5,
	secondValue = 35,
	execute = function()
		return this.firstValue + this.secondValue
	end
}

print(eventsContainer.event1:execute()) -- 40

so I can toss that function to whatever i want, and it will work bc of this, as it basically tells you what is the ‘parent’ of that function. I don’t know how to explain…

What i am looking for is a global variable or something that allows me to get firstValue and secondValue without knowing the names of dictionaries and stuff

I’m not sure it’s possible, but you can try the following:

local eventsContainer = {}
eventsContainer.event1 = {
	firstValue = 5,
	secondValue = 35,
	execute = function(self)
		return self.firstValue + self.secondValue
	end
}

print(eventsContainer.event1:execute()) -- 40

Taking advantage of : which calls the function and passes its containing table as the first argument always. In this case, it is self.

1 Like

Yes, it is what i wanted. Thank you!
I actually tried using self, thought it was working just as i expected in javascript, but got an error…

Anyways, big thanks

Btw do you really need to use self? It appears to be you can use anything you want

EDIT: I originally misunderstood MrSuperKrut’s question. Yes, you can use whatever name you want for the paramter.

Yeah, unless you are defining the function with : (which in this case is impossible) you have to specify self yourself.

Here are some examples of function definitions. Each function behaves the same, but they have different rules as to how they must be defined to work with self:

local t1 = {}
t1.a = 123

function t1.dot(self) -- self is necessary
    print(self.a)
end

function t2:colon() -- self is not necessary
    print(self.a)
end

t1.assignment = function(self) -- self is necessary. No alternative syntax
    print(self.a)
end

local t2 = {
    b = 456,
    tableDefinition = function(self) -- same as assignment.
        print(self.a)
    end
}

The same idea for when you call any (you can mix and match) of the above functions:

t1:func() -- t1 already passed because of ':'
t1.func(t1) -- t1 must be passed manually because of '.' Inconvenient.

You can mix and match these too. What I mean is you can define a function with . and call it with :, you can also define a function with : and call it with ..

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.