View unanswered posts | View active topics It is currently Sat Nov 17, 2018 4:37 pm



Reply to topic  [ 3 posts ] 
 A few simple lua questions I'd like to understand 
Author Message
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post 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.


Tue Jan 31, 2012 3:24 am
Profile
Data Realms Elite
Data Realms Elite
User avatar

Joined: Fri Jan 07, 2011 8:01 am
Posts: 6211
Location: In your office, earning your salary.
Reply with quote
Post 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.


Tue Jan 31, 2012 5:19 am
Profile
DRL Developer
DRL Developer
User avatar

Joined: Thu Jun 11, 2009 2:34 pm
Posts: 959
Reply with quote
Post 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.


Wed Feb 01, 2012 7:10 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.018s | 15 Queries | GZIP : Off ]