|
| 20 Aug 2016 07:38 PM |
so i saw this
Account = {} function Account.new(balance) return setmetatable({balance = balance}, Account) end Account.__index = Account
so i think, if i say Account.new(5), it will be set to balance = 5? |
|
|
| Report Abuse |
|
|
ParaText
|
  |
| Joined: 15 Aug 2016 |
| Total Posts: 799 |
|
| |
|
|
| 20 Aug 2016 07:41 PM |
| actually i saw a thread, took this snippet of code and i wanted to try to understand it |
|
|
| Report Abuse |
|
|
ParaText
|
  |
| Joined: 15 Aug 2016 |
| Total Posts: 799 |
|
|
| 20 Aug 2016 07:42 PM |
Ohhh, this video can explain it better than I can: https://www.youtube.com/watch?v=kLsso_8draE |
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 07:43 PM |
| aah before you do, can you tell me if that above example was correct though? |
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 07:44 PM |
Yes OP, a new table is created and in that table "balance" is set to 5.
|
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 07:53 PM |
| {(balance=balance) how does that work? |
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 07:53 PM |
It's just a table...
local x = { a = 5 }
print(x.a) --5 |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 20 Aug 2016 07:55 PM |
"{(balance=balance) how does that work?"
Does this make it clearer?
function Account.new(newBalance) return setmetatable({balance = newBalance}, Account) end
|
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 07:58 PM |
@flux so basically balance is inside balance? Still confused
|
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 07:59 PM |
No, the key is balance and the value the argument passed.
local function x(y) local z = {y = y} print(y.z) -- 3 end
x(3) |
|
|
| Report Abuse |
|
|
| |
|
|
| 20 Aug 2016 08:02 PM |
| I honestly have no idea anymore |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 20 Aug 2016 08:03 PM |
It's super duper simple. It's a normal dictionary like
table = {INDEX = VALUE} print(table.INDEX)
>VALUE
The confusing part (for you) is that the index is called balance and the value (from the parameter) is also called balance.
|
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 08:04 PM |
| Whoops I posted that while you posted. I got it now. One last question y=y?? |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 20 Aug 2016 08:06 PM |
just imagine this:
function(x) table = {x=x} print(table.x) end
is the same as this:
function(x) table = {y=x} print(table.y) end
Just that the index is different, in the first example the index is X, second example index is Y
|
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 08:10 PM |
| So if you did table{y=x} print(table.y) it would be the same as print(table.x) ? |
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 08:14 PM |
tables can store variables like anything else can
local tbl = { x = "hello there" }
print(tbl.x)
think of it like properties of a part
print(part.Position)
and you can store functions and etc, anything that is a variable |
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 08:19 PM |
| No. What is so confusing to understand. Learn tables before trying to learn metatables. |
|
|
| Report Abuse |
|
|
|
| 20 Aug 2016 08:39 PM |
| I'm confused how Y equals X but X doesn't print what the value of y is |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 20 Aug 2016 08:46 PM |
You really don't understand what we're trying to explain.
table = {a = "asdf"} print(table.a) >asdf
table = {b = "asdf"} print(table.b) >asdf
local c = "asdf" table = {c=c} print(table.c) >asdf
|
|
|
| Report Abuse |
|
|