PDA

View Full Version : for loop does not run through the last item



Fenloh
May 23rd, 2010, 17:48
I have a for loop that gets the windows of a windowlist. it works fine when the function is called upon due to a onValueChanged event.

I have added an onInit()-super.onInit() that calls the event. when the onInit calls the function the last windowlist Item is not being processed, while the onValueChanged runns through it correctly.

Any Ideas why that happens? Here is the code.

Fenloh
Example:
Armor1 (1) (2) (active)
Armor2 (2) (2) (inactive)
Armor3 (10) (11) (active)

result after onValueChanged(): (11) (13)

Result after onInit(); (1) (2)

If an empty armoritem would follow, then the result would be correct.


function onInit()
super.onInit();
window.windowlist.addArmorvalue();
end

function onValueChanged()
window.windowlist.addArmorvalue();
end




function addArmorvalue()
local balisticvalue = 0
local impactvalue = 0

for k, v in ipairs(getWindows()) do
local armorisactive = v.getDatabaseNode().getChild("armorisactive").getValue();
if armorisactive == 1 then
balisticvalue = balisticvalue + v.getDatabaseNode().getChild("armor.balistic").getValue();
impactvalue = impactvalue + v.getDatabaseNode().getChild("armor.impact").getValue();
end
end
window.getDatabaseNode().getChild("armor.hiddenbalisticvalue").setValue(balisticvalue);
window.getDatabaseNode().getChild("armor.hiddenimpactvalue").setValue(impactvalue);
end

Moon Wizard
May 23rd, 2010, 21:02
Try using pairs(), not ipairs().

The ipairs iterator assumes that all of the keys used in the list are in order 1,2,3,... with no breaks in number ordering. Once it encounters a number key with no table entry, it assumes it is done.

Cheers,
JPG

Fenloh
May 23rd, 2010, 21:46
Thanks,

I thought so too and tested it, but it has the same effect. I also checked the Database and there is no gap inbetween the data. It is also only the last key. it seems not to finish the loop somehow. The strange thing is, that it works with the onValueChanged() just fine.

I am not sure if the first loop goes astray or the last one.

Fenloh

Moon Wizard
May 24th, 2010, 01:16
Based on what you've provided, I would assume that the getWindows() call is not returning all the windows you are expecting in the onInit function.

You should try adding some debug statements to send messages to the chat window to see how many windows are being returned by getWindows() and what the database names are for those windows.

Cheers,
JPG

Fenloh
May 24th, 2010, 07:01
The problem seems to be the Iconcycler template. The Code that is used, including Nodemanager is the Code from the DnD4 Ruleset (the new one)

it seems, that there is some kind of unwanted loop there.

after this line:
for k, v in ipairs(getWindows()) do
I added a print(v.getDatabaseNode().getNodeName());

the result is for 5 Items
id 1
id 1,id 2
id 1,id 2,id 3
id 1,id 2,id 3,id 4

I was not able to pinpoint the problem so far.

Fenloh

Moon Wizard
May 25th, 2010, 00:03
I'm not sure how iconcycler is related to windowlist. Can you provide an example?

Thanks,
JPG

Fenloh
May 25th, 2010, 06:06
Well sometimes it is just my own creativity that stands in the way.

Instead of calling the addValueArmor event onInit of the windowlist, as i should have, I called it from the windowinstance.

Sorry for the stupid question...

Fenloh