• I want to get into more advanced, efficient techniques, what can I do?
    4 replies, posted
More detailed statement: I have the basics of entity creation, network messaging, Derma, navmeshes, and other small aspects of Glua down fairly well, however, I want to make my code more efficient, I see all of these scripts that have manipulable prop positioning built into their systems, or things that are "compatable" with other addons. I would like to learn how to do things like whitelisting systems, to save values assigned to certain players, darkRP machines that generate money, somewhat like BitMiners 2, (Huge fan of CODE BLUE btw cause his videos helped me learn most of what I already know). It also could not hurt to make my code more slim, and efficient, what can I replace a hefty loop with? Multiple ifelse statements, thinking function checks, hooks, all of these things, I want to know what types of coding exercises I can do, if you will, just to become more knowledgeable in the more advanced parts of Glua, so I will be able to create better, and more useful addons, thanks. Any ideas welcome btw.
just do it (learn by experimenting)
The best way I learn new techniques is to just look at the addons. Read through the code, make small changes, see what happens. Repeat until you begin to understand what is happening. If you can't understand anything, start smaller. After a while of doing this kind of thing you'll learn to just know what to do to accomplish certain tasks from experience.
A lot of addons are hosted on github, allowing you to read the files without de-compiling them. Here is a quick list. All those if-else statements can usually be replaced with a table and/or a loop. -- Example on tables and loop. local tickets = {} function GiveTicket(ply)     if GetTicket(ply) then return false end -- Player already got a ticket     table.insert(tickets,ply) -- Insert a ticket     return true end function TakeTicket(ply)     local id = GetTicket(ply)     if not id then return end -- No ticket found     table.remove(tickets,id) -- Remove the ticket end function GetTicket(ply)     for id,ply2 in ipairs(tickets) do -- Loop the whole list. ipairs and pairs are super useful.         if ply2 == ply then return id end -- If the player match, return the ticket id     end end Try and keep the script localize with "local" in-front of variables and functions. This makes it faster and more compatible. local function GiveTicket(ply)     if GetTicket(ply) then return end -- Player already got a ticket     table.insert(tickets,ply) -- Insert a ticket end Entities can hold variables function GiveMoney(ply,amount)     ply["money"] = (ply["money"] or 0) + amount end function GetMoney(ply)     return ply["money"] or 0 end Try and challenge yourself. My first script was an addon to ULX called !yoda <text>. It would switch the text given, to sound like Yoda.
If you want faster code, you have to benchmark it. Dbugr and FProfiler are pretty essential there. Find the most expensive functions and hooks and work down. Understanding when you should cache variables, and when you create objects needlessly. (Color() for example might be in a loop, but it never changes. So you're creating a new color object with the same values every loop iteration. Move it out of the loop so you only make it once.) Things like Dist2Sqr are helpful too because of how distance is calculated in math. Square root functions are very expensive, so if you want to check distance between two points, ent1:Dist(ent2) <= 500 could become ent1:Dist2Sqr(ent2) <= 250000 (that's just 500², since Dist2Sqr doesn't do the square root). Obviously for human readable stuff you'll use normal distance but for technical stuff, squaring your distance is way cheaper than square rooting. There's a whole bunch of things you can do once you figure out how things fundamentally work like I mentioned above.
Sorry, you need to Log In to post a reply to this thread.