Jump to content

OOP and metatables


franku

Recommended Posts

Sup guys, so before I get into the actual question, I'd like to tell you that I haven't scripted in almost a year and I just started like a week ago again.

So what i'm asking is, I have this code here (Below) from a script I made 3 days ago, what I'd like to know is, how do I turn it into OOP? And throw a metatable in it, plus making use of it. And why is OOP useful and how? (If you'd like to help explaining, mind not linking me to different pages? explain using your own words please, that'd be easier for me to understand. :) )

Once I know why it's useful and how to use it, I'ma start using both OOP and metatables in future scripts of mine :)

Code:

function stopEvent( thePlayer ) 
local groupName = "EventManager" 
local zzz= getPlayerAccount( thePlayer ) 
    if ( zzz) then 
        local accName = getAccountName( zzz) 
        if  ( isObjectInACLGroup ( "user."..accName, aclGetGroup ( groupName ) ) ) then 
            -- for k, thePlayer in pairs( getElementsByType( 'player' ) ) doe 
            removeEventHandler( "onFreezFight", resourceRoot, freezHandlar ) 
            removeEventHandler( "onFightStart", resourceRoot, spawnHandlre ) 
                for _, thePlzzayer in pairs( getElementsByType( 'player' ) ) do 
                    takeAllWeapons( thePlzzayer ) 
                    setElementFrozen( thePlzzayer, false ) 
                    toggleControl ( thePlzzayer, "fire", true ) 
                    spawnPlayer ( thePlzzayer, xw, yw, zw ) 
                end 
            -- end 
        outputChatBox( '[Event] Event has stopped.', localPlayer, 255, 0, 0, true ) 
        end 
    end 
end 
addCommandHandler( 'stopit', stopEvent ) 

Thank you.

Link to comment

So, I'll try to sum it up quickly, the advantage of OOP is that you don't have to rewrite the whole thing over and over like "normal" scripts do. Even tho OOP is much harder, once you get used to it you won't ever script in the "normal" way again.

In the script you posted, if you turn it into OOP nothing would change but the syntax. It's the same stuff literary, but I have a very good example that progressed me very much.

https://forum.multitheftauto.com/viewtopic.php?f ... le#p753385

a cool example of metatables and OOPs.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...