forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Quick question of dofile.

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
DiabloTerrorGF



Joined: 15 Jul 2005
Posts: 64

PostPosted: Mon Mar 27, 2006 12:41 pm    Post subject: Quick question of dofile. Reply with quote

Ok, is this correct:

a.lua code is
Code:
a=1200
dofile("b.lua")
c=200


b.lua
Code:
a=100


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

Code:
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?
Back to top
View user's profile Send private message
DiabloTerrorGF



Joined: 15 Jul 2005
Posts: 64

PostPosted: Mon Mar 27, 2006 12:42 pm    Post subject: Reply with quote

Also, would this garbage collect 'x'

Code:
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.
Back to top
View user's profile Send private message
CheChin



Joined: 04 Aug 2005
Posts: 8

PostPosted: Tue Mar 28, 2006 9:51 am    Post subject: Reply with quote

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:
a = 10

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

blabla()
print(a)


would print:
5
10

correct me if i'm wrong
Back to top
View user's profile Send private message
DiabloTerrorGF



Joined: 15 Jul 2005
Posts: 64

PostPosted: Tue Mar 28, 2006 10:33 am    Post subject: Reply with quote

Not exactly what I was wondering about but thanks for the input.
Back to top
View user's profile Send private message
JoshDB



Joined: 05 Oct 2005
Posts: 87

PostPosted: Tue Mar 28, 2006 11:00 am    Post subject: Reply with quote

LUA runs line-to-line.

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

Code:
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.
Back to top
View user's profile Send private message Send e-mail AIM Address
glynnder



Joined: 04 Sep 2005
Posts: 35

PostPosted: Mon Apr 03, 2006 10:21 pm    Post subject: Reply with quote

i thinks its cos if u dont use the variable there's no real need to clear it...
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Mon Apr 03, 2006 11:06 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
DiabloTerrorGF



Joined: 15 Jul 2005
Posts: 64

PostPosted: Tue Apr 04, 2006 3:14 pm    Post subject: Reply with quote

Quote:
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...
Back to top
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Wed Apr 05, 2006 1:53 am    Post subject: Reply with quote

You need to write your own function to copy the contents of a table to a new table. The code is given:
Code:
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
Back to top
View user's profile Send private message Send e-mail
DiabloTerrorGF



Joined: 15 Jul 2005
Posts: 64

PostPosted: Wed Apr 05, 2006 6:41 am    Post subject: Reply with quote

table? Same thing as an array?
Back to top
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Wed Apr 05, 2006 7:24 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group