[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to optimize this?
- From: HyperHacker <hyperhacker@...>
- Date: Sat, 5 Nov 2011 21:42:27 -0600
On Sat, Nov 5, 2011 at 21:26, PROXiCiDE <saiyuk7@gmail.com> wrote:
> I want to reduce the typing of repeating for each RACE, in a array. Contains
> location of sound files for each race and sex. How can i optimize
> the code below?
>
> Sound file locations seem to repeat with each other containing
> "Sound\\Character\\[Race]\\[Race][Sex]_Err_NoMana03.wav
>
> local manaSoundList = {
> ["Draenei"] = {
> female = "Sound\\Character\\Draenei\\DraeneiFemale_Err_NoMana03.wav",
> male = "Sound\\Character\\Draenei\\DraeneiMale_Err_NoMana03.wav"
> },
> ["Dwarf"] = {
> female = "Sound\\Character\\Dwarf\\DwarfFemale_Err_NoMana03.wav",
> male = "Sound\\Character\\Dwarf\\DwarfMale_Err_NoMana03.wav"
> },
> etc...
> }
>
> The below contains a structure to hold all the data from the players
> inside the party, if it doesnt exist then we will have to create it to
> prevent errors later on
>
> if not healerList[unitID] then
> healerList[unitID] = {}
> healerList[unitID].strikes = 0
> healerList[unitID].name = UnitName(unitID)
> healerList[unitID].unitID = unitID
> healerList[unitID].connection = UnitIsConnected(unitID)
> healerList[unitID].race = select(2,UnitRace(unitID))
> end
>
> local function PlayLowManaSound(unitID)
> if not healerList[unitID] then return end
>
> local soundFile = nil
> if healerList[unitID].sex == 2 then
> soundFile = manaSoundList[healerList[unitID].race].male
> else
> soundFile = manaSoundList[healerList[unitID].race].female
> end
>
> PlaySound(soundFile)
> end
>
>
>
Learn to use loops and string concatenation:
local races = {"Draenel", "Dwarf", etc...}
local sexes = {"male", "female"}
local manaSoundList = {}
for i, race in ipairs(races) do
local tbl = {}
for j, sex in ipairs(sexes) do
tbl[sex] = "Sound\\Character\\" .. race .. "\\" .. race .. sex ..
"_Err_NoMana03.wav"
end
manaSoundList[race] = tbl
end
Notice that we aren't actually using i and j here. A convention in Lua
would be to name them both _ to indicate they aren't being used.
Then, you can improve it even more using string.gsub():
local races = {"Draenel", "Dwarf", etc...}
local sexes = {"male", "female"}
local path = "Sound\\Character\\<race>\\<race><sex>_Err_NoMana03.wav"
local manaSoundList = {}
for i, race in ipairs(races) do
local tbl = {}
for j, sex in ipairs(sexes) do
tbl[sex] = path:gsub('<race>', race):gsub('<sex>', sex)
end
manaSoundList[race] = tbl
end
>From there you can probably see how you'd go about adding another loop
for multiple sound files, or perhaps a table of variables and gsub()
in a loop instead of 2 gsub() calls, and so on...
As for the rest of the code, the second block can be optimized to
remove the repetition:
if not healerList[unitID] then
healerList[unitID] = {
strikes = 0,
name = UnitName(unitID),
unitID = unitID,
connection = UnitIsConnected(unitID),
race = select(2,UnitRace(unitID)),
}
end
Lua lets you leave that last comma there. I like to leave it so I
don't forget it if I want to add more later.
And the third block can be trimmed a little too. You could trim it
even more, but it becomes a bit less readable.
local function PlayLowManaSound(unitID)
if not healerList[unitID] then return end
local sexes = {[1] = "female", [2] = "male"} --I'm assuming 1 = female here
local race = healerList[unitID].race
local sex = sexes[healerList[unitID].sex]
PlaySound( manaSoundList[race][sex] )
end
Generally, when coding it's a good idea to eliminate as much
repetition as possible. (This is the DRY principle - Don't Repeat
Yourself.) You do this by putting things in loops, variables, or
functions instead of repeating them. This can make your code shorter
and more readable, and if you ever have to change something (like the
path to your sound files), you only have to change one line instead of
50.
--
Sent from my toaster.