Using what I learned from the above, I was able to add in MP, and then condense 230+ lines down to 150 lines.
I'm sure I could further condense the Update function, but I don't quite understand the third example from Iryoku, which I think is how I would be able to condense it. I feel a lot better about my code now though, Thanks again.
Here's a screenshot of how it is working, in case you wanted to see the result
for i = 1, 6 do
_G['p' .. i .. '_settings'] = {pos = {x = nondefault.pos.x + nondefault.step.x * i, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
end
is the same as this
p1_settings = {pos = {x = nondefault.pos.x + nondefault.step.x * 1, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
p2_settings = {pos = {x = nondefault.pos.x + nondefault.step.x * 2, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
p3_settings = {pos = {x = nondefault.pos.x + nondefault.step.x * 3, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
p4_settings = {pos = {x = nondefault.pos.x + nondefault.step.x * 4, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
p5_settings = {pos = {x = nondefault.pos.x + nondefault.step.x * 5, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
p6_settings = {pos = {x = nondefault.pos.x + nondefault.step.x * 6, y = nondefault.pos.y}, text = {size = nondefault.font_size}}
_G stands for the global table
when you create a table or function
examples:
function a()
a = function()
a = {}
the full name would actually be
_G.a
because _G is the global table
tho _G is only needed if you need to call a variable with a dynamic name (use the above code fore an example) because _G is implied
also _G is useful if you have a local and global variable with the same name
example:
a = {]
function b()
local a = {}
end
you can call the local table with a and the global table with _G.a
in programing implied is something that is put in even if you do not use it
also using the above code in your code does not make the running memory smaller only your code smaller in fact the running memory will be bigger
--running memory is the running code in memory
so if your table will never be changed by your code is best to write it out manually for memory size reasons
but if your going to build and rebuild your table dynamically based on data its best to use the above code
--if you look at the code i created for my gearswap display you might understand
---my display code is 29KB how ever if i wrote everything out it would be around 90KB+
---however my code in memory is 100KB+ because of all the tables i build for it
however if you want to do both your code and memory size condensing
you cant at lest not in lua as far as i am aware
here is your code minimized as far as i can make it (running memory size may actually be larger then what you had)
_addon.name = 'PartyP'
_addon.author = 'Allison Jane'
_addon.version = '0.2'
_addon.language = 'english'
texts = require('texts')
config = require('config')
defaults = {pos = {x = 200,y = 100},step = {x = 90,y = 20},pet = {x = 840,y = 440},font_size = 12}
settings = config.load(defaults)
config.save(settings)
config.register(settings, function(settings_table)
local nondefault = defaults
if settings_table.pos.x ~= nil then
nondefault = settings_table
end
hp_settings,hp_display,mp_settings,mp_display = {},{},{},{}
for i = 0, 5 do
hp_settings[i] = {pos = {x = nondefault.pos.x + (nondefault.step.x * i), y = nondefault.pos.y}, text = {size = nondefault.font_size}}
hp_display[i] = texts.new('HP: ${hpp|---}%', hp_settings[i])
mp_settings[i] = {pos = {x = nondefault.pos.x + (nondefault.step.x * i), y = nondefault.pos.y + nondefault.step.y}, text = {size = nondefault.font_size}}
mp_display[i] = texts.new('MP: ${mpp|---}%', mp_settings[i])
end
pet_settings = {pos = {x = nondefault.pet.x, y = nondefault.pet.y}, text = {size = nondefault.font_size}}
p0pet = texts.new(' Pet: ${hpp|---}%', pet_settings)
end)
windower.register_event('prerender', function()
local party = windower.ffxi.get_party()
local mypet = windower.ffxi.get_mob_by_target('pet')
for i = 0, 5 do
if party["p"..i] ~= nil then
hp_display[i].hpp = party['p'..i].hpp
mp_display[i].mpp = party['p'..i].mpp
hp_display[i]:show()
mp_display[i]:show()
else
hp_display[i]:hide()
mp_display[i]:hide()
end
end
if mypet ~= nil then
p0pet.hpp = mypet.hpp
p0pet:show()
else
p0pet:hide()
end
end)
i dont recommend using prerender like this because it will be triggered 30+ times a second
i recommend
function Update()
local party = windower.ffxi.get_party()
local mypet = windower.ffxi.get_mob_by_target('pet')
for i = 0, 5 do
if party["p"..i] ~= nil then
hp_display[i].hpp = party['p'..i].hpp
mp_display[i].mpp = party['p'..i].mpp
hp_display[i]:show()
mp_display[i]:show()
else
hp_display[i]:hide()
mp_display[i]:hide()
end
end
if mypet ~= nil then
p0pet.hpp = mypet.hpp
p0pet:show()
else
p0pet:hide()
end
end
frame_count = 0
windower.register_event('prerender',function()
if not windower.ffxi.get_info().logged_in then -- stops prerender if not logged in yet
return
end
if frame_count%30 == 0 and windower.ffxi.get_info().logged_in then
Update()
end
frame_count = frame_count + 1
end)