PDA

View Full Version : Understanding Modules



djnacht
01-27-2016, 10:40 PM
This post isn't another how do modules work in Tibia, it's more of trying to make sense of the code itself.
My programming experience is weak, so the concept of object oriented programming is not clear for me just yet.
I'm hoping with this post someone will be able to clear up some of the programming concepts pertaining to Xenobot modules in LUA that have been hard for me to grasp.

Declaring a module and the concept of a module is rather straight forward.

Module.New(name, function) -- "Registers the specified function to loop as a module."

This is clear to me. We give a name to the module and we declare an anonymous function.

Next!

Sometimes I see people assigning a module to a variable like so.

NewModule = Module.New('module name', function(mod)

This is where things start to get a bit confusing since Object Oriented programming is still very confusing to me.

Question 1:

What does the variable NewModule contain? Would it be correct to say that it contains the "module object", and if so what does that mean? If this is too complicated a concept to explain in a few lines, then a good topic to research on Google will do just fine.

Question 2:

The anonymous function is expecting a parameter "mod", where does this parameter come from?

In a lot of code examples I then see that "mod" parameter being used to delay the Module itself, like so :

mod:Delay(20000)

I'm assuming that mod contains the object created by "Module.New", but how did it get passed into that function?

kamilqq
01-27-2016, 10:57 PM
This post isn't another how do modules work in Tibia, it's more of trying to make sense of the code itself.
My programming experience is weak, so the concept of object oriented programming is not clear for me just yet.
I'm hoping with this post someone will be able to clear up some of the programming concepts pertaining to Xenobot modules in LUA that have been hard for me to grasp.

Declaring a module and the concept of a module is rather straight forward.

Module.New(name, function) -- "Registers the specified function to loop as a module."

This is clear to me. We give a name to the module and we declare an anonymous function.

Next!

Sometimes I see people assigning a module to a variable like so.

NewModule = Module.New('module name', function(mod)

This is where things start to get a bit confusing since Object Oriented programming is still very confusing to me.

Question 1:

What does the variable NewModule contain? Would it be correct to say that it contains the "module object", and if so what does that mean? If this is too complicated a concept to explain in a few lines, then a good topic to research on Google will do just fine.

Question 2:

The anonymous function is expecting a parameter "mod", where does this parameter come from?

In a lot of code examples I then see that "mod" parameter being used to delay the Module itself, like so :

mod:Delay(20000)

I'm assuming that mod contains the object created by "Module.New", but how did it get passed into that function?

My english is not that perfect so i can't really help you but :



local function someFunction()
print('Iam some function')
end

local someFunction = function()
print('Iam also a function')
end

thats just visual difference i think. Im using the second one.




local modulando = Module.New('Modulus', function(this)
print('im module, im executing every 200ms and + 500ms cause of this:Delay(500)')
this:Delay(500)
end)

-- Module.Stop('Modulus')
-- modulando:Stop()


uncomment the stoppers below, they work same: They stops that module. Get the differnce, sorry but my english isnt that great to exaplain it well. I think i helped in some way.


Also if you want to learn something go if u installed casual : C>Program Files or Program Files(x86) > Xenobot> data | and watch that files. Around 200 line there are Modules, you won't undestand alot cause there are metatables ( its not that easy in lua ) but u can take a look how wheel had been created. :)

djnacht
01-27-2016, 11:17 PM
Your post is helpful, thank you, especially for pointing out that file I can open up to read some of the Xenobot code.

When you write function(this), where does "this" come from? I don't see it being declared anywhere, so how is it being magically passed into the function?

DarkstaR
01-27-2016, 11:20 PM
It is the instance of the module class that gets created when you make a new module, and it can be used to delay, stop, or otherwise modify the module.

djnacht
01-27-2016, 11:28 PM
What is the difference between these two techniques? I've underscored the difference in code.

local modulando = Module.New('Modulus', function(this)
print('im module, im executing every 200ms and + 500ms cause of this:Delay(500)')
this: Delay(500)
end)

local modulando = Module.New('Modulus', function(this)
print('im module, im executing every 200ms and + 500ms cause of this:Delay(500)')
modulando: Delay(500)
end)

DarkstaR
01-27-2016, 11:32 PM
Nothing. This and modulando both point to the same class instance, you are just accessing them from different variables. So it's semantics.

The differences may come in where you have many different modules executing the same function. In that case, the function would want to use the provided instance (this) parameter when accessing the calling module.

djnacht
01-27-2016, 11:36 PM
Alright, things are becoming clearer now, thank you. I don't think I will fully understand the mechanics behind the modules until I've figured out how object oriented programming works.

kamilqq
01-28-2016, 12:16 AM
Alright, things are becoming clearer now, thank you. I don't think I will fully understand the mechanics behind the modules until I've figured out how object oriented programming works.

Learn basics of c++, then lua will be much much much much easier. I can write some shits in c++ not something special but ive made tic tac toe and some statistic program. But some lua functions are hard to understand. But c++ really is going to help you. I think some people will +++ me and they think the same.