Army@1 Posted December 31, 2015 Share Posted December 31, 2015 Hello everyone, I am slow learner and today I wanna know what are the differences between: Object (not in-game objects like createObject) Classes MTA Classes Tables Arrays Metatables Elements Children Parent I know most of these things but I am quiet confused between them and also it will help me in understanding programming in deep. Also where can I learn matrix and vertex as used in programming languages (lua). There is page for them but it is not that helpful, I want a basic one for a beginner for complete understanding. I would really appreciate your guys help. Link to comment
Revolt Posted December 31, 2015 Share Posted December 31, 2015 You can explore the Lua's wiki to find out more about these topics you're interested in, but if you only wish to learn Lua for MTA, you don't need to understand all of them (for example, Lua does not use classes at all, rather it uses light user data, therefore you probably won't need them either) Here are some links you might find useful: http://lua-users.org/wiki/SimpleLuaClasses http://lua-users.org/wiki/TablesTutorial http://www.lua.org/pil/11.1.html http://www.lua.org/pil/2.5.html Link to comment
Army@1 Posted December 31, 2015 Author Share Posted December 31, 2015 Sorry but I am not asking particularly for lua but for programming as a whole. Also the links you give assume the user knew and understand some parts of the lua, which I do not (at least fully). And it would be really helpful if someone explain these things from personal views and understanding. This way, we could also clear our misunderstandings. Thanks. Link to comment
Revolt Posted December 31, 2015 Share Posted December 31, 2015 If you wish to learn about programming and how computers work in general, I recommend you to read some C++ book. To explain these concepts to you fully, an user would have to spend a lot of time writing, so since there already exist a hefty load of resources for you to learn from, it is a waste of time and, therefore, no-one will do it. Link to comment
Army@1 Posted December 31, 2015 Author Share Posted December 31, 2015 I am agree with you, covering such things in details could require lots of writing. but covering some basics like globalVariable = "Script a" -- this is a variable function blabla() local globalVariable = "Function a'" -- this is a local variable and it works only inside the function anotherGlobalVariable = "Script b" -- global variables can be define inside the functions too local localVariable = "Function c" -- this is also a local variable and it works only inside the functon outputChatBox(globalVariable) -- this will output "function a" and not "Script a" as it is inside the function outputChatBox(anotherGlobalVariable) -- this will output "Script b" outputChatBox(localVariable) -- this will output "Function C" end outputChatBox(globalVariable) -- this will output "Script a" and not "Function a" as "Function a" works only inside the function outputChatBox(anotherGlobalVariable) -- this will output "Script b" as it is also a global variable but just define inside outputChatBox(localVariable) -- this will output an error as localVariable works only inside the function may not. Maybe just distinguishing and comparing them or a example code could also be helpful. Sorry if it troubles you. Link to comment
Noki Posted December 31, 2015 Share Posted December 31, 2015 Objects & Classes - A class is, as it's name suggests, a class. Classes are used to organise functions and make things cleaner. Let's say we have a class called "Bank". When we create a new instance of Bank (using something like Bank() or Bank.create()), we have a Bank object. With this bank object, we can do many things with it (bank:balance(500), bank:interest(0.04), etc). MTA Classes - Player, Vehicle, Object (not to be confused with OOP objects like explained above), Light, Vector(2/3/4), Matrix, etc. These are in-built classes in MTA and contain methods (also known as functions) which can be used to do something with an instance of the object (vehicle:method()). Though, you don't always need an instance of that object to call a function from that class (Vehicle.method() - a static function). Table & Arrays - A table contains a key and a value. To access the value, you need to provide a key. t = {} t[1] = "one", -- print(t[1]) -> one t["one"] = 1, -- print(t["one"]) -> 1 t[1] = "one", t[2] = "two", t[3] = "three" for index, value in ipairs(t) do print(index.." - "..value) end --> 1 - one --> 2- two --> 3 - three Elements - An element is usually an instance of a certain class. It's just the procedural way of addressing it. There are vehicle, object, player elements and more. Children & Parents - Familiarize yourself with the element tree. If I create a gridlist within a GUI window, the window would be the parent of the gridlist and the gridlist would be the child of the parent. If I hid the window, the gridlist would also hide because it is a child of the window. The child does whatever their parent does (though there are exceptions and you can sometimes change this). Link to comment
Revolt Posted January 1, 2016 Share Posted January 1, 2016 Player, Vehicle, Object... are not actually classes, rather they're light user-data pointers. The difference would be that the MTA team hasn't implemented any class functions into Lua, so we have to use global functions to modify any class attributes. Also table does not require a key enumerator. local t = {"a", "b", "c"} for a, b in ipairs(t) do print(tostring(a) .. ": " .. b) end -- Output would be: -- 1: a -- 2: b -- 3: c Link to comment
Army@1 Posted January 1, 2016 Author Share Posted January 1, 2016 Thanks a lot Noki for the explanations! Objects and Classes: Let Bank And Water be classes. Then we have a 'Bank Class' and 'Water Class' Bank.create,Bank.destroy,Bank.Interest,etc... are 'Bank Objects'. Similarly, Water.create, Water.destroy, Water.createOcean, etc... are 'Water Objects'. Am I right? Also all programming languages class's and object's work in similar manner? MTA Classes: whats the different between vehicle:method() and vehicle.method()? How to define one and how each one work? Player, Vehicle, Object... are not actually classes, rather they're light user-data pointers. The difference would be that the MTA team hasn't implemented any class functions into Lua, so we have to use global functions to modify any class attributes.Also table does not require a key enumerator. local t = {"a", "b", "c"} for a, b in ipairs(t) do print(tostring(a) .. ": " .. b) end -- Output would be: -- 1: a -- 2: b -- 3: c Thanks Revolt. but do you mean that there are not classes in MTA i.e MTA Classes? Also what is light user-data pointers and where I can learn about them? Link to comment
Revolt Posted January 1, 2016 Share Posted January 1, 2016 User-data is a type of variable in Lua. You don't need to know anything about it unless you wish to integrate Lua into your C/C++ application. Basically what it is is a pointer to the real class created in C/C++. As of pointers, they are a type of data that actually does not store what it represents in it self, yet is stores a pointer (a location; address) to that data. For example, let's say you declare this integers (simplified): int a = 62; // imagine this is stored at the 0x00000001 memory address int b = 241; // 0x00000005 int c = 352; // 0x00000009 int* _b = &b; // the value data of _b is actually 0x00000005, but since it is a pointer (declared int*), when used in code (as *_b), it will represent whatever b is representing (it is 241 now) printf("%i", *_b); // the print function, it prints out 241 b = 5; // because _b is a pointer, it points to the value of b, and therefore if you use _b, it will point to the data whose value now is 5 printf("%i", *_b); // prints out 5; notice how we only changed b, yet the value of *_b is still same as the value of b I tried simplifying it, but I'm pretty bad at explaining anything, so rather use cplusplus.com to learn. Link to comment
Army@1 Posted January 1, 2016 Author Share Posted January 1, 2016 I think I got the basic Idea of pointers. a = 23; b = a -- Here b act as a pointer(Not actually but in a similar way)? outputChatBox(b) Did I? Also the key, when we loop through the player table, is actually a user-data? I.e. for k,v in ipairs(getElementsByType("player")) do outputChatBox(k) -- here 'k' is a user-data value? and the thing which points 'k' to 'v' is a pointer? end Link to comment
Revolt Posted January 1, 2016 Share Posted January 1, 2016 Actually, pointers are not accessible in Lua (my example was in C). The thing you're doing in Lua is just copying one value (a) and setting the other one (b). And for the other code, you could do outputChatBox(tostring(v)) -- not k, and you could see the actual value. Link to comment
Army@1 Posted January 1, 2016 Author Share Posted January 1, 2016 Actually, pointers are not accessible in Lua (my example was in C). The thing you're doing in Lua is just copying one value (a) and setting the other one (b). Of-course, I know as your previous code was in C but I am talking about the idea. Does it similar to it in some way(without changing the value)? you could do outputChatBox(tostring(v)) -- not k, and you could see the actual value. Yeah but I am talking about the user-data not the actual which should be 'k' If I understood correctly. Did you read my comment inside the lua code? Link to comment
Revolt Posted January 1, 2016 Share Posted January 1, 2016 No, that Lua code you given was completely unrelated to pointers. It is practically impossible to use pointers (aside from user-data elements or tables, which can't be manipulated) in Lua. As of your other code, k is actually the enumerator value, and the v is an actual element. If you print the value of k, you would get a number (for example, 1 if your element is the first in the table, 2 if it's the second...), but if you print the value of v, you would get the actual pointer to the class in memory. Link to comment
Army@1 Posted January 1, 2016 Author Share Posted January 1, 2016 Oh, my bad. Thanks, I understand it now. Could you please answer the following questions too if you have time? Objects and Classes:Let Bank And Water be classes. Then we have a 'Bank Class' and 'Water Class' Bank.create,Bank.destroy,Bank.Interest,etc... are 'Bank Objects'. Similarly, Water.create, Water.destroy, Water.createOcean, etc... are 'Water Objects'. Am I right? Also all programming languages class's and object's work in similar manner? MTA Classes: whats the different between vehicle:method() and vehicle.method()? How to define one and how each one work? Link to comment
Revolt Posted January 1, 2016 Share Posted January 1, 2016 Account = {} Account.__index = Account function Account.create(balance) local acnt = {} -- our new object setmetatable(acnt,Account) -- make Account handle lookup acnt.balance = balance -- initialize our object return acnt end function Account:withdraw(amount) self.balance = self.balance - amount end -- create and use an Account acc = Account.create(1000) acc:withdraw(100) Take this for example (from http://lua-users.org/wiki/SimpleLuaClasses ) The difference is that you can call Account.create(..) without having an instance of the class available, but to call withdraw you must have a class instance to perform withdrawal on. Link to comment
Army@1 Posted January 3, 2016 Author Share Posted January 3, 2016 hmm, that looks tough. I will look into it later. Meanwhile I am struggling with user-data. In lua, Whenever I create a table, for instance, and print it, it output something like 'table: 003DAF20'.In that, what is '003DAF20'? Is it a user-data? If not, what is it then? table-data? or something related to memory? i.e. function myTable() local atable = {} print(atable) end -- output: --table: 003DAF20 --what is '003DAF20' Link to comment
Addlibs Posted January 3, 2016 Share Posted January 3, 2016 It's a hexadecimal memory address reference Link to comment
Army@1 Posted January 4, 2016 Author Share Posted January 4, 2016 It's a hexadecimal memory address reference What is it actually? is there limit to how much hma(hexademical memory address), RAM can handle? I made a simple script to find out if they can be repeated. What I did was that I made a for loop which create a table, gets its hma and then set the table to nil. The hma is stored in another table. But what I found is stange, the hma number is not repeated until it is near 70, the least I could found is 67. Is it how much my ram can handle or something like that? Here is a actual script which I made: function hmaRepChecker() print("Hexadecimal Memory Address repeatation checker has been started: ") local hma = {} local hmarep = {} local hmarepdata = {} print("(Start)Total HMA's: "..#hma) print("(Start)Total repeated HMA's: "..#hmarep) for i=1,100 do local tab = {} local tvalue = tostring(tab) local tud= tvalue:gsub("table: ", "") table.insert(hma,tud) for j=1,100 do if hma[i] == hma[j] and i ~= j then table.insert(hmarep,hma[i]) hmarepdatavalue = tostring("hma("..hma[i]..") repeated at: "..j.." and "..i) table.insert(hmarepdata,hmarepdatavalue) print(hmarepdatavalue) end end tab = nil end print("(Finish)Total HMA's: "..#hma) print("(Finish)Total repeated HMA's: "..#hmarep) end Change the for loop's i's and j's repeatation value(which is 100 currently) proportionally and see where they start repeating hma for you. Sorry, if its just simple or nonsense type of thing but I am curious. Link to comment
Revolt Posted January 4, 2016 Share Posted January 4, 2016 There is a limit of how high in the memory can the processor write. For 32 bit applications it is 0xFFFFFFFF (well, theoretically at least). Those values (around 70) overlap because you declared the table local, therefore, when they're no longer needed they get destroyed automatically. Link to comment
Army@1 Posted January 4, 2016 Author Share Posted January 4, 2016 There is a limit of how high in the memory can the processor write. For 32 bit applications it is 0xFFFFFFFF (well, theoretically at least). Those values (around 70) overlap because you declared the table local, therefore, when they're no longer needed they get destroyed automatically. I get that but I am wondering why they overlap at around 70 (or 67 >= number) specifically. Is it related to 32 bit or 64 bit? And no, they don't overlap just in local table, I tried global too and got the same result. Link to comment
Addlibs Posted January 4, 2016 Share Posted January 4, 2016 One memory address is a bit-sized. Therefore, after you exhaust all 64 bits, it resets and starts overlapping the old ones that are no longer needed, I think? This would be an explanation as to why ≈70 (64 ≈ 70±6) is the number where it starts overlapping. I think this is hardware-related, as on the http://www.lua.org/cgi-bin/demo site, it starts repeating near ≈106. (Yep, had to extend the for i loop as it didn't catch any conflicts with the original code) It might also be possible that previous tables change their memory address at one point to free up space for others. Therefore a new table might be assigned an address that was once used but isn't in use anymore. http://www.cplusplus.com/forum/beginner/134971/ — even though it's not directly about Lua, I think we can safely assume Lua acts the same way because of the OS. Link to comment
Revolt Posted January 4, 2016 Share Posted January 4, 2016 Not really related to CPU architecture (by your theory, a table would occupy 16777216 TB of RAM in x64), yet it is related to available (assigned) memory. The program running Lua, in this case MTA, has a specific amount of RAM it can address to. Table is then given the maximum space it can write to (upon creation), then when the end of memory is reached, the unused table is automatically cleared (or overlaps with the previous one). Link to comment
Army@1 Posted January 4, 2016 Author Share Posted January 4, 2016 Thanks guys for the explanations. BTW, in lua, whats the different between just 'function' and 'local function'. I know about variables but functions seems bit confusing to me as I hear we can called them outside scope too. Link to comment
Revolt Posted January 4, 2016 Share Posted January 4, 2016 Local function can only be used inside the file it was declared it (and so can any other local variable that has been declared outside of functions). Link to comment
Army@1 Posted January 5, 2016 Author Share Posted January 5, 2016 Thanks everyone for answering my confusions till now. Okay, the classes,objects,metatables concepts is appearing to be less confusing to me. The other thing which I find hard to understand is vectors and matrices.... What are these actually and how/when to use them? The MTA wiki seems to not help me. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now