Quick question of dofile.

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Quick question of dofile.

Post by DiabloTerrorGF »

Ok, is this correct:

a.lua code is

Code: Select all

a=1200
dofile("b.lua")
c=200
b.lua

Code: Select all

a=100
So, when a.lua is ran, it would act like this:

Code: Select all

a=1200
a=100
c=200
The ending variables would be
a=100
c=200 in a.lua right... and in b.lua, 'a' was not nil, but 1200 before 100 was set.

Exactly like that right?
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Post by DiabloTerrorGF »

Also, would this garbage collect 'x'

Code: Select all

x=100

b=x

x=nil
? Is b set to x still, or what x was? would the variable x be completely gone from code? When does LUA GC?

Edit: I know 'b' will return 100, but is it still linked x?

Edit 2: There should be a way to get rid of stuff in memory manually. Not by calling that function, but when using multiple LUA scripts.

I know you arn't supposed to have many files, but it's hard for us Java programmers... I would like to get a point, and then do a whoe new LUA script, without the last one in memory, like a newfile() or something...

Would make title screens and such a lot easier in my opinion.
CheChin
Posts: 8
Joined: Thu Aug 04, 2005 10:51 am

Post by CheChin »

yeah, it's like you said:

dofile() kind of inserts the script inside the script.

to make it not reuse the same vars, maybe you can use "local"

like:

Code: Select all

a = 10

function blabla()
    local a = 5
    print(a)
end

blabla()
print(a)
would print:
5
10

correct me if i'm wrong
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Post by DiabloTerrorGF »

Not exactly what I was wondering about but thanks for the input.
User avatar
JoshDB
Posts: 87
Joined: Wed Oct 05, 2005 3:54 am

Post by JoshDB »

LUA runs line-to-line.

x=10
b=x
x=100
c=b*x
x=10000
dofile("somefile.lua")

Code: Select all

x=nil
x=nil
b=10
c=1000

Also, there's no need to garbage collect. I asked this a while ago. Somebody told me it does it automatically. I don't understand how... Ask someone else.
glynnder
Posts: 35
Joined: Sun Sep 04, 2005 9:54 pm

Post by glynnder »

i thinks its cos if u dont use the variable there's no real need to clear it...
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

When you write "x = 10" think of it as you put the number 10 to a global hashmap with the key "x". The number 10 is not an object, so "b=x" creates a new entry in this global hashmap. "x=nil" just overwrites the memory in the hashmap with the nil value, so "b" has still the value 10. But if you write "x={10};b=x;b[1]=2", then x[1] has the value 2, because {10} creates an object and saves the reference to x, then a reference to the same object is stored as b in the hashmap.

Now consider this: "x={10};x=nil". First a reference to an object is stored with the key "x" in the global hashmap. Then the variable is set to nil. This means the object is not accessible any more and it is GC'ed by Lua (the GC test for all variables is executed most of the time when new objects are allocated). So this answers your next question: You can "delete" objects, when you no longer reference it, e.g. store anything in a global table "level" and set this variable to nil, if you don't need the objects in it any more. And use local variables in functions, to avoid cluttering the global hashtable with uneeded objects, because they are not GC'ed, if still accessible with a name from the global hashtable.
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Post by DiabloTerrorGF »

But if you write "x={10};b=x;b[1]=2", then x[1] has the value 2, because {10} creates an object and saves the reference to x, then a reference to the same object is stored as b in the hashmap.
Is there a way to copy the contents of x into b, not have b linked to x?

I know you could do it manually, but I was just wondering if there something you can add to make it just copy the data(?) I guess.

Like(Does LUA start at 0 for arrays? I'm going to pretend it does for the example):

x={3}
x[0]=1
x[1]=2
x[2]=3

b=x

--b[1] is 2, etc.

Now I change

b[1]=0, but I still want x[1] to = 2.

So my result would be

x[0] 1
x[1] 2
x[2] 3

b[0] 1
b[0] 0
b[0] 3

Is there a way without setting b={3} and such and copying each individual field?

Sorry, this "carrying over" stuff annoys me...
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

You need to write your own function to copy the contents of a table to a new table. The code is given:

Code: Select all

function copy(x)
  local y = {}
  for i = 1, table.getn(x) do
    y[i] = x[i]
  end
  return y
end
And use the calling: b = copy(x)

Note: Lua prefers to start the table index with a "1", not "0".
Geo Massar
Retired Engineer
DiabloTerrorGF
Posts: 64
Joined: Fri Jul 15, 2005 11:44 pm

Post by DiabloTerrorGF »

table? Same thing as an array?
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

yes and no

yes -- a table can act like an array as long as indices are used. Eg, x[1] = 1234, etc.

no -- a table can act like a dictionary, that's, it contains pairs of key and value.

It would be more helpful if you read/study a book called "Programming in Lua". It can be found at www.lua.org.

BTW, enjoy the book. :)
Geo Massar
Retired Engineer
Post Reply