• Object oriented functions vs normal variable functions?
    10 replies, posted
Hello everyone! I have got a quick question. Which method is the best one to use? A more object oriented method function ply:Teleport( pos, ang ) if( !self:IsValid() ) then return end self:SetPos( pos ) self:SetAngles( ang ) end Normal function method function Teleport( ply, pos, ang ) if( !ply:IsValid() ) then return end ply:SetPos( pos ) ply:SetAngles( ang ) end Does it even matter which method I choose to use?
I would say less characters means less bits the game has to process so probably ply but only the tiniest of margin you wouldn't even be able to tell.
The first is better practice, but won't work in the way you've written there. If you want to make it so that you can call Teleport(pos,ang) on a player ex: for k, v in pairs(player.getAll()) do v:Teleport(pos,ang) end Then you'll have to define the function onto the player object template (from which all Player entities inherit functions from). FindMetaTable
Don't try to help if you don't even know what you are talking about, really, why even bother? As bitches above mentioned, using a meta function for this is good practice and generally looks nicer, this way you also define it only for Players, as oposed to when using the normal method, you can pass any entity as the 'ply' argument.
Alright cool! Thanks for the responses!
Because this is a discussion thread and every time I'm wrong one of you comes along and corrects me; and if anyone else that wants to learn lua sees any of these threads things are also explained. Last time I check being wrong wasn't against the rules. My name is pretty self explanitory so I don't know why you all feel the need to keep harassing me. Thanks for explaining why it's more efficient.
The OP asked a question, he expected information from people with experience who know what they are talking about. You being a beginner who just recently picked up Lua (yes, I keep track of people and their shenanigans around the Lua Dev Discussion) replied with an answer which indicates that even YOU were unsure about what you have stated, yet you still did it, and it was infact dumb. Again, I am not trying to be hostile or harass you, but I have seen SO many people around here (you included) who don't know what the hell they are talking about, spreading misinformation on many subjects. If you are not sure that the answer you are about to provide is not correct or at least remotely relevant to the subject at hand, you should refrain from replying, you will just confuse other people, you may or may not get laughed at, and me or someone like me will surely point out things like this.
Lua uses string interning. This means that only 1 copy of a given string can exist within the Lua state. Why is this important or relevant? The benefit of string interning is that checking the equality of 2 strings is fast. It's as fast as checking numbers. The length of the strings don't matter for equality tests. Nothing is free though. The downside is that creating new strings is slow. Performing string concatenations in Lua will cause an interning operation for each concat operation. This is why table.concat exists. It allows you to sidestep this downside by performing mass string concatenation in C, where string interning is not used. Lua only sees the result string, so only 1 interning operation must be performed.
So are you saying everything is pre-indexed? Or is that just for locals?
Just for locals. Accessing a variable on a table still requires a string compare, but like I said, it's pretty fast due to string interning. The names of locals aren't important at runtime, so they can be optimized into an access by numerical index. Variables on a table have names that can be printed and accessed from other scripts, so those names must be preserved.
Thank you for elaborating on this
Sorry, you need to Log In to post a reply to this thread.