Data Realms Fan Forums
http://forums.datarealms.com/

A few simple lua questions I'd like to understand
http://forums.datarealms.com/viewtopic.php?f=73&t=29762
Page 1 of 1

Author:  ryry1237 [ Tue Jan 31, 2012 3:24 am ]
Post subject:  A few simple lua questions I'd like to understand

I found these lua snippets in Kettenkrad's Artillery L-GED mod and I want to understand them better.
Code:
function Create(self)
    print("C-4 detonated!");
    if c4ListD ~= nil then
   for i=1,#c4ListD do
       if MovableMan:IsParticle(c4ListD[i]) then
      c4ListD[i]:GibThis();
       end
   end
    end
    c4ListD = { };
end

Code:
function Create(self)
    print("C-4 planted!");
    if c4ListD == nil then
   c4ListD = { };
    end
    c4ListD[#c4ListD + 1] = self;
   
   self.LTimer = Timer();
end


They seem fairly simple, but there are a few variables I don't understand and which I failed to find in the wiki.

1. c4ListD - I assume that "c4" can be replaced by any other object in the game, but what does "ListD" represent? I also assume that "#c4ListD" just turns the object into a number?

2. i=1 - What does "i" represent? It seems to be more than just a randomly defined value, since I remember seeing lua scripts using "i" quite a lot.

3. c4ListD[i] - so if "i=1", this is the same as "c4ListD[1]"?

4. c4ListD = { } - What do the empty brackets represent?

5. Is there any functional purpose in the line"
Code:
    print("C-4 detonated!")

other than printing out a line which says "C-4 detonated!"?
(I mean, if I get rid of this line, will anything happen to the script that will make it unable to run properly?)

Thanks for any help.

Author:  Asklar [ Tue Jan 31, 2012 5:19 am ]
Post subject:  Re: A few simple lua questions I'd like to understand

1. c4ListD is just a name, so you can change any part of it, including the ListD part.

2. i is just a variable, you can change it to any other letter or a word. However, i is the most used variable in For Loops, so generally when you see an i there should be a For Loop nearby.

3. Yep.

4. Empty brackets are for defining a table. There isn't anything inside the brackets, so the table is empty. Now, coming back to point number 3, c4ListD[i] is a variable related to the table c4ListD. When you add the [] with a number inside after the name of a table, you'll refer to whatever is stored in that number of the table.
Everytime you throw a C4, it gets registered inside the table, and in the For Loop you make everything listed on the table get gibbed, because the i of c4ListD[i] is a number that gets +1 everytime the loop is played, and the loop is played #c4ListD times, being #c4ListD the total amount of variables stored in the table.

5. Nothing really aside from debugging, deleting it won't do anything.

Author:  weegee [ Wed Feb 01, 2012 7:10 am ]
Post subject:  Re: A few simple lua questions I'd like to understand

#c4ListD = length of c4ListD. If you have 4 items in c4ListD then # will return 4.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/