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; load sound if pad cross then play sound1 elseif pad circle then play sound2 end...
-
11-02-2007, 11:53 PM #8071lol

- Registriert seit
- Aug 2006
- Ort
- Whittier, CA
- Beiträge
- 5.791
- Points
- 20.859
- Level
- 91
- Downloads
- 0
- Uploads
- 0
load sound
if pad cross then
play sound1
elseif pad circle then
play sound2
end
-
11-03-2007, 03:00 AM #8072QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
ehh... I usually don't help people who ask for code as basic as this, but it was just too easy to mash up...
Code:sound1 = Sound.load("sound1.wav",false) sound2 = Sound.load("sound2.wav",false) oldpad = Controls.read() while true do pad = Controls.read() screen:print(10,10,"Press X for first sound, and O for the second") if ((pad:cross() ~= oldpad:cross()) and pad:cross()) then sound1:play() end if ((pad:circle() ~= oldpad:circle()) and pad:circle()) then sound2:play() end oldpad = pad screen.waitVblankStart() screen.flip() endGeändert von Nielkie (11-03-2007 um 01:56 PM Uhr)
-
11-03-2007, 06:02 AM #8073
Tanks a lot.
-
11-03-2007, 11:37 AM #8074QJ Gamer Green
- Registriert seit
- Nov 2005
- Ort
- Sweden
- Beiträge
- 460
- Points
- 6.520
- Level
- 52
- Downloads
- 0
- Uploads
- 0
Does "sound1:play" really work? I made a cleaner alternative, removed unnecessary "(" and ")"s.
Zitat von Nielkie
I may not have to be this way, but i think it's a cleaner alternative, since LUA don't need the highlighted ones "if (something) then"Code:sound1 = Sound.load("sound1.wav",false) sound2 = Sound.load("sound2.wav",false) oldpad = Controls.read() while true do pad = Controls.read() screen:print(10,10,"Press X for first sound, and O for the second") if pad:cross() ~= oldpad:cross() and pad:cross() then sound1:play() end if pad:circle() ~= oldpad:circle() and pad:circle() then sound2:play() end oldpad = pad screen.waitVblankStart() screen.flip() end
Also the "sound1:play", it may not need the "sound1:play()" but i think it's a lot less confusing.[CENTER]Some of my homebrew Applications/Games:
[URL=http://forums.qj.net/showthread.php?t=47294&page=1&pp=10]Planet Fighter[/URL] | [URL=http://forums.qj.net/showthread.php?p=641672#post641672]Graphic Creator (V2.0)[/URL] | [URL=http://forums.qj.net/showthread.php?p=512717]Fire Pong[/URL] | [B][URL="http://forums.qj.net/f-psp-development-forum-11/t-release-brushes-v20-99207.html#post1430891"][COLOR="Red"][SIZE="3"]Brushes v2.0[/COLOR][/SIZE][/B][/URL] [URL="http://forums.qj.net/f-psp-development-forum-11/t-release-brushes-v20-99207.html"][B][SIZE="2"][COLOR="Black"]Released![/COLOR][/SIZE][/B][/URL]
[URL="http://haxxblaster.2u.se/"][COLOR="black"][FONT="Arial Black"]www.HaxxBlaster.com[/FONT][/COLOR][/URL]
[URL="http://forums.qj.net/f-psp-development-forum-11/t-release-brushes-v20-99207.html"][IMG]http://img19.imageshack.us/img19/1346/brushesbannerqz3.png[/IMG][/URL][/CENTER]
-
11-03-2007, 01:56 PM #8075QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Sorry, I have got a habit of over-bracketing my code. Oh, and yeah, it is : play().
-
11-06-2007, 10:17 AM #8076
Does anyone here have any experience with the 3D lighting functions?
-
11-06-2007, 10:49 PM #8077
ive been working on a horizontal scrolling shooting for a couple of days now and have run into a problem. i want to create another enemy after 1-2 seconds. ive looked at the timer tutorials at evilmana.com but cant get them to work. anyone know what to do?
-
11-07-2007, 12:33 AM #8078QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Zitat von drag_93
A basic timer goes like this:
yourtimer = timer.new(0)
This creates a new timer.
Next, you must create a variable to hold the current time.
yourtime = yourtimer:time()
This must be put in your main loop - to update every frame.
Next, you must start the timer.
yourtimer:start()
To stop the timer you have:
yourtimer:stop()
And you can reset the timer to 0 like this:
yourtimer:reset(0)
Now, you can do this:
If yourtime >= 1000 then
enemy.new()
yourtimer:reset(0)
end
-
11-07-2007, 01:32 PM #8079
thanks
-
11-10-2007, 07:05 AM #8080Developer 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 someone help me figure out why this doesnt work please
i dont have much time to work on things at the moment and this is taking too long to work out
ALL help appreciated thanks
i know i have a few extra ends in there at the moment but the main error araises before thatCode:-- image resizer -- either the file name or variable that holds file name origpic = Image.load(browserbg.png) -- either the file name or variable that holds file name -- creates an empty variable that holds the values that need to be resized resize = "" -- creates an empty variable that holds the values that need to be resized -- creates an empty variable that stores the picture to be used pictouse = "" -- creates an empty variable that stores the picture to be used -- will check if the image needs to be resized -- if not it will skip the resize process -- if it does it will check the height, width or both height and width function sizecheck() if origpic:height() > 240 -- set to the largest x value wanted or origpic:width() > 230 -- set to the largest y value wanted then resizecheck() else pictouse = screen:blit(244, 37, origpic) end end -- end of size check function rvx = 0 rvy = 0 -- resize check value - checks which values need to be resized -- for example if the x is bigger than the y then the x will be -- the value to be checked to see if the new image is small enough -- and vice versa if they are the same then the x valuse will be used function resizecheck() if origpic:height() > origpic:width() then resize = y elseif origpic:width() > origpic:height() then resize = x elseif origpic:height() == origpic:width() then resize = x end resizevalues() end -- end of resizecheck function -- stores the new x and y values function resizevalues() if resize == x then rvx = origpic:width() / 2 rvy = origpic:height() / 2 until rvx < 240 -- set to the largest x value wanted elseif resize == y then rvx = origpic:width() / 2 rvy = origpic:height() / 2 until rvy < 230 -- set to the largest y value wanted end end end resizepicture(rvx, rvy, origpic) end -- stores the new x and y values -- creates a new version or the image that you have resized function resizepicture(newx, newy, theimage) newpicture = Image.createEmpty(newx, newy) for thex = 1, newx do for they = 1, newy do newpicture:blit(thex, they , theimage, math.floor(thex * (theimage:width() / newx)), math.floor(they * (theImage:height() / newy)), 1, 1) end end return pictouse = newpicture end -- creates a new version or the image that you have resized function blitpic() pictouse end while true do blitpic()
the error message is
line 64 'end' expected (to close 'if' at line 59) near 'until' but if you look i dont want to end it there
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).
-
11-10-2007, 04:40 PM #8081QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
You know, you will get more help if you're code is actually readable. (Indenting, not one-word-per-line)
Anyway, what are you trying to accomplish with that "until"? "until" is used like this:
repeat block until exp
It's basically a reverse "while" loopGeändert von Nielkie (11-10-2007 um 04:53 PM Uhr)
-
11-10-2007, 05:14 PM #8082QJ Gamer Bronze
- Registriert seit
- Mar 2007
- Beiträge
- 758
- Points
- 8.665
- Level
- 62
- Downloads
- 0
- Uploads
- 0
Your code is horrible. I took out all the comments and made things on the same line here. A couple of things which I'm not sure where they are, but these are the problems:Code:origpic = Image.load(browserbg.png) resize = "" pictouse = "" function sizecheck() if origpic:height() > 240 or origpic:width() > 230 then resizecheck() else pictouse = screen:blit(244, 37, origpic) end end rvx = 0 rvy = 0 function resizecheck() if origpic:height() > origpic:width() then resize = y elseif origpic:width() > origpic:height() then resize = x elseif origpic:height() == origpic:width() then resize = x end resizevalues() end function resizevalues() if resize == x then rvx = origpic:width() / 2 rvy = origpic:height() / 2 until rvx < 240 elseif resize == y then rvx = origpic:width() / 2 rvy = origpic:height() / 2 until rvy < 230 end end end resizepicture(rvx, rvy, origpic) end function resizepicture(newx, newy, theimage) newpicture = Image.createEmpty(newx, newy) for thex = 1, newx do for they = 1, newy do newpicture:blit(thex, they , theimage, math.floor(thex * (theimage:width() / newx)), math.floor(they * (theImage:height() / newy)), 1, 1) end end return pictouse = newpicture end function blitpic() pictouse end while true do blitpic()
1. One of your functions is "pictouse". That means absolutely nothing. Putting a random variable in a functions is... WTF?
2.???? You are trying to assign something to "pictouse" using screen:blit? Does screen:blit even return anything?Code:pictouse = screen:blit(244, 37, origpic)
3. As Nielkie said, why is there a random until in the middle of nowhere?
4.I'm pretty sure that is not how return statements work...Code:return pictuose = newpicture
There are an unbelievable amount of problems with your code.
-
11-10-2007, 05:33 PM #8083Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
ok maybe if i explain what i was wanting to do it is hard to know what i am doing wrong when i dont have much time and have to work on it in little bits so as i said earlier it was taking too long so i came to ask for some help
the code is meant to take an image either predefined or defined from a file browser
then when it has loaded the file see if it needs resizeing or not
if it doesnt skip the whole resize process to save time
if it does then do the resize function (which is very messed up i know) (i just explained why above)
then it checks the x and y and sees which is larger
then it makes both x and y untill the larger is smaller than the defined value
then it resizes using the defined values and blits the image to the screen
well thats what ti is supposed to do but it doesnt
so i am asking for some help pleas for example some revised code or a propper explanation of how to fix the problems that you have apparently found
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).
-
11-10-2007, 06:02 PM #8084
This is where you get help with your code. Not where others code for you. Post the code you have, and we'll help you fix it.
Zitat von FaT3oYCG
[IMG]http://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Zoso.svg/744px-Zoso.svg.png[/IMG]
Looking for some good C programming tutorials for the PSP? Look no further! [URL="http://psp-coding.com/"]PSP-Coding.com[/URL] is your source for all your PSP coding needs.
-
11-10-2007, 06:24 PM #8085QJ Gamer Bronze
- Registriert seit
- Mar 2007
- Beiträge
- 758
- Points
- 8.665
- Level
- 62
- Downloads
- 0
- Uploads
- 0
He did post his code. Check a couple posts back. My last post takes out all of the comments and organizes it a bit.
First of all, you pretty much need to take out that until. For sure, unless you know how to use this:
http://lua-users.org/wiki/ControlStructureTutorialCode:repeat block of code until expression
You did not end your main loop, "while true do". Put an end at the end of it.
Try making the variable names more descriptive so you don't need so many comments. Instead of rvy and rvx, make them what they mean.
Indent!!!! So you know where to put "end"s.
-
11-10-2007, 07:43 PM #8086QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Untested, but this is basically what you're code is trying to do in a condensed forum.
Code:function load_img_scaled(image,width,height) local retimg; local img = Image.load(image) local aspect = img:width / img:height local function scaleimg(theImage,newX,newY) resizedImage = Image.createEmpty(newX, newY) for x = 1, newX do for y = 1, newY do resizedImage:blit(x,y , theImage, math.floor(x*(theImage:width()/newX)), math.floor(y*(theImage:height()/newY)),1,1) end end return resizedImage end if img:width > width and img:height > height then if img:width > img:height then local heightA = width / aspect retimg = scaleimg(img,width, heightA) else local widthA = height * aspect retimg = scaleimg(img,widthA, height) end elseif img:width > width then local heightA = width / aspect retimg = scaleimg(img,width, heightA) elseif img:height > height then local widthA = height * aspect retimg = scaleimg(img,widthA, height) else retimg = img end return retimg end
usage: load_img_scaled("path/to/file.png",maxwidth,maxhei ght)
e.g:
resized_image = load_img_scaled("pic_that _is_bigger_than_480x272.p ng",480,272)
-
11-11-2007, 05:59 AM #8087QJ Gamer Bronze
- Registriert seit
- Mar 2007
- Beiträge
- 758
- Points
- 8.665
- Level
- 62
- Downloads
- 0
- Uploads
- 0
You put a semi-colon at the end of your second line.
Code:function load_img_scaled(image,width,height) local retimg;
-
11-11-2007, 06:39 AM #8088Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
thanks a load i modified it slightly to make it work but thanks
here is the finished and working code
thank you so much neilkieCode:function load_img_scaled(image,width,height) local retimg; local img = Image.load(image) local aspect = img:width() / img:height() local function scaleimg(theImage,newX,newY) resizedImage = Image.createEmpty(newX, newY) for x = 1, newX do for y = 1, newY do resizedImage:blit(x,y , theImage, math.floor(x*(theImage:width()/newX)), math.floor(y*(theImage:height()/newY)),1,1) end end return resizedImage end if img:width() > width and img:height() > height then if img:width() > img:height() then local heightA = width / aspect retimg = scaleimg(img,width, heightA) else local widthA = height * aspect retimg = scaleimg(img,widthA, height) end elseif img:width() > width then local heightA = width / aspect retimg = scaleimg(img,width, heightA) elseif img:height() > height then local widthA = height * aspect retimg = scaleimg(img,widthA, height) else retimg = img end return retimg end resized_image = load_img_scaled("image.png",230,136) while true do screen:blit(244, 37, resized_image) screen.flip() screen.waitVblankStart() 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).
-
11-12-2007, 12:38 PM #8089
you are allowed to do that so it won't mess anything up
Zitat von michaelp
-
11-14-2007, 12:43 PM #8090
- Registriert seit
- Jul 2007
- Beiträge
- 9
- Points
- 3.143
- Level
- 34
- Downloads
- 0
- Uploads
- 0
I need a better way to select a random line from a file.
I'm trying to write a name generator for the PSP. I have a file with a list of names in it, one per line.
My code works perfectly for small files (under about 1000 lines), but for larger ones (like 300Kb), it gives me the error "No space left on device". Seems there isn't enough memory to load the entire file into memory.
Any suggestions on a more memory efficient way to do this? My existing code is below. It opens the file into a table and returns 5 randomly selected names.
Code:function getname() firstnamefile = "names-first.txt" local count = 0 local lines = {} for line in io.lines(firstnamefile) do table.insert(lines, line) end -- Figure out how many there are count = table.maxn(lines) -- Select 5 random names first1 = lines[math.random(1,count)] first2 = lines[math.random(1,count)] first3 = lines[math.random(1,count)] first4 = lines[math.random(1,count)] first5 = lines[math.random(1,count)] return first1, first2, first3, first4, first5 end
-
11-14-2007, 03:08 PM #8091
Instead of reading the entire file into memory, you can use file:seek() to change the current position in the file, then read from that point:
http://pgl.yoyo.org/luai/i/file%3Aseek
-
11-14-2007, 07:20 PM #8092
- Registriert seit
- Jul 2007
- Beiträge
- 9
- Points
- 3.143
- Level
- 34
- Downloads
- 0
- Uploads
- 0
Is there a way to seek by line instead of by byte?
Or would I just have to seek to a random location and read in two lines (one possibly partial line, one full line)?
-
11-14-2007, 07:44 PM #8093
i've been reading the collision tutorial on evilmana.com but dont understand any of it. what i'm trying to do is in a horizontal scrolling shooter i want the enemy to collide with the bullet, be replaced with an explosion animation, the explosion disappears and the enemy goes to 500(x) and a random position between 20 and 232(y). anyone know how to do this?
-
11-14-2007, 10:26 PM #8094QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
I don't think so, but you could do this:
Zitat von nobodyimportant
Code:local ranline = math.random(1,io.lines("wordlist.txt")) local ranword; local count = 0 for lines in io.lines("wordlist.txt") do count = count + 1 if count == ranline then ranword = line break --Not sure, can you break out of a for loop? I've never tried. end endGeändert von Nielkie (11-15-2007 um 11:10 PM Uhr)
-
11-14-2007, 11:06 PM #8095
- Registriert seit
- Jul 2007
- Beiträge
- 9
- Points
- 3.143
- Level
- 34
- Downloads
- 0
- Uploads
- 0
Thanks Nielkie. math.random doesn't like me putting io.lines in its parameters...says it was expecting a number but got a function.
But it did get me thinking.
My new function looks like this. It works, even on my 37000 line file. Wish there was a more efficient way, but it'll do for now:
The full script actually saves the number of lines in the file, so it doesn't have to repeat that step each time. It takes about 5 seconds the first time I run it, and about 3-4 for subsequent runs.Code:function getname() filename = "names-first.txt" -- Count lines in the file local namelines = 0 for line in io.lines(lastnamefile) do namelines = namelines + 1 end -- Select 5 random lines local name1num = math.random(1,namelines) local name2num = math.random(1,namelines) local name3num = math.random(1,namelines) local name4num = math.random(1,namelines) local name5num = math.random(1,namelines) local name1found, name2found, name3found, name4found, name5found = false,false,false,false,false local curline = 0 -- Go through the entire file, -- selecting the lines as we encounter them. -- When all the needed lines are found, exit for line in io.lines(lastnamefile) do curline = curline + 1 if curline == name1num then name1 = line name1found = true end if curline == name2num then name2 = line name2found = true end if curline == name3num then name3 = line name3found = true end if curline == name4num then name4 = line name4found = true end if curline == name5num then name5 = line name5found = true end if name1found and name2found and name3found and name4found and name5found then break end end
-
11-15-2007, 12:41 PM #8096Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
making a slight change to the way nielkie set out the code would make i work
Zitat von Nielkie
by changing this code to
as you cannot use a function as a paramete or inside another function as the error you got explainedCode:endline = io.lines("wordlist.txt") local ranline = math.random(1, endline) local ranword; local count = 0 for lines in io.lines("wordlist.txt") do count = count + 1 if count = ranline then ranword = line break --Not sure, can you break out of a for loop? I've never tried. end end
try that and tell me what happens, there might be another error with nielkie's code near the end as also i dont know weather you can break a for loop.
anyway repot back and im sure either myself or nielkie will be able to help, unless someone else wants a pop at it
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).
-
11-15-2007, 03:26 PM #8097
Why not just use something like this:
This works, and since you are only reading from the file 10 times, it'll be much faster than using io.lines to read through the file until you find five matches, especially if it's a file with 37000 linesCode:function getname() filename = "names.txt" file = io.open(filename) filesize = file:seek("end") -- get the size of the file names = {} for i = 1, 5 do local pos = math.random(filesize - maxnamesize - 1) if pos < 1 then pos = 1 end file:seek("set", pos ) file:read() names[i] = file:read() end file:close() return names end
-
11-17-2007, 06:17 AM #8098QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Just a quick question, could anyone give me an example of the usage of Font:getTextSize(string) ? I can't seem to get it working.
(using:
) (from here)Code:width = myfont:getTextSize(text)["width"]
EDIT: I see in the luaplayer 0.20 release notes:
- Font:getTextSize fixed.
Is it broken in 0.16?
If yes, is there any work-around?
-
11-19-2007, 01:29 PM #8099
why not just use the .20 version?
-
11-19-2007, 03:35 PM #8100lol

- Registriert seit
- Aug 2006
- Ort
- Whittier, CA
- Beiträge
- 5.791
- Points
- 20.859
- Level
- 91
- Downloads
- 0
- Uploads
- 0
.16 is better than .20 IMO.


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