I find objects really good in decomposing a complex problem into smaller parts , which is one of the 4 pillars of programming /computational thinking in general which are decomposition , pattern recognistion , abstraction and algorithms
You don't need to set the metatable's *__index* every time you create a new fighter, though, you can do that only once. You also shouldn't use a colon on the constructor call *(Fighter:new()),* as the constructor is a static method of the class, not of the instances. *local Fighter = {* -- default class stuff *}* *Fighter.__index = Fighter* -- set index only once *function Fighter.new(info)* *local t = setmetatable({* *name = info.name or "Unnamed Fighter",* *health = info.health or 100,* *speed = info.speed or 100,* *}, Fighter)* *return t* *end* *local blanka = Fighter.new( {name="Blanka", speed=60} )* (youtube's lousy text formatting ruins the underscores...)
I like this way, but setting optional values with *or* isn't good at all, because you can duplicate values, setting the optional values in the table Fighter (the class) you get more performance and avoid duplicating values
Is there a way to make "Fighter:new" just "new"? That function isn't doing anything special, just Lua's way of creating a class, right? If I wanted to have a game that had multiple classes (fighters, gunners, drivers, etc.) it seems a bit silly to have to copy and paste the same exact function, but call it "Fighter:new" "Gunner:new" "Driver:new" and so on. In other words, I'd like to do Jack = Fighter:new ([Name = Jack]) John = Gunner:new ([Name = John]) and it does all that t and self stuff, but it goes to the same "new" function.
Thank you, finally a visual representation of the interaction between these components that show us how they tie together!
I find objects really good in decomposing a complex problem into smaller parts , which is one of the 4 pillars of programming /computational thinking in general which are decomposition , pattern recognistion , abstraction and algorithms
Finally understood objects in Lua, thank you!
You don't need to set the metatable's *__index* every time you create a new fighter, though, you can do that only once. You also shouldn't use a colon on the constructor call *(Fighter:new()),* as the constructor is a static method of the class, not of the instances.
*local Fighter = {*
-- default class stuff
*}*
*Fighter.__index = Fighter* -- set index only once
*function Fighter.new(info)*
*local t = setmetatable({*
*name = info.name or "Unnamed Fighter",*
*health = info.health or 100,*
*speed = info.speed or 100,*
*}, Fighter)*
*return t*
*end*
*local blanka = Fighter.new( {name="Blanka", speed=60} )*
(youtube's lousy text formatting ruins the underscores...)
I like this way, but setting optional values with *or* isn't good at all, because you can duplicate values, setting the optional values in the table Fighter (the class) you get more performance and avoid duplicating values
Thank you very much, this is really helping me grasp OOP in Lua, and I now feel much more confident in the game I am making currently!
Very awesome!
I'm familiar with doing OOP with Lua, this video is a lot better than learning what to do from the wiki article, haha
Thank you! As someone getting into lua, this really helped with how to use the code :))
Glad it helped!
very underrated best lecturer! please make more courses on udemy and you're website, related to game development or other programming languages
Hi professor...I am trying to access your curses but it is dropping down the page.
Hello Bruno! Something must be going on with my hosting server. I'll check it out. Thanks!
Is there a way to make "Fighter:new" just "new"? That function isn't doing anything special, just Lua's way of creating a class, right?
If I wanted to have a game that had multiple classes (fighters, gunners, drivers, etc.) it seems a bit silly to have to copy and paste the same exact function, but call it "Fighter:new" "Gunner:new" "Driver:new" and so on.
In other words, I'd like to do
Jack = Fighter:new ([Name = Jack])
John = Gunner:new ([Name = John])
and it does all that t and self stuff, but it goes to the same "new" function.
Check DevJeeper's videos on lua OOP, as he goes into that (which is called inheritance).
At 14:50 you say "t is not going to be new hopefully". Why do you say that? I thought the whole point was that you are instanciating a new object?
I think the word I tried to say was "nil", as in it was not going to be nil/null. 🥲
So you have a table.
And function that will work with it
I wish I could also see some inheritance with this method. Still great video!
Next time! :)
It's a shame you didn't continue this
Hi. We did. It's all hosted at pikuma.com.
History teacher
Jesus amado pq tu nao fez em ptbr mesmo esse inglês não ta bom não fi mas bom tutorial usando a legenda.