How are these different?

I’ve just finished refactoring some code, and while doing that I ran into the problem of misunderstanding how something works.

I have a module with a function that takes (self) as an argument and in a script I was pointing to that function in init(self) by doing self.thing = module:function() but when I did that, it seemed that the (self) was not the same (self) as the (self) used by script and whenever I tried to index anything found in the script’s (self) it would return nil. When I didn’t use the colon syntax, it behaved as I expected and I was able to access the things I had in (self). What am I missing here? I thought colon syntax was purely syntactic?

module:function(args) is same as function(module, args).

4 Likes

Well, self.function is referential for that script. So if your module is M and it has a function M.foo(self) you can reference it by adding M to the self of this script or by M.foo. For my modules I have a variable on self called modules (self.modules) and a function that returns all of the modules for that script, initialize_modules. So to call M.foo would need to do self.modules.M.foo for this particular scripts version of M.foo.

Depending on if you want the scripts to share the modules or have their own instance you need to create the modules differently. I believe this is called a stateful module. https://defold.com/manuals/modules/