Author Topic: Best Practices, Design Patterns and General Coding Conventions  (Read 6030 times)

0 Members and 1 Guest are viewing this topic.

Offline foobioTopic starter

  • Newbie
  • *
  • Posts: 6
  • Activity:
    0%
  • Reputation Power: 1
  • foobio has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
0
All,

The only dumb question is the one you ask ...

Just joined the site.  Still lurking around a bit and feeling around in the dark.  Is there a thread in the forums talking to best practices and design patterns for LUA and general script building.

Foo's second law - someone smarter than you has already figured it out.

(Foo's First law - what is clear to you - is clear to you)
(Foo's Third law - attach yourself to revenue and distance yourself from cost) < those of you who program for a living should live by this one!!

Have sifted through the LUA site and gotten the basics of the language ... and I see the genetic code of oeuo as well.  But ... It always helps to start coding in a manner that won't make the reader's head explode.  In my day job ... we have style checkers and build agents that force compliance ... so I have become a bit complacent in living in the real world.

Regardless - I will most likely stumble upon something ... but it doesn't hurt to stop and ask for directions. 

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: Best Practices, Design Patterns and General Coding Conventions
« Reply #1 on: April 30, 2012, 05:25:37 AM »
0
lol good luck ;)
Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Best Practices, Design Patterns and General Coding Conventions
« Reply #2 on: April 30, 2012, 07:51:28 AM »
0
Most people here didn't really start programming in Lua until Cheffe released OEUO.  I certainly didn't know much about the language aside from my silly little scripts I wrote to help my WoW character.  So we mostly all learned from scratch.  I noticed my Lua code evolved very quickly from basic scripts to scripts that could better be used as libraries and transitions quickly to object-oriented code.  I still pretty much suck at Lua though - it's not very comfortable since I have some fundamental arguments with the code syntax and style (break, but no continue is one and no switch-case construct for example).

If you'd like to share your adventures coming up with a decent set of practices and style, please do so.  I learn constantly from code that others have done.  I'm sure everyone else would appreciate such an effort.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOver

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Best Practices, Design Patterns and General Coding Conventions
« Reply #3 on: April 30, 2012, 09:37:06 AM »
0
Foo's second law - someone smarter than you has already figured it out.
Although true, the trick is to find said smarter person and get them to tell you what they figured out in an understandable way ;)

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline foobioTopic starter

  • Newbie
  • *
  • Posts: 6
  • Activity:
    0%
  • Reputation Power: 1
  • foobio has no influence.
  • Respect: 0
  • Referrals: 0
    • View Profile
Re: Best Practices, Design Patterns and General Coding Conventions
« Reply #4 on: April 30, 2012, 11:42:36 AM »
0
Well, figuring it out slowly.

As the saying goes ... RTFM ... haha.

I do find the lack of a switch irritating ... but the Dictionary stuff is pretty cool I guess.

I did notice that these latest UO client patches are integrating with a LUA engine ... as evidenced by all the .LUA stuff in the enhanced client directory ...ha.  Also noticed that the dopey new Map utility (which seems rather buggy on the east-west coordinates) is leveraging the UO LUA featureset.

Haven't really jumped into exploring just what our benevolent masters at EA have done with this latest advance ... but I am sure they haven't overachieved on the functionality.

Offline KaliOfLS

  • That's "Dr." Kali to you!
  • Sr. Member
  • *
  • Posts: 406
  • Activity:
    0%
  • Reputation Power: 6
  • KaliOfLS has no influence.
  • Gender: Male
  • Respect: +33
  • Referrals: 2
    • View Profile
Re: Best Practices, Design Patterns and General Coding Conventions
« Reply #5 on: December 11, 2012, 10:54:41 AM »
0
Code: [Select]
I for one, really really really like lua as opposed to the euox scripting language.
I hate to do this and wonder if I'm breaking a taboo, you should also check out easyUO's openEUO forum.  A few guys over there like Snicker7 and ximan are excellent lua coders, from what I can tell.

In terms of good coding axioms, I try to practice lua script writing as if I were writing in C++.  I like writing modular functions so I can copy/paste them into new scripts as needed and know that those functions are going to work as intended.  One axiom I code with is avoiding upvalues in functions.  If you're not entirely sure what an upvalue is in lua, you can check out the lua page http://www.lua.org/pil/6.1.html

in short, I avoid this at all cost
[code]
--some code
local resourcebagID = UO.LtargetID
--blah more code

function GetRegs()
    --blah, ScanItems/GetItems or some appropriate call to a FindItem sub. 
    if FountItemContID == resourcebagID then
    --blah
    end
    return
end

In that example, resourcebagID is known as an upvalue.  It was defined in a local scope that the function was defined in.  In lua, if you do not define a variable it will look in the parent scope for a definition.  I prefer to send all variables in as arguments and define all function constants within the function itself.

Something else I do, is create a function for each simple task I want to do.  Then create functions for each Macro task I wish to do, and then use a main driving loop that is nothing but function calls.  Keeps your thoughts organized and much easier to track down bugs in the script.  For example:
Code: [Select]
function MoveItem(id,dropbag,stack)
           UO.Drag(id,(stack or 1))
           UO.DropC(dropbag or UO.BackpackID)
           wait(1000)
           return
end

function MoveAllOfType(Type,Col,contID)
           --Appropriate finditem call for Type/Col
           for k,v in pairs(AllItems) do
               MoveItem(v['ID'],contID)
           end
           return
end

Now I can use those subs when ever I want in any script.  A bod sorting script, Looting script, etc and if they fail to move something, then I know it's a consequence of something else, not those functions. 

I am also a comment happy fella.  It makes it easier to understand your own work later, as well as for others to learn from you. :)


     


[/code]
R~~~~ B~~~~~~~~ 
^ real life signature for sure

Tags: