Advertising (This ad goes away for registered users. You can Login or Register)

[LUA] Need help with code plz

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Locked
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

Like this?

Code: Select all

imgdir = System.listDirectory("/images")
Image = {}
Image[1] = {}
Image[2] = {}
Image[3] = {}
Image[4] = {}
Image[5] = {}
Image[6] = {}
Image[7] = {}
Image[8] = {}
Image[9] = {}
Image[10] = {}

pics = {}
Picture 1
Picture 2
Picture 3
Picture 4
Picture 5
Picture 6
Picture 7
Picture 8 
Picture 9
Picture 10

for i = 1, table.getn(imgdir) do
  if string.lower(string.sub(imgdir[i].name, -4)) == ".png" then
  table.insert(pics, imgdir[i])
  end
end
--this is one way to filter the array for the images only, you can do that for BMP and whatever images your Lua interpreter supports.

counter = 0
currentpic = 1

while true do
counter = counter + 1
  if counter > 10 then
  counter = 0
  picture = Image.load("/images" .. pics[Picture 1].name)
  currentpic = currentpic + 1
    if currentpic > table.getn(pics) then
    currentpic = 1
    end
  end
screen:clear()
  if picture then
  screen:blit(0, 0, picture)
  end
screen.waitVblankStart()
screen.flip()
end
Advertising
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [LUA] Need help with code plz

Post by qwikrazor87 »

If you run the code right now you are going to run into errors with Picture # near the top of your code, it'll probably say something like "Error: 15: '=' expected near Picture". When you declare a value holder, function, etc., it should not contain any spaces, otherwise you will keep getting errors.
If you meant for Picture to be a table that holds the values from 1 to 10 then you can do this,

Code: Select all

Picture = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
or you can automate the task and do this,

Code: Select all

Picure = {}
for i = 1, 10 do
  Picture[i] = i
end
Placing currentpic inside imgdir will do the job.
Another problem is the path you specified in Image.load,

Code: Select all

picture = Image.load("/images" .. pics[Picture 1].name)
you need a slash at the end of "/images" otherwise the string that is passed will be for example

Code: Select all

ms0:/PSP/GAME/LPE8/imagespicture1.png
placing a slash will fix the problem and make it for example

Code: Select all

ms0:/PSP/GAME/LPE8/images/picture1.png
Advertising
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

C:\Users\"my name"\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:16: attempt to index global `Picture' (a nil value)

Doesn't that mean I have to replace i with 1?

Code: Select all

pics = {}
for i = 1, 10 do
  Picture[i] = i
end

Code: Select all

imgdir = System.listDirectory("/images/")
Image = {}
Image[1] = {}
Image[2] = {}
Image[3] = {}
Image[4] = {}
Image[5] = {}
Image[6] = {}
Image[7] = {}
Image[8] = {}
Image[9] = {}
Image[10] = {}

pics = {}
for i = 1, 10 do
  Picture[i] = i
end

for i = 1, table.getn(imgdir) do
  if string.lower(string.sub(imgdir[i].name, -4)) == ".png" then
  table.insert(pics, imgdir[i])
  end
end

counter = 0
currentpic = 1

while true do
counter = counter + 1
  if counter > 10 then
  counter = 0
  picture = Image.load("/images/" .. pics[Picture 1].png)
  currentpic = currentpic + 1
    if currentpic > table.getn(pics) then
    currentpic = 1
    end
  end
screen:clear()
  if picture then
  screen:blit(0, 0, picture)
  end
screen.waitVblankStart()
screen.flip()
end
Different55
Posts: 130
Joined: Thu Jun 07, 2012 9:28 pm

Re: [LUA] Need help with code plz

Post by Different55 »

dragonxtamer596 wrote:C:\Users\"my name"\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:16: attempt to index global `Picture' (a nil value)

Doesn't that mean I have to replace i with 1?
no, it means you have to declare Picture as an array.

Code: Select all

pics = {}
for i = 1, 10 do
  Picture[i] = i
end
You declared pics as an array, then went and tried to use Picture as an array, so:

Code: Select all

Picture = {}
for i = 1, 10 do
  Picture[i] = i
end
would solve this error.
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

C:\Users\""\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:19: bad argument #1 to `getn' (table expected, got nil)

Do I switch the imgdir to Image because it's the table?
If I switch that I get something like...

C:\Users\""\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:20: bad argument #1 to `sub' (string expected, got nil)

Code: Select all

imgdir = System.listDirectory("/images/")
Image = {}
Image[1] = {}
Image[2] = {}
Image[3] = {}
Image[4] = {}
Image[5] = {}
Image[6] = {}
Image[7] = {}
Image[8] = {}
Image[9] = {}
Image[10] = {}

Picture = {}
for i = 1, 10 do
  Picture[i] = i
end

for i = 1, table.getn(imgdir) do
  if string.lower(string.sub(imgdir[i].name, -4)) == ".png" then
  table.insert(pics, imgdir[i])
  end
end

counter = 0
currentpic = 1

while true do
counter = counter + 1
  if counter > 10 then
  counter = 0
  picture = Image.load("/images/" .. pics[Picture1].png)
  currentpic = currentpic + 1
    if currentpic > table.getn(pics) then
    currentpic = 1
    end
  end
screen:clear()
  if picture then
  screen:blit(0, 0, picture)
  end
screen.waitVblankStart()
screen.flip()
end
Forum rules
Any post not directly related to programming will be moderated.
Do not request people to code something for you.
Avoid posting messages that do not bring anything to the conversation. We want the threads in this subforum to stay focused.
I kept thinking about this and i'm sure i broke second one opps....
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [LUA] Need help with code plz

Post by qwikrazor87 »

The problem lies in System.listDirectory, remove both of the slashes and it should work.
You shouldn't use Image as a table since it's already in use by the Lua player. When you declare Image as a table it will erase all the Image functions, so it's best to use something else.
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

C:\Users\""\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:32: attempt to index field `?' (a number value)

It's this line \/ but why an ? thats not even there. :?

Code: Select all

picture = Image.load("/images/" .. Picture[currentpic].png)

Code: Select all

imgdir = System.listDirectory("images")
LU = {}
LU[1] = {}
LU[2] = {}
LU[3] = {}
LU[4] = {}
LU[5] = {}
LU[6] = {}
LU[7] = {}
LU[8] = {}
LU[9] = {}
LU[10] = {}

Picture = {}
for i = 1, 10 do
  Picture[i] = i
end

for i = 1, table.getn(Image) do
  if string.lower(string.sub(Image[i].name, -4)) == ".png" then
  table.insert(Picture, imgdir[i])
  end
end

counter = 0
currentpic = 1

while true do
counter = counter + 1
  if counter > 10 then
  counter = 0
  picture = Image.load("/images/" .. Picture[currentpic].png)
  currentpic = currentpic + 1
    if currentpic > table.getn(Picture) then
    currentpic = 1
    end
  end
screen:clear()
  if picture then
  screen:blit(0, 0, picture)
  end
screen.waitVblankStart()
screen.flip()
end
I also have another question why when I look at peoples lua code it repeats several times( a table in the loop would work) of is jumbled in one strait line?
Different55
Posts: 130
Joined: Thu Jun 07, 2012 9:28 pm

Re: [LUA] Need help with code plz

Post by Different55 »

dragonxtamer596 wrote:C:\Users\""\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:32: attempt to index field `?' (a number value)
picture = Image.load("/images/" .. Picture[currentpic].png)

lua thinks that .png is supposed to be a variable or something so it throws an error. You need another .. and some quotes:

picture = Image.load("/images/" .. Picture[currentpic] .. ".png")
Different55
Posts: 130
Joined: Thu Jun 07, 2012 9:28 pm

Re: [LUA] Need help with code plz

Post by Different55 »

dragonxtamer596 wrote:C:\Users\""\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:32: attempt to index field `?' (a number value)
picture = Image.load("/images/" .. Picture[currentpic].png)

lua thinks that .png is supposed to be a variable or something, but it isn't and it gives you an error. You need another .. and some quotes:

picture = Image.load("/images/" .. Picture[currentpic] .. ".png")
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

Different55 wrote:
dragonxtamer596 wrote:C:\Users\""\Desktop\luaplayerwindows>luaplayer script.lua
error: script.lua:32: attempt to index field `?' (a number value)
picture = Image.load("/images/" .. Picture[currentpic].png)

lua thinks that .png is supposed to be a variable or something, but it isn't and it gives you an error. You need another .. and some quotes:

picture = Image.load("/images/" .. Picture[currentpic] .. ".png")
Thanks different55 and qwikrazor87.
Locked

Return to “Programming and Security”