Lua Programming Help Thread
This is a discussion on Lua Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; could i see the section of code that that part is in, and the table that the data is being ...
-
04-24-2008, 02:13 PM #8521Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
could i see the section of code that that part is in, and the table that the data is being loaded from again, as it is in the script
------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
04-24-2008, 02:32 PM #8522QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
Okay.
That's the heart of it. It's taken from throughout the entire script but hopefully it makes sense. I can give you the whole thing if you want, but it's probably not necessary.Code:locations = {} while true do pad = Controls.read() screen:clear() dx = pad:analogX() dy = pad:analogY() if gamestate == "game" then if locations ~= nil then for i = 1, table.getn(locations) do screen:blit(locations[i].x,locations[i].y,dot) end end elseif gamestate == "menu" then pad = Controls.read() --if user is loading... took that out for this example if pad:square() and not oldpad:square() then System.usbDiskModeDeactivate() loadFile = io.open("Saved Sets/" .. loadFile2 .. ".txt","r") number = 0 for line in loadFile:lines() do number = number + 1 end for j = 1, math.floor(number/2)*2, 2 do xc = loadFile:read() yc = loadFile:read() table.insert(locations,{x=xc,y=yc}) end loadFile:close() System.usbDiskModeActivate() gamestate = "game" pausestate = "select" end end end
-
04-24-2008, 02:38 PM #8523Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
hmm you know this part
could you check for me by printing the data to the screen before continuing what these are, because i think that what you are doing is thisCode:xc = loadFile:read() yc = loadFile:read()
so calling a screen:blit() with those valuse it is causing the loop in gettable error where 4, 10 is the valueCode:xc = 4, 10 yc = 4, 10
also try using the code that i made for you it does work, it loads and saves files easier, and you wouldnt get that error
if what i said is the case then it would be easier to use the code that i made that i know works, than to recode what you have to work, because it isnt the right way to load data from a table anyway, not saying mine is the right way but it is better than the current method that you are using
thanks------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
04-24-2008, 03:25 PM #8524QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
Printing them told me that they were nil. But I used the way you showed me a few posts ago, and it worked like a charm. Thanks a lot!
-
04-24-2008, 03:36 PM #8525QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Zitat von andyauff
You are still not reading the file correctly.
loadFile:read() is returning "x,y", correct? (where x and y are the co-ordinates in each case) So, you are basically stating:
Even if you stated:Code:xc = "x,y" yc = "x,y"
It still wouldn't work because Lua cannot convert "x,y" (A string) into a group of numbers.Code:xc,yc = "x,y"
You would need something like this:
But even that wouldn't work because you are just reading the the first line over and over, you need to move forward a line every time. So you would need something like this: (I'm not sure what the for loop looping through in your code, but this is general)Code:loc = loadFile:read() xc = string.sub(loc,1,string.find(loc,",") - 1) yc = string.sub(loc,string.find(loc,",") + 1)
EDIT: Oh, I see. You are iterating in groups of 2, so your files are just stored as a list of numbers. in that case:Code:if pad:square() and not oldpad:square() then for line in io.lines("Saved Sets/" .. loadFile2 .. ".txt") do table.insert(locations,{x=string.sub(line,1,string.find(line,",") - 1),y=string.sub(line,string.find(line,",") + 1)}) end System.usbDiskModeDeactivate() System.usbDiskModeActivate() gamestate = "game" pausestate = "select" end
Or if you are adding to an existing list:Code:local n = 0 for line in io.lines("Saved Sets/" .. loadFile2 .. ".txt") do n = n + 1 if math.floor(n / 2) * 2 == n then locations[n / 2 + 1].y = line else locations[(n - 1) / 2 + 1] = {x = line} end end
Code:local p = table.getn(locations) local n = 0 for line in io.lines("Saved Sets/" .. loadFile2 .. ".txt") do n = n + 1 if math.floor(n / 2) * 2 == n then locations[p + n / 2 + 1].y = line else locations[p + (n - 1) / 2 + 1] = {x = line} end endGeändert von Nielkie (04-24-2008 um 11:20 PM Uhr)
-
04-24-2008, 03:41 PM #8526Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
no problem, glad it worked
Zitat von andyauff
------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
04-24-2008, 04:00 PM #8527QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
I see what you mean, but that would be if I had the x and y values on the same line in the file. The way I had it was an x value on the first line, y on the second, x on the third, y on the fourth, etc. But I'm using a completely different method now, thanks to FaT3oYCG, so it's all good. But thanks :).
Zitat von Nielkie
-
04-24-2008, 04:09 PM #8528QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
I updated my post when I saw what the for loop was doing. Anyway, it's always nice to know the reason for a problem.
Zitat von andyauff
-
04-24-2008, 04:40 PM #8529QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
Okay, thanks Nielkie. Yes, I agree, it is certainly important to understand the problem.
Zitat von Nielkie
-
04-25-2008, 11:34 AM #8530
hey would anyone know how to get a decimal converted to a whole number ?
for example i get 88.3333333333333 but i want it to print 88
-
04-25-2008, 12:17 PM #8531QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
math.floor(83.33333333333 33)
I'm pretty sure that's it.
-
04-25-2008, 12:27 PM #8532
thanks andyauff :]]
-
04-25-2008, 12:53 PM #8533QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
No problem.
-
04-25-2008, 12:54 PM #8534Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
oops ill put this in in just a moment
EDIT: there you go
if the number after the decimal is smaller than 5 then it will round down like it would in propper maths and if it is 5 or above then it will round upCode:function round_number(number) check_number_full = string.format("%.1f", number) check_number_single = string.sub(check_number_full, -1) if check_number_single < 5 then return math.floor(number) else return math.ceil(number) end end
e.g. 5.27577 woud return 5 and 5.78435 would return 6------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
04-25-2008, 01:20 PM #8535QJ Gamer Blue
- Registriert seit
- Jan 2007
- Ort
- U.S.
- Beiträge
- 405
- Points
- 7.014
- Level
- 55
- Downloads
- 0
- Uploads
- 0
Yeah, I thought there was a function for rounding up too. That's why I said I was pretty sure. math.floor worked because you were rounding down. Thanks for adding that function, FaT3oYCG.
Geändert von andyauff (04-25-2008 um 01:43 PM Uhr)
-
04-25-2008, 01:21 PM #8536
thanks :]
Zitat von FaT3oYCG
the math.floor worked for me but its always good to see exactly how it works thanks
-
04-25-2008, 01:32 PM #8537Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
yeah i just though id put that in because it can be used all the time, ill explain the functions for you aswell
math.floor(number) - all this does is remove any decimals on the number after the point, to return the whole number
math.ceil(number) - all this does is add one to the number and then remove any decimals after the point, to return a whole number
i made that function because it rounds depending on the decimal not by just rounding up or down all of the time------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
04-25-2008, 03:11 PM #8538QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Wow. That is a horrible rounding function, and you explained what math.floor and ceil do incorrectly. For negative numbers, your explanation no longer holds true.
Also, nonsucky rounding function:
Code:function math.round(n) return math.floor(n+0.5) end
-
04-25-2008, 03:27 PM #8539QJ Gamer Bronze
- Registriert seit
- Mar 2007
- Beiträge
- 758
- Points
- 8.665
- Level
- 62
- Downloads
- 0
- Uploads
- 0
Doesn't math.floor round down to the nearest whole number and math.ceil round up?
-
04-25-2008, 03:42 PM #8540
yes but the way he just wrote it is that it adds .5 to any of ur decimals and rounds down... so if ur deciam is say 1.5 it would be 1.5 +.5 witch = 2 and 2 rounds to 2... if it were 1.3 then it would be 1.3 +.5 witch = 1.8 witch with math.floor would round to 1 so..it still works :P cool and simple rounding function :}]]
Zitat von michaelp
-
04-25-2008, 04:31 PM #8541QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
michaelp - yes. that would be what it does. please refer to above posts (although incorrect it still gets the point across) and http://lua-users.org/wiki/TutorialDirectory before asking more questions.
-
04-26-2008, 12:08 AM #8542Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
lol
everyday when im walking down the street, every turtle that i meet says, its a simple message and it comes from the shell, you help people and then i pwn you, hey!!!. every day when im ...
thanks lol
yeah my function sux, but i did make it a long time ago
oh and
math.floor (x)
Returns the largest integer smaller than or equal to x.
math.ceil (x)
Returns the smallest integer larger than or equal to x.Geändert von FaT3oYCG (04-26-2008 um 12:20 AM Uhr)
------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
05-01-2008, 02:49 PM #8543
CAn someone can show me how to use this function in he Lua Player HM.
System.usbDevUMD()
If I try it like that :
if pad:cross() then
System.usbDevUMD()
end
Nothing happen when i push X. CAn someone help me please.
-
05-01-2008, 02:51 PM #8544Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
System.usbDevUMD() sets the usb to the umd
System.diskModeActivate() still turns it on so
if pad:cross() then
System.usbDevUMD()
System.diskModeActivate()
end------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
05-01-2008, 02:57 PM #8545
Attemp to call field 'diskmodeActivate" (a nil value)
Do you know what does that means ?
-
05-01-2008, 02:58 PM #8546Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
sorry
System.usbDiskModeActivat e()------ FaT3oYCG -----
AKA Craig, call me what you want to It's your preference.
My Website: http://www.modern-gamer.co.uk/
Currently working on:
(0) MediaGrab
(0) PGE Gears Of War - On hold (Very large project).
(0) PS???? -On Hold A tactical 2d side scrolling game involving AI and online multiplayer features. - Tile engine nearley finished (1 bug to fix).
-
05-01-2008, 03:03 PM #8547
Thanks. For the fastest with anwser me. What abotu that one.
I'll want to creat a drum app. I wnt to load my sound and associate them to a name. Then I want to, if i push X, per exemple, play the sound associate to "kick" bu tonly once. Do u know how i have to do that.
I alerdy try but i wasn'n able.
-
05-01-2008, 04:13 PM #8548QJ Gamer Blue
- Registriert seit
- Apr 2008
- Beiträge
- 497
- Points
- 4.268
- Level
- 41
- Downloads
- 0
- Uploads
- 0
you need to put
oldpad = Controls.read()
at the top of your code by like the colors, or what not and put
if pad:cross() and oldpad:cross() ~= pad:cross() then
before the sound you want to be heard
and finally right before the end of your code put
oldpad=pad
that should work, Please correct me if I'm wrong Fat3oYCG
also I think your idea sounds cool, keep up the good work
-
05-01-2008, 07:46 PM #8549QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
PSProgrammer - that gets the job done, but personally I believe it is a horrible way to accomplish that job.
Replace:
with:Code:pad = Controls.read()
and then for button press detection, use:Code:oldpad = pad pad = Controls.read()
Basically, that will make the logic of it say, if cross has been pressed but it was not pressed last loop (aka it JUST got pushed down) then blahCode:if pad:cross() and not oldpad:cross() then
If you want to change that logic to, if cross was pressed down but just got released then blah, then do this:
Code:if oldpad:cross() and not pad:cross() then
-
05-01-2008, 10:01 PM #8550Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
TurtlesPwn - that gets the job done, but personally I believe it is a horrible way to accomplish that job.
Replace:
with:Code:Dpad = Controls.read()
and then for button press detection, use:Code:oldDpad = Dpad Dpad = Controls.read()
Basically, that will make the logic of it say, if cross has been pressed but it was not pressed last loop (aka it JUST got pushed down) then blahCode:if Dpad:cross() and not oldDpad:cross() then
If you want to change that logic to, if cross was pressed down but just got released then blah, then do this:
Think about this before responding.Code:if oldDpad:cross() and not Dpad:cross() then


LinkBack URL
About LinkBacks
Mit Zitat antworten

Hello everyone I am new here and I am glad to be part of this amazing community and I think there...
New to forum