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; Ok i was wondering if anyone could give me a link to a tut that shows me how to add ...
-
07-24-2007, 05:16 PM #7801QJ Gamer Bronze
- Registriert seit
- Dec 2006
- Beiträge
- 654
- Points
- 5.934
- Level
- 49
- Downloads
- 0
- Uploads
- 0
Ok i was wondering if anyone could give me a link to a tut that shows me how to add a menu for my game, like main menu, and animation... Thanks if you can.
-
07-24-2007, 05:31 PM #7802QJ Gamer Gold
- Registriert seit
- Aug 2006
- Ort
- Under Your Bed
- Beiträge
- 3.083
- Points
- 12.189
- Level
- 72
- Downloads
- 0
- Uploads
- 0
-
07-24-2007, 07:25 PM #7803QJ Gamer Bronze
- Registriert seit
- Dec 2006
- Beiträge
- 654
- Points
- 5.934
- Level
- 49
- Downloads
- 0
- Uploads
- 0
Ok heres my code so far
But when i press X in the menu, nothing happens, any help?Code:--Colors white = Color.new(255,255,255) red = Color.new(255,0,0) current = 1 --this is our current menu selection oldpad = Controls.read() while true do pad = Controls.read() screen:clear() screen:print(240,100,"Single Player",white) screen:print(240,110,"Options",white) screen:print(240,120,"Exit",white) if current == 1 then screen:print(240,100,"Single Player",red) end if current == 2 then screen:print(240,110,"Options",red) end screen:print(240,120,"Exit",red) end if pad:up() and oldpad:up() ~= pad:up() then current = current - 1 end if pad:down() and oldpad:down() ~= pad:down() then current = current + 1 end if current == 6 then current = 1 end if current == 0 then current = 5 end screen.waitVblankStart() screen.flip() oldpad = pad end if pad:cross() and current == 1 then Single = Player( screen:clear() )background = Image.load("images/background.jpg") player2 = Image.load("images/player2.png") end if pad:cross() and current == 2 then Options() end if pad:cross() and current == 3 then Exit() end
Oh and is there any tutorials on like making the screen scroll to see the rest of a map (like in Halo Genocide, if u keep moving right the screen will start to scroll)?
-
07-24-2007, 08:55 PM #7804
anyone ever seen this error?
i have a feeling its from loading over 50 pngs but i thought i deleted them by setting them to nil. this does delete them correct?Code:libpng error: Too many IDAT's found
-
07-24-2007, 09:10 PM #7805QJ Gamer Bronze
- Registriert seit
- Jul 2006
- Ort
- Louisiana
- Beiträge
- 183
- Points
- 4.563
- Level
- 43
- Downloads
- 0
- Uploads
- 0
1. clicking x on current 1 only set variables
Zitat von _-Tiger-_
2. have you written the functions for Options() and Exit()Survived Hurricane Gustav!!!!!
-
07-24-2007, 09:12 PM #7806QJ Gamer Green
- Registriert seit
- Dec 2005
- Ort
- Here
- Beiträge
- 2.715
- Points
- 13.310
- Level
- 75
- Downloads
- 0
- Uploads
- 0
very easy, just put this:
Zitat von _-Tiger-_
in the 'while' loop, you have it outside.Code:if pad:cross() and current == 1 then Single = Player( screen:clear() )background = Image.load("images/background.jpg") player2 = Image.load("images/player2.png") end if pad:cross() and current == 2 then Options() end if pad:cross() and current == 3 then Exit() end
Code:--Colors white = Color.new(255,255,255) red = Color.new(255,0,0) current = 1 --this is our current menu selection oldpad = Controls.read() while true do pad = Controls.read() screen:clear() screen:print(240,100,"Single Player",white) screen:print(240,110,"Options",white) screen:print(240,120,"Exit",white) if current == 1 then screen:print(240,100,"Single Player",red) end if current == 2 then screen:print(240,110,"Options",red) end screen:print(240,120,"Exit",red) end if pad:up() and oldpad:up() ~= pad:up() then current = current - 1 end if pad:down() and oldpad:down() ~= pad:down() then current = current + 1 end if current == 6 then current = 1 end if current == 0 then current = 5 end if pad:cross() and current == 1 then Single = Player( screen:clear() )background = Image.load("images/background.jpg") player2 = Image.load("images/player2.png") end if pad:cross() and current == 2 then Options() end if pad:cross() and current == 3 then Exit() end screen.waitVblankStart() screen.flip() oldpad = pad end
-
07-24-2007, 09:25 PM #7807QJ Gamer Bronze
- Registriert seit
- Jul 2006
- Ort
- Louisiana
- Beiträge
- 183
- Points
- 4.563
- Level
- 43
- Downloads
- 0
- Uploads
- 0
change you code to this:
Zitat von _-Tiger-_
Code:--Colors white = Color.new(255,255,255) red = Color.new(255,0,0) --Images background = Image.load("images/background.jpg") player2 = Image.load("images/player2.png") current = 1 --this is our current menu selection oldpad = Controls.read() while true do pad = Controls.read() screen:clear() screen:print(240,100,"Single Player",white) screen:print(240,110,"Options",white) screen:print(240,120,"Exit",white) if current == 1 then screen:print(240,100,"Single Player",red) end if current == 2 then screen:print(240,110,"Options",red) end if current ==3 then screen:print(240,120,"Exit",red) end if pad:up() and oldpad:up() ~= pad:up() then current = current - 1 end if pad:down() and oldpad:down() ~= pad:down() then current = current + 1 end if current == 6 then current = 1 end if current == 0 then current = 5 end if pad:cross() and current == 1 then Single = Player( screen:clear()) screen:blit(0,0,background) screen:blit(0,0,player2) end if pad:cross() and current == 2 then Options() end if pad:cross() and current == 3 then Exit() end screen.waitVblankStart() screen.flip() oldpad = pad endSurvived Hurricane Gustav!!!!!
-
07-24-2007, 11:00 PM #7808QJ Gamer Bronze
- Registriert seit
- Dec 2006
- Beiträge
- 654
- Points
- 5.934
- Level
- 49
- Downloads
- 0
- Uploads
- 0
Hey thanks all. So does anyone know where i can find a tutorial on how to make the screen slid so to speak? For like sidescrolling games?
-
07-25-2007, 12:47 AM #7809lol

- Registriert seit
- Aug 2006
- Ort
- Whittier, CA
- Beiträge
- 5.791
- Points
- 20.859
- Level
- 91
- Downloads
- 0
- Uploads
- 0
TacticalPenguin's code
Code:playerimg = Image.load("player.png") player = {x = 0, y = 272-playerimg:height()} bgp1 = {x = 0, img = Image.load("bgp1.png") } bgp2 = {x = 480, img = Image.load("bgp2.png") } bgp3 = { x = 960, img = Image.load("bgp3.png") } while true do if player.x < 120 and bgp1.x < 0 then bgp1.x = bgp1.x + 1 bgp2.x = bgp2.x + 1 bgp3.x = bgp3.x + 1 end if player.x + playerimg:width() > 360 and bgp1.x > -960 then bgp1.x = bgp1.x - 1 bgp2.x = bgp2.x - 1 bgp3.x = bgp3.x - 1 end if pad:left() and player.x > 0 then player.x = player.x - 1 end if pad:right() and player.x < 480-playerimg:width() then player.x = player.x + 1 end screen:blit(bgp1.x,0,bgp1 .img) screen:blit(bgp2.x,0,bgp2 .img) screen:blit(bgp3.x,0,bgp3 .img) screen:blit(player.x,play er.y,playerimg) screen.waitVblankStart() screen.flip() end
-
07-25-2007, 05:46 AM #7810QJ Gamer Bronze
- Registriert seit
- Mar 2007
- Beiträge
- 758
- Points
- 8.665
- Level
- 62
- Downloads
- 0
- Uploads
- 0
-
07-25-2007, 01:33 PM #7811lol

- Registriert seit
- Aug 2006
- Ort
- Whittier, CA
- Beiträge
- 5.791
- Points
- 20.859
- Level
- 91
- Downloads
- 0
- Uploads
- 0
function cleanUp()
image1 = nil
image2 = nil
image3 = nil
collectGarbage()
end
Then call the function when needed. It is slow though.
-
07-25-2007, 03:20 PM #7812
ah, thanks to the both of ya
-
07-27-2007, 11:51 AM #7813
on the topic of image modification with the gu, would anyone know where to start in creating a gu model that could produce as effects as this:
Zitat von Merick

exact numbers can be tweaked, just a general form.
-
07-27-2007, 06:10 PM #7814
Check this one out:
When used as-is these functions will let you blit a rotated image, but you might be able to alter them to do the other image transforms you wantedCode:function RotateBlit(bx, by, bz, img, xrot, yrot, zrot, size) -- special blit function using the GU --[[ bx, by, bz = x, y, and z coordinates to draw the image at img = image to use xrot, yrot, zrot = rotation in degrees for each axis size = size to display image in %, will default to 100% if left blank ]] local w = img:width()/2 local h = img:height()/2 xrot = xrot * (Gu.PI/180) yrot = yrot * (Gu.PI/180) zrot = zrot * (Gu.PI/180) if size == nil then size = 1 else size = size / 100 end local imagetile = { -- top section { .5, .5, 0, 0, 0}, { 0, 0, -w*size, h*size, 0}, { 1, 0, w*size, h*size, 0}, -- right section {0.5, 0.5, 0, 0, 0}, { 1, 0, w*size, h*size, 0}, { 1, 1, w*size, -h*size, 0}, -- bottom section {0.5, 0.5, 0, 0, 0}, { 1, 1, w*size, -h*size, 0}, { 0, 1, -w*size, -h*size, 0}, -- left section {0.5, 0.5, 0, 0, 0}, { 0, 1, -w*size, -h*size, 0}, { 0, 0, -w*size, h*size, 0}, } Gu.enable(Gu.TEXTURE_2D); Gu.texImage(img) Gu.texFunc(Gu.TFX_MODULATE, Gu.TCC_RGBA) Gu.texFilter(Gu.NEAREST, Gu.NEAREST) Gu.texScale(1, 1) Gu.texOffset(0, 0) Gum.matrixMode(Gu.MODEL) Gum.loadIdentity() Gum.translate(bx, by, bz); Gum.rotateXYZ(xrot, yrot, zrot) Gum.drawArray(Gu.TRIANGLES, Gu.TEXTURE_32BITF+Gu.VERTEX_32BITF+Gu.TRANSFORM_3D, imagetile) end function start_gu(fov) -- starts the gu for drawing with the GuBlit function, --if you dont put anything in the () when calling this function the fov will default to 50 if fov == nil then fov = 50 end Gu.start3d() Gu.clearDepth(0); Gu.clear(Gu.COLOR_BUFFER_BIT+Gu.DEPTH_BUFFER_BIT) Gum.matrixMode(Gu.PROJECTION) Gum.loadIdentity() Gum.perspective(fov, 480/272, 0.5, 1000) Gum.matrixMode(Gu.VIEW) Gum.loadIdentity() Gum.matrixMode(Gu.MODEL) Gum.loadIdentity() Gu.enable(Gu.BLEND) Gu.blendFunc(Gu.ADD, Gu.SRC_ALPHA, Gu.ONE_MINUS_SRC_ALPHA, 0, 0) end
example:
startSP() -- sets up the Gu for 3d
RotateBlit(0, 0, -290, image, 0,0,90, 150) -- blits an image rotated at 90 degrees in the center of the screen
Gu.end3d() -- stops the Gu
screen:flip()
Because this one uses perspective mode, any images blitted normally will always be underneath the images drawn with this function, anything drawn with fillRect or screen
rint will be on top
Also, if used as-is this function will only work with images where the height and width are the same, and are multiples of 8
One last note - this is an old function that I gave up on a while ago, before mod 4 came out. Since mod 4 is supposed to have ortho mode implemented, if you can figure out how to use it instead of perspective mode you could probably fix the problems.
-
07-27-2007, 07:04 PM #7815
how come the model is drawn with 4 triangles versus 2?
-
07-27-2007, 07:49 PM #7816
by drawing with 4 triangles, you can rotate it around the center of the image, if you were to draw it with 2 triangles the image would rotate around a corner, I've attached a crappy drawing to illustrate this
-
07-28-2007, 04:56 AM #7817QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Does anyone know an easy way to draw a diagonal rectangle?
I mashed up this...
... but obviously it fragments up at certain angles, most noticeably at 45.Code:function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end function diagline(x,y,width,height,angle) for i=0,height do local x0 = round(x - (i * math.cos((angle + 90)*math.pi/180)),0) local y0 = round(y - (i * math.sin((angle + 90)*math.pi/180)),0) Image:drawLine( x0, y0, round(x0 - (width * math.cos(angle*math.pi/180)),0), round(y0 - (width * math.sin(angle*math.pi/180)),0), grey ) end end
-
07-29-2007, 02:20 PM #7818
You could try using the gu, seems its the hot topic of today.
Speaking of which, can anyone explain the different texture filters that I found in pspgu.h?
* - GU_NEAREST
* - GU_LINEAR
* - GU_NEAREST_MIPMAP_NEAREST
* - GU_LINEAR_MIPMAP_NEAREST
* - GU_NEAREST_MIPMAP_LINEAR
* - GU_LINEAR_MIPMAP_LINEAR
What are they, how do they work, what side effects do they have (ie. speed), and whats the result?
-
07-29-2007, 03:19 PM #7819Developer

- Registriert seit
- Mar 2006
- Beiträge
- 1.026
- Points
- 7.577
- Level
- 58
- Downloads
- 0
- Uploads
- 0
You shouldn't need to change them from what Lua Player inits them to.
If you really must know, then Google for the OpenGL counterparts.
Merick: You don't need four triangles, you could do the same with two.
Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
-
07-29-2007, 04:23 PM #7820
How? the only way that I can think of for doing it with only two triangles would require using extra calculations to create coordinate offsets which would need to rotate around the center of the image in the opposite direction of the image rotation...
-
07-29-2007, 06:07 PM #7821Developer

- Registriert seit
- Mar 2006
- Beiträge
- 1.026
- Points
- 7.577
- Level
- 58
- Downloads
- 0
- Uploads
- 0
You just need to create the vertices for the outmost points, forget about the central point.
Geändert von Insert_Witty_Name (07-29-2007 um 07:12 PM Uhr)

Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
-
07-29-2007, 08:29 PM #7822Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
Whenever I use that method it rotates around the corner point instead of the center, which is usually what I want, am I doing something wrong?
Zitat von Insert_Witty_Name
-
07-29-2007, 09:41 PM #7823
i used triangle_strip and got it to run perfectly on 2 triangles. it also fixed the flicker problem.
-
07-29-2007, 09:41 PM #7824Developer

- Registriert seit
- Mar 2006
- Beiträge
- 1.026
- Points
- 7.577
- Level
- 58
- Downloads
- 0
- Uploads
- 0
See attached image.
You need to translate to the centre point of where the image will be located, and plot your vertices around that central point.
Pseudo code:
I've used a triangle fan instead of two triangles, but you get the idea.Code:drawRotatedRect(x, y, width, height, angle) ( halfWidth = width/2 halfHeight = height/2 createVert(x = -realWidth, y = -realHeight, 0) createVert(x = -realWidth, y = realHeight, 0) createVert(x = realWidth, y = realHeight, 0) createVert(x = realWidth, y = -realHeight, 0) loadIdentity() translate(x + halfWidth, y + halfHeight, 0) rotateZ(angle) draw(triangle_fan, verts) )

Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
-
07-30-2007, 01:07 AM #7825
I'm really stuck. I'm trying to create my own quiz game and everything works perfectly but I can't figure out a way to go the next question. The questions work on random numbers (as in a random number is made and the questions are shown accordingly to that random number e.g if random == 1 then) but after it says whether the answer is correct or not, it returns to the same question. Does anyone know how to fix this?
-
07-30-2007, 01:13 AM #7826words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
Post your code. For a quiz game, the general design should be something similar to having an array of structures/classes (in Lua, an array of tables). Within each structure/class members generally should be: question string, an array of answer strings and a variable saying which element in the answer array is the correct answer. Other members could include colors for each question or answer, a timer for each question so on more difficult questions they are given more time, X and Y coordinates for the question and answers to display, number of answers, etc. Then you assign a variable a random value ranging between how many elements in your question array.
Here's the general design of a quiz game I'd use. Implementing a time limit, different colors, etc. is made easy by this design. However, some designs may be made to run faster via sacrificing this ability Don't let this influence you to always use this sort of design, as there may be others out there that are better and the only way to find them is design your own.
Code:math.randomseed(os.time()); NUM_QUESTIONS = 4 NUM_ANSWERS = 4 question = {} question[1] = { name = "What is 2 + 2?", answer = {"X: 2","[]: 3","^: 4","O: 5"}, correctAnswer = 3 } question[2] = { name = "What is SG57's real name?", answer = {"X: Jordan","[]: John","^: George","O: Bob"}, correctAnswer = 1 } question[3] = { name = "How old is SG57?", answer = {"X: 12","[]: 13","^: 14","O: 15"}, correctAnswer = 4 } question[4] = { name = "How long has SG57 been programming?", answer = {"X: ~1 year","[]: ~2 years","^: ~3 years","O: ~4 years"}, correctAnswer = 2 } currentQuestion = math.random(1, NUM_QUESTIONS) -- does math.random generate a number including the smallest and biggest? so 1 and 4 may be an output? if not, change thisto 0 and 5 correct = 0 incorrect = 0 oldQuestion = 0 oldpad = Controls.read() function printCentered(y, STRING, color) screen:print( 240-string.len(STRING)/2*8, y, STRING, color) end while true do pad = Controls.read() if oldpad ~= pad then if pad:cross() then if question[currentQuestion].correctAnswer == 1 then correct = correct + 1 else incorrect = incorrect + 1 end oldQuestion = currentQuestion while currentQuestion == oldQuestion do currentQuestion = math.random(1,4) end elseif pad:square() then if question[currentQuestion].correctAnswer == 2 then correct = correct + 1 else incorrect = incorrect + 1 end oldQuestion = currentQuestion while currentQuestion == oldQuestion do currentQuestion = math.random(1,4) end elseif pad:triangle() then if question[currentQuestion].correctAnswer == 3 then correct = correct + 1 else incorrect = incorrect + 1 end oldQuestion = currentQuestion while currentQuestion == oldQuestion do currentQuestion = math.random(1,4) end elseif pad:circle() then if question[currentQuestion].correctAnswer == 4 then correct = correct + 1 else incorrect = incorrect + 1 end oldQuestion = currentQuestion while currentQuestion == oldQuestion do currentQuestion = math.random(1,4) end end end oldpad = pad screen:clear() printCentered(140,question[currentQuestion].name, Color.new(255,255,255)) for i=1,NUM_ANSWERS do printCentered(160 + i*15,question[currentQuestion].answer[i], Color.new(200,200,200)) end printCentered(10,"Correct: " .. correct .. " Incorrect: " .. incorrect, Color.new(255,255,255)) screen.waitVblankStart() screen.flip() endGeändert von SG57 (07-30-2007 um 02:13 AM Uhr)

...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
07-30-2007, 02:03 AM #7827QJ Gamer Gold

- Registriert seit
- Jul 2005
- Ort
- everywhere
- Beiträge
- 3.526
- Points
- 17.453
- Level
- 84
- Downloads
- 1
- Uploads
- 0
make sure that ur calling recalling the random variable after the player has chosen the correct answer
1. Failed....again...
2. http://slicer.gibbocool.com/ stay updated on all my projects
3. it'll be 5 years in june, that's nearly 1/4 of my life on this planet that i've visited these forums, what a ride it has been
-
07-30-2007, 12:38 PM #7828Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
Thats so simple and makes perfect sense. Thanks for that, I'm still trying to get a handle on 3d designs, My background is in VBA apps so all these coordinates give me a headache sometimes.
Zitat von Insert_Witty_Name
-
07-30-2007, 12:44 PM #7829
How do I do that?
Zitat von slicer4ever
-
07-30-2007, 03:03 PM #7830QJ Gamer Green
- Registriert seit
- Dec 2005
- Ort
- Here
- Beiträge
- 2.715
- Points
- 13.310
- Level
- 75
- Downloads
- 0
- Uploads
- 0
you have to post some code up so someone can actually help you, or the most people can really do is throw examples at you like SG57 did.
Zitat von drag_93
and what slicer4ever means (correct me if i'm wrong!) is for the variable 'random' to change its value after the player answers the question correctly so that a new one appears, but i'm assuming thats in the case that a different question appears depending on the value of 'random'.
it would really help us help if you posted some code
[edit]an example i guess would be:
get it?Code:random = 1 if random == 1 then -[post question] --if question is answered then --random = some other number --end elseif random == some other number then -[post another question] end
[CENTER][IMG]http://img148.imageshack.us/img148/6985/siglw8.jpg[/IMG][/CENTER]


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