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; But first you need to put something like this in your code: Code: System.playAllMp3sInASpecificDirectoryOneAfterTheOther = function(dir) local list = System.listDirectory(dir) ...
-
08-21-2008, 05:45 PM #9301QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
But first you need to put something like this in your code:
Also, the first letter of a function name is not capitalized (play not Play)Code:System.playAllMp3sInASpecificDirectoryOneAfterTheOther = function(dir) local list = System.listDirectory(dir) local bgcolor = Color.new(255,255,255) table.remove(list,1) table.remove(list,1) for i = 1, table.getn(list) do if not list[i].directory and string.lower(string.sub(list[i].name,-4)) == ".mp3" then local path = dir.."/"..list[i].name Mp3.load(path) Mp3.play() screen:print(0,0,path.." is playing and has been for") while Mp3.eos ~= "true" do --note its a string not a boolean because some people are idiots screen:clear(bgcolor) screen:print((string.len(path)*8)+232,0,Mp3.gettime()) screen.flip() screen.waitVblankStart(30) end Mp3.stop() end end end
-
08-21-2008, 06:26 PM #9302QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Ruining the fun, as usual, TurtlesPwn, I see.
-
08-21-2008, 06:33 PM #9303QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Oh come on I hate handing them code as much as you do, but im a junkie for making them go away
-
08-21-2008, 06:41 PM #9304QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Oh snap, inside jokes suck
-
08-21-2008, 06:42 PM #9305QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Yea I don't think anybody except me, you, and one other person has any idea what the hell we're talking about. Let's let the noobs take back this thread.
-
08-21-2008, 06:43 PM #9306QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Deal.
(You can't stop me)
-=Double Post Merge =-
Har har, turtle is a pretty funny guyGeändert von _dysfunctional (08-21-2008 um 06:45 PM Uhr) Grund: Automerged Doublepost
-
08-21-2008, 06:50 PM #9307QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Oh why thank you, I've always wanted to be a comedian, but whenever I do something truly epic and funny I get in trouble
-
08-21-2008, 07:18 PM #9308QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
And I thought our relationship was far past this nonsense. I forgive you <3
-
08-21-2008, 07:29 PM #9309QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
*is confused due to confusion and unsureness of sarcasm detector*
-
08-21-2008, 07:47 PM #9310OMFG

- Registriert seit
- Jul 2005
- Ort
- Toronto
- Beiträge
- 2.814
- Points
- 19.453
- Level
- 88
- Downloads
- 0
- Uploads
- 0
my gaydar is going craaaaazy
jk
-
08-22-2008, 10:01 AM #9311
- Registriert seit
- Sep 2007
- Beiträge
- 5
- Points
- 2.976
- Level
- 33
- Downloads
- 0
- Uploads
- 0
I need help..my problem is that when I have like a menu and a submenu like this
The problem with this is it all happens too fast...I think it does all the code when you hold down cross because you still didnt lift it up yet. It works if I change one of them to a different button...and I tried to use system.sleep but it didnt work does anybody know how to solve this?Code:if state="mainmenu" then --draw code-- if pad:cross() and oldpad ~= pad then state="submenu" end end if state="submenu" then --draw code-- if pad:cross() and oldpad ~= pad then --start game, etc end end
-
08-22-2008, 10:12 AM #9312QJ Gamer Green
- Registriert seit
- Jul 2008
- Ort
- In your pocket.
- Beiträge
- 192
- Points
- 5.875
- Level
- 49
- My Mood
-
- Downloads
- 0
- Uploads
- 0
-=Double Post Merge =-Code:while true do if state == "MainMenu" then if pad:cross() and oldpad:cross() ~= pad:cross() then state = "SubMenu" end end if state == "SubMenu" then if pad:cross() and oldpad:cross() ~= pad:cross() then --start Game end end end
-.-
-=Double Post Merge =-
oh thanks.....-.-'Geändert von Digikid13 (08-22-2008 um 10:16 AM Uhr) Grund: Automerged Doublepost
AIM: Digikid91 | PSN: Digikid13 | GaiaOnline: Digikid13
-
08-22-2008, 10:26 AM #9313QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Do an else if:
The reason why it wasn't working was because it could process the second if statement after the state has changed in the same frame. ie:Code:if state=="mainmenu" then --draw code-- if pad:cross() and oldpad ~= pad then state="submenu" end elseif state=="submenu" then --draw code-- if pad:cross() and oldpad ~= pad then --start game, etc end end
Players presses Cross, state is now "submenu". It then processes the second if statement in the same frame so pad:cross is still false and oldpad is different to pad.Code:if state=="mainmenu" then --draw code-- if pad:cross() and oldpad ~= pad then state="submenu" end endGeändert von yaustar (08-22-2008 um 03:20 PM Uhr)
[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
08-22-2008, 01:02 PM #9314Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
also make sure you are using the right check functions
notCode:if something == "this" then --aaaaa end
Code:if something = "this" then --aaaaa 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).
-
08-22-2008, 03:20 PM #9315QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Do'h, fixed.
[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
08-22-2008, 07:24 PM #9316
- Registriert seit
- Sep 2007
- Beiträge
- 5
- Points
- 2.976
- Level
- 33
- Downloads
- 0
- Uploads
- 0
I tried adding elseif and else if neither worked though...I get
error: index.lua: 82: 'end' expected (to close ' function' at line 28) near 'else'
Error: No script file found.
It runs without the else though..I dont get why
-
08-22-2008, 07:37 PM #9317Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
post the code and i will fix it for you right now.
EDIT:
nvm i just looked up and found it this has been said before but if it doesnt work then try the code below that, if that doesnt work then post your whole script and ill fix it.
Code:if state == "mainmenu" then --draw code-- if pad:cross() and oldpad ~= pad then state = "submenu" end elseif state = "submenu" then --draw code-- if pad:cross() and oldpad ~= pad then --start game, etc end endEDIT 2:Code:if state == "mainmenu" then --draw code-- if pad:cross() and oldpad ~= pad then state = "submenu" oldpad = Controls.read():cross() end elseif state = "submenu" then --draw code-- if pad:cross() and oldpad ~= pad then --start game, etc end end
Damn I just wasted my 1337'est post on this!Geändert von FaT3oYCG (08-22-2008 um 07:51 PM 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).
-
08-22-2008, 08:16 PM #9318
- Registriert seit
- Sep 2007
- Beiträge
- 5
- Points
- 2.976
- Level
- 33
- Downloads
- 0
- Uploads
- 0
It doesnt work..Ill post my code but its pretty long. Also keep in mind this doesnt really do anything yet..im just working on the menu. Its going to be a tycoon game and I will be fixing up the graphics. Also the else ifs are spread apart alot
Code:math.randomseed(os.time()) System.usbDiskModeActivate() --Colors white = Color.new(255, 255, 255) red = Color.new(255,0,0) green= Color.new(0,255,0) blue=Color.new(0,0,255) --Font font = Font.load("Comic.ttf") font:setPixelSizes(0,15) --Variables state="menu" select=0 money=100 fans=0 skill=0 moneyearned=0 fansgained=0 expenses=0 totalearned=0 --Functions function menu() if state=="menu" then if select==0 then screen:fontPrint(font,20,100,"Start Day",blue) screen:fontPrint(font,20,120,"Location",white) screen:fontPrint(font,20,140,"Lessons",white) screen:fontPrint(font,20,160,"Equipment",white) end if select==1 then screen:fontPrint(font,20,100,"Start Day",white) screen:fontPrint(font,20,120,"Location",blue) screen:fontPrint(font,20,140,"Lessons",white) screen:fontPrint(font,20,160,"Equipment",white) end if select==2 then screen:fontPrint(font,20,100,"Start Day",white) screen:fontPrint(font,20,120,"Location",white) screen:fontPrint(font,20,140,"Lessons",blue) screen:fontPrint(font,20,160,"Equipment",white) end if select==3 then screen:fontPrint(font,20,100,"Start Day",white) screen:fontPrint(font,20,120,"Location",white) screen:fontPrint(font,20,140,"Lessons",white) screen:fontPrint(font,20,160,"Equipment",blue) end if pad:cross() and oldpad ~= pad then if select==0 then state="startday" select=0 end if select==1 then state="locations" select=0 end if select==2 then state="lessons" select=0 end if select==3 then state="equipment" select=0 end end end --startday elseif state="startday" then moneyearned=(fans*math.random(5))+10 fansgained=math.floor(moneyearned/2) expenses=0 totalearned=moneyearned-expenses screen:fontPrint(font,20,100,"Money Earned: ",white) screen:fontPrint(font,140,100,moneyearned,white) screen:fontPrint(font,20,120,"Fans Gained: ",white) screen:fontPrint(font,140,120,fansgained,white) screen:fontPrint(font,20,140,"Expenses: ",red) screen:fontPrint(font,140,140,expenses,red) screen:fontPrint(font,20,160,"Total: ",white) screen:fontPrint(font,140,160,totalearned,white) end elseif state="locations" then screen:fontPrint(font,20,80,"Choose Location",white) if select==0 then screen:fontPrint(font,20,100,"The Bronx",blue) screen:fontPrint(font,20,120,"Manhatten",white) end if select==1 then screen:fontPrint(font,20,100,"The Bronx",white) screen:fontPrint(font,20,120,"Manhatten",blue) end if pad:circle() and oldpad~=pad then state="menu" end end elseif state="lessons" then if select==0 then screen:fontPrint(font,20,100,"Take Lessons",blue) screen:fontPrint(font,20,120,"Leave",white) end if select==1 then screen:fontPrint(font,20,100,"Take Lessons",white) screen:fontPrint(font,20,120,"Leave",blue) end if pad:circle() and oldpad ~= pad then state="menu" end if pad:cross() and oldpad ~= pad then if select==0 then skill=skill+1 money=money-skill*100 state="menu" select=2 end if select==1 then state="menu" select=2 end end end --Update Select if pad:down() and oldpad ~= pad then select=select+1 end if pad:up() and oldpad ~= pad then select=select-1 end if select>3 then select=0 end if select<0 then select=3 end end --Main Loop while true do screen:clear() pad=Controls.read() screen:fontPrint(font,5,15,"Money:",white) screen:fontPrint(font,55,15,money,white) screen:fontPrint(font,100,15,"Fans:",white) screen:fontPrint(font,160,15,fans,white) menu() if pad:triangle() then break end oldpad=pad screen.waitVblankStart() screen.flip() end
-
08-22-2008, 09:07 PM #9319QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Aside from the fact that the menu code is horrible, and your indenting is ridiculous, elseifs work like this:
( http://www.lua.org/manual/5.1/manual.html#2.4.4 )Code:if exp then block elseif exp then block end
As you can see, you multiple times have:
Code:end elseif state=...
-
08-22-2008, 09:56 PM #9320
- Registriert seit
- Sep 2007
- Beiträge
- 5
- Points
- 2.976
- Level
- 33
- Downloads
- 0
- Uploads
- 0
How can I make it easier..because it gets kind of complicated. Should I use switch statements(if lua even has them). Also are you saying I cant have multiple elses.
-
08-22-2008, 11:24 PM #9321QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
What he means is, you are doing:
when you should be doing:Code:if blah then -- something end elseif blah then -- something else end
Code:if blah then -- something elseif blah then -- something else end
-
08-22-2008, 11:36 PM #9322QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
To make it easier, use tables and loops.
Oh, and you can do multiple elseif. (I suggest you look through the lua reference manual to avoid these problems in future).Code:colour1 = Color.new(128,128,128) colour2 = Color.new(255,255,255) options = { "lol1", "lol2", "lol3" } selection = 1 seperation = 50 while true do --normal int stuff if pad:up and not oldpad:up() then selection = selection - 1 end if pad:down and not oldpad:down() then selection = selection + 1 end if selection > table.getn(options) then selection = 1 elseif selection < 1 then selection = table.getn(options) end for i, text in ipairs(options) do if selection == i then screen:print(50, (i - 1) * seperation, text, colour1) else screen:print(50, (i - 1) * seperation, text, colour2) end end --normal end stuff end
Code:if blah then lolcat elseif blah2 then loldog elseif blah3 then lolbird end
-
08-23-2008, 08:50 AM #9323
Hi all.
I'm using the HM function to save date with officiel Sony Data Manager but Lua Player HM is always putting on the save a ugly ICON0 and PIC1. Does someone knoe how to fix it ?
-
08-23-2008, 10:25 PM #9324QJ Gamer Silver

- Registriert seit
- Sep 2007
- Ort
- AUS
- Beiträge
- 284
- Points
- 12.501
- Level
- 73
- Downloads
- 0
- Uploads
- 0
I will fix it
Wyvern. That is all
-
08-24-2008, 06:12 PM #9325Raining
- Registriert seit
- Jun 2005
- Ort
- In The Fog...
- Beiträge
- 545
- Points
- 7.355
- Level
- 57
- My Mood
-
- Downloads
- 0
- Uploads
- 0
Agh, my brain isn't working. I have been figuring out a whole bunch of things on my own but right now I guess I am feeling too much pressure to figure this out. This might seem simple to you all but for some reason I just can't wrap my head around ti. I am sure if I just spend some time thinking about it I will eventually get it but I would appreciate it if anyone can give me a hint as to how I would solve the following problem:
Okay, if I were making a dodging style game where the speed kept increasing after every 30 seconds how would I do that? I want all of the objects to advance in speed and make sure that the game could go on to infinity if someone were that good(or maybe if I set an AI to control the player and make it able to dodge all the objects). How would I do this? Just a rough explanation or a quick example would be nice. I do not need a full blown explanation.
Thanks,
-Daniel
-
08-24-2008, 06:39 PM #9326QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
You would do something like...
Code:local timer = Timer() timer:start() local last = 0 while gameRunning do local delta = timer:delta() local diff = delta - last if diff > (30 seconds) then PlayerSpeed = PlayerSpeed + 1 end last = delta -- Other stuff end
-
08-24-2008, 06:45 PM #9327Raining
- Registriert seit
- Jun 2005
- Ort
- In The Fog...
- Beiträge
- 545
- Points
- 7.355
- Level
- 57
- My Mood
-
- Downloads
- 0
- Uploads
- 0
Thank you dysfunctional. What does "timer:delta()" get? I have not used that before.
-
08-24-2008, 06:59 PM #9328QJ Gamer Blue
- Registriert seit
- Jul 2007
- Beiträge
- 296
- Points
- 3.795
- Level
- 38
- Downloads
- 0
- Uploads
- 0
Those aren't actual luaplayer functions, I just used the names of the classes I export to lua in my own 'luaplayer'. Timer:delta() is the amount of time that passed since the timer started. If you look up the documentation to whichever lib you use (I suggest PGELua), you will find the corresponding function name.
-
08-24-2008, 07:07 PM #9329QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
In luaplayer:
In PGE Lua: (Is't the delta the time since the last timer update?)Code:timer = Timer.new() timer:start() while gameRunning do if timer:time() > 30000 then PlayerSpeed = PlayerSpeed + 1 timer:reset() end -- Other stuff end
or simpler (but possabily slower):Code:timer = pge.timer.create() timerTime = 0 while gameRunning do timerTime = timerTime + timer:getdelta() if timerTime > 30000 then PlayerSpeed = PlayerSpeed + 1 timerTime = 0 end -- Other stuff end
Code:timer = pge.timer.create() while gameRunning do timer:update() if timer:totalTime > 30000 then PlayerSpeed = PlayerSpeed + 1 timer = pge.timer.create() end -- Other stuff endGeändert von Nielkie (08-24-2008 um 07:25 PM Uhr)
-
08-24-2008, 07:41 PM #9330Raining
- Registriert seit
- Jun 2005
- Ort
- In The Fog...
- Beiträge
- 545
- Points
- 7.355
- Level
- 57
- My Mood
-
- Downloads
- 0
- Uploads
- 0
Okay, thanks Nielkie and dystfunctional. That just made me feel like an idiot. Now that I look at it it seems so obvious. Thanks guys.


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