You are not logged in.

Dear visitor, welcome to Runes of Magic EU. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

41

Monday, August 3rd 2009, 7:08pm

there is error

Source code

1
unexpected symbol near '+'
at line

Source code

1
num = index or num++;
i think lua don't know operations like ++... :)


"num++" is equivalent "num = num + 1"... right?

what symbol _ mean in line

Source code

1
2
3
...
for index,_ in ipairs(timers)do
...



can you explain how ipairs work?

i found disciption:

Source code

1
2
3
4
5
[B]ipairs (t)[/B]

    Returns three values: an iterator function, the table t, and 0, so that the construction  
     for i,v in ipairs(t) do [I]body[/I] end
 will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table.



but i don't understand - what is i,v here...

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

42

Monday, August 3rd 2009, 8:00pm

ok, forgot that lua doesnt know ++ operators :P then just replace ++ with "num+1" :-)

each table consists of pairs of values, e.g.

talbe = { alt = new, whatever = xyz, };

the iterator construction ipairs(table) goes pair by pair through the table.

in pairs(table) is for non-arrays, which means they dont use a numeric index then

in ipairs is for arrays with numeric index (e.g. table = { alt, new , whatever} => table[1] = alt, table[2] = new, etc... [forgot whether in lua they start with 0 or 1...])

now for i,k in ipairs(table) means that the whole table will be walked through from the beginning (starting at the lowest index) and "i" will hold the index, "k" will hold the value whic his the same as "k = table"
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

43

Monday, August 3rd 2009, 8:19pm

thx for explanation ipairs...


i tryed 2 types of addTimer:

1)

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
local timer = {};
        local num;
        [B]local num2;[/B]
        DEFAULT_CHAT_FRAME:AddMessage("adding new timer");
        timer.remainingTime = countDownValue;
        timer.table = callBackTable;
        timer.func = callBackFunction;
        timer.values = callBackValues;
        for index,_ in ipairs(timers)do
            [B]num2 = num2 + 1;[/B]
            num = index or [B]num2;[/B]
            if(index == nil)then
                  timers[num] = timer;
                  DEFAULT_CHAT_FRAME:AddMessage("timer № "..num.." created");
                  break;
            end;
        end;
        return num; --index of the Timer for later reuse(remove, change, whatever)

...
2)

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
local timer = {};
        local num;
        DEFAULT_CHAT_FRAME:AddMessage("adding new timer");
        timer.remainingTime = countDownValue;
        timer.table = callBackTable;
        timer.func = callBackFunction;
        timer.values = callBackValues;
        for index,_ in ipairs(timers)do
            num = index or [B]num + 1;[/B]
            if(index == nil)then
                  timers[num] = timer;
                  DEFAULT_CHAT_FRAME:AddMessage("timer № "..num.." created");
                  break;
            end;
        end;
        return num; --index of the Timer for later reuse(remove, change, whatever)
...
results are the same: i only appear message "adding new timer"... i think it's becouse timers hasn't any element and ipairs doesn't start

how get number of elements in table?




i think another problem: timer 2 ignors if timer 1 removed becouse

Source code

1
2
3
...
for index,timer in ipairs(timers)do
...
when there isn't timers[1]... timers[2] will be ignored, right? maybe better use pairs then?

44

Monday, August 3rd 2009, 9:08pm

after testing some lua rules:happydance:

i did like this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
countDown = function(self,elapsedTime)
        for index,timer in [size=10][B]pairs[/B][/SIZE](timers)do
            timer.remainingTime = timer.remainingTime - elapsedTime;
            if(timer.remainingTime <= 0)then
                timer.func(timer.table,timer.values);
                self:removeTimer(index);
            end;
        end;
    end;
    
    addTimer = function(self,countDownValue,callBackTable,callBackFunction,callBackValues)
        local timer = {};
        local num;
        local Add = true;--[B]it's a flag for adding first timer or not[/B]
        timer.remainingTime = countDownValue;
        timer.table = callBackTable;
        timer.func = callBackFunction;
        timer.values = callBackValues;
        for index,_ in ipairs(timers)do--[B]checking if any timers already exist[/B]
            Add = false;--[B]if any timer exist then setting flag false, to make additional timer...[/B]
            num = index + 1;--[B]...and getting first missing index in our table timers[/B]
        end;
        if (Add) then--[B]this way if it's first timer[/B]
            num = 1;
            timers[num] = timer;
        else--[B]this way if it's not first timer[/B]
            timers[num] = timer;
        end;
        return num; --index of the Timer for later reuse(remove, change, whatever)
    end;
...
so it work :woot:

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

45

Monday, August 3rd 2009, 11:40pm

I rewrote it before I read your last post, this way it has to work too, and it should be a bit more flexible :P

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local timers = {};
local timerNum = 0;

local HelloWorld = {
    countDown = function(self,elapsedTime)
        for index,timer in ipairs(timers)do
            timer.remainingTime = timer.remainingTime - elapsedTime;
            if(timer.remainingTime <= 0)then
                timer.func(timer.table,timer.values);
                self:removeTimer(index);
            end;
        end;
    end;
    
    addTimer = function(self,countDownValue,callBackTable,callBackFunction,callBackValues)
        local timer = {};
        timerNum = timerNum+1;
        timer.remainingTime = countDownValue;
        timer.table = callBackTable;
        timer.func = callBackFunction;
        timer.values = callBackValues;
        timer.ID = timerNum;
        timers:insert(timer);
        return timer.ID; --ID of the Timer for later reuse(remove, change, whatever)
    end;
    
    removeTimer = function(self,ID)
        for index,timer in ipairs(timers)do
            if(timer.ID == ID)then
                timers:remove(index);
                return true;
            end;
        end;
        return false; -- timer doesnt exist(anymore)
    end;
    
    changeTimer = function(self,ID,countDownValue,callBackTable,callBackFunction,callBackValues)
        for index,timer in ipairs(timers)do
            if(timer.ID == ID)then
                timer.remainingTime = countDownValue or timer.remainingTime;    --change value if its != nil/0
                timer.table = callBackTable or timer.table;    --change value if its != nil/0
                timer.func = callBackFunction or timer.func;    --change value if its != nil/0
                timer.values = callBackvalues or timer.values;    --change value if its != nil/0
                return true;
            end;
        end;
        return false;        
    end;
}
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

46

Tuesday, August 4th 2009, 7:28am

but it will have problem with timer ignoring, becouse

Source code

1
for index,timer in [U][B]ipairs[/B][/U](timers)do
if we add 2 timers, for 4 second and 8 - second timer will be ignored after first removed, becouse ipairs will find that timers[1] = nil and will not start


but thx anyway ;)

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

47

Tuesday, August 4th 2009, 9:43am

no it wont have problems xDD

just test it :-)

timers:remove(index) will shift up all following table-members, so there wont be any space left.
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

48

Wednesday, August 5th 2009, 2:15pm

to Sl4ker, i'm confused... look at this code

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
function HelloWorld.SayHello()
        local b = {};--declared an array
        local k = {};--declared an array
        local i;--declared integer for loop
        k.val = 1;--set value of field "val" = 1
        b[1] = k;--storing value of field "val" = 1 for element [1]
        k.val = 2;--set value of field "val" = 2
        b[2] = k;--storing value of field "val" = 2 for element [2]
        for i = 1, 2 do
            k = b[i];--loading values of b[i] into k
            DEFAULT_CHAT_FRAME:AddMessage(k.val]);--displaying value of field "val"
        end;
    end;
DEFAULT_CHAT_FRAME:AddMessage will add in chat

Source code

1
2
2
2
why are the values of fields "val" the same for 2 different elements in array?

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

49

Wednesday, August 5th 2009, 2:37pm

Quoted from "HappyHorror;890755"

to Sl4ker, i'm confused... look at this code

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
function HelloWorld.SayHello()
        local b = {};--declared an array
        local k = {};--declared an array
        local i;--declared integer for loop
        k.val = 1;--set value of field "val" = 1
        b[1] = k;--storing value of field "val" = 1 for element [1]
        k.val = 2;--set value of field "val" = 2
        b[2] = k;--storing value of field "val" = 2 for element [2]
        for i = 1, 2 do
            k = b[i];--loading values of b[i] into k
            DEFAULT_CHAT_FRAME:AddMessage(k.val]);--displaying value of field "val"
        end;
    end;
DEFAULT_CHAT_FRAME:AddMessage will add in chat

Source code

1
2
2
2
why are the values of fields "val" the same for 2 different elements in array?

thats easy :-)

ill try to explain it really simple :P

Source code

1
2
3
4
function HelloWorld.SayHello()
        local b = {};--declared an array
        local k = {};--declared an array
        local i;--declared integer for loop
both, b and k are references to EMPTY TABLES here.
this means, these two newly declared tables have no members and no references except "b" and "k".

Source code

1
2
        k.val = 1;--set value of field "val" = 1
        b[1] = k;--storing value of field "val" = 1 for element [1]
the values of "k" are now like followed:

Source code

1
2
3
k = {
   ['val'] = 1,
};
the values of "b":

Source code

1
2
3
4
5
b = {
   [1] = {
           ['val'] = 1,
       },
};
"b[1]" has a reference to "k" now. you didnt copy the values, you copied the reference !

in lua, "table1 = table2" doesnt copy a table, they both refer to the same table after this operation.

Source code

1
2
3
4
5
6
table1 = {};
table2 = {};
table1.a = 2;
table2 = table1;
table1.a = 4;
DEFAULT_CHAT_FRAME:AddMessage(table2.a);
will result in:

Source code

1
4
that being said and explained, lets have a look at your next operations:

Source code

1
        k.val = 2;--set value of field "val" = 2
you just changed "b[1]['val']" to 2 too ;-)

Source code

1
        b[2] = k;--storing value of field "val" = 2 for element [2]
b[1] is now exactly the same like b[2], they both refer to the same table.

Source code

1
2
        for i = 1, 2 do
            k = b[i];--loading values of b[i] into k
because both, "b[1]" and "b[2]" refer to the table "k", this is exactly the same like stating "k = k";

Source code

1
2
3
            DEFAULT_CHAT_FRAME:AddMessage(k.val]);--displaying value of field "val"
        end;
    end;
you probably know right now, why you get "2" displayed all the time :-)


if you want to store the value of k.val into b[1]['val'] you have to type:

Source code

1
b[1]['val'] = k['val'];
but this only works while k['val'] is no table !
if k['val'] is a table too, you would only create a new reference...

if you want to copy a whole table (or a subtable of a greater table) pair by pair, you have to write your own tableCopy() and tableClone() functions, like e.g. the one in ikarus.utils :-)

gl/hf
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

50

Thursday, August 6th 2009, 10:01am

thx for explanation :bow:


one more offtop question:


to load any saved variables i should do in my xml file:

Source code

1
2
3
4
5
...
<OnLoad>
SaveVariables("Variable Name");
</OnLoad>
...
but <OnLoad> will be called only 1 time at load? is there another way to load variables? like - if you press any button on frame or calling function...

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

51

Thursday, August 6th 2009, 3:31pm

no there is no other way... but you wont need another way to load :P

you can have presets etc in *.lua files which you can load dynamically... but user saved variables are only loadable at <OnLoad> and available after Event VariablesLoaded (or similar)
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

52

Thursday, August 6th 2009, 4:40pm

hm...

so maybe it possible then save variable as element of array?

like

Source code

1
2
3
TotalM = 1;
Members[TotalM] = "Sam";
SaveVariables("Members[TotalM]");


i tryed, but it doesn't work, it even don't save it as Members[TotalM] = "Sam"

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

53

Thursday, August 6th 2009, 4:49pm

well. the saved variables are tables!

so this works :

Source code

1
2
3
4
5
6
7
8
9
MySavedVariables = {
                   ['value1'] = whatever,
                   ['value2'] = {
                                      ['something'] = xyz,
                   },
                   ['value3'] = thisorthat,
};

SaveVariables("MySavedVariables");
the string you pass to "SaveVariables" must be the NAME of the table/variable to save... and "Members[TotalM]" is no name :P
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

54

Thursday, August 6th 2009, 4:52pm

hm, very interesting, but what should i do, if i don't know how many fields i need? (becouse new members come with time and with array i just make new element)

is it possible to make field name with string concatenation?

Source code

1
2
3
4
5
6
MySavedVariables = {
                   ['value1'..'some custom text'] = whatever,

};

SaveVariables("MySavedVariables");

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

55

Thursday, August 6th 2009, 4:56pm

sure....

THE WHOLE TABLE will be saved (except functions):-)
so the current state of the table, including all members and fields will be saved, as long as the type of the saving value is either number, string or table :-)
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

56

Thursday, August 6th 2009, 5:00pm

how read table field right?

Source code

1
2
3
4
5
6
MySavedVariables = {
                   ['value1'..'ss'] = whatever,

};

DEFAULT_CHAT_FRAME:AddMessage(MySavedVariables.['value1some custom text']);
?

found, MySavedVariables['value1ss'] :)

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

57

Thursday, August 6th 2009, 6:25pm

naaaa xD

Source code

1
2
local whatsoever = "haha"
MySavedVariable['xyzblablub'..whatsoever..'blibalbo'] = x
is the same as:

Source code

1
MySavedVariable.xyzblablubhahablibalbo = x;
dont mix these two types :P
dynamic names for the field can only be declared with the first method!
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

58

Thursday, August 6th 2009, 6:56pm

Quoted from "Sl4ker;892811"

naaaa xD
...
variable names for the field can only be declared with the first method!


you mean this?

Quoted from "Sl4ker;892679"

well. the saved variables are tables!

...

Source code

1
2
3
4
5
6
7
8
9
MySavedVariables = {
                   ['value1'] = whatever,
                   ['value2'] = {
                                      ['something'] = xyz,
                   },
                   ['value3'] = thisorthat,
};

SaveVariables("MySavedVariables");
...

Sl4ker

Intermediate

Posts: 1,985

Occupation: Ex- RoM-Zocker

  • Send private message

59

Thursday, August 6th 2009, 7:17pm

what do you mean with this last post?:>
[Temeraire] - [Ikarus] - [AddOn-FAQ]
My english is not the yellow from the egg :-P

60

Thursday, August 6th 2009, 7:20pm

don't worry, nothing... tested - all work fine ;)