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; Zitat von Urameshi i already have a chasePlayer function though, do i make another? Well I dont see it whats ...
-
03-24-2007, 07:50 AM #6361
- Registriert seit
- Sep 2006
- Beiträge
- 484
- Points
- 8.718
- Level
- 62
- Downloads
- 0
- Uploads
- 0
Well I dont see it whats your error or prob anyways?
Zitat von Urameshi
-
03-24-2007, 07:53 AM #6362QJ Gamer Gold
- Registriert seit
- Nov 2006
- Ort
- ...
- Beiträge
- 2.080
- Points
- 11.942
- Level
- 71
- Downloads
- 0
- Uploads
- 0
well the game runs, but when Urameshi chases the player, he shows no animation what so ever, but i want him to show animation
-
03-24-2007, 08:29 AM #6363QJ Gamer Green
- Registriert seit
- Jan 2007
- Ort
- beside my PC
- Beiträge
- 520
- Points
- 6.446
- Level
- 52
- Downloads
- 0
- Uploads
- 0
Zitat von myschoo
hmm can u tell me what did u repair?...
i dont have a psp right now
i gave my psp to my bro as a graduation gift:)
-
03-24-2007, 12:02 PM #6364QJ Gamer Green
- Registriert seit
- Jul 2006
- Ort
- Middle Europe
- Beiträge
- 1.281
- Points
- 11.800
- Level
- 71
- Downloads
- 0
- Uploads
- 0
only 2 things:
sometimes you used Red and sometimes red in your menu so i changed it all to "red" and then you were flashing font to flash0:/vsh/font/ and thats not right[1 Year QJ Member]
[LUA Coder and C Learner]
[Ball Revamped Clone v0.1]
[Phil's Shooting Range v0.3]
[HideFile PRX v2]
[SSR PRX v1.1]
-
03-24-2007, 01:13 PM #6365
try either using animLib, or do something like:
Zitat von Urameshi
Code:function animateweirdnamedguy() movingstate = movingstate + 1 if movingstate>= 48 or movingstate<=0 then movingstate = 0 elseif movingstate < 12 then player.image = right1 elseif movingstate < 24 then player.image = right2 elseif movingstate < 36 then player.image = right3 elseif movingstate < 48 then player.image = right4 end end
-
03-24-2007, 05:10 PM #6366QJ Gamer Green
- Registriert seit
- Jan 2007
- Ort
- beside my PC
- Beiträge
- 520
- Points
- 6.446
- Level
- 52
- Downloads
- 0
- Uploads
- 0
Zitat von myschoo
u mean i can't flash fonts?
whats wrong with the font script?
-
03-24-2007, 10:44 PM #6367
- Registriert seit
- Dec 2006
- Beiträge
- 18
- Points
- 3.542
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Hey, hope someone can help me out on this. I started to learn lua 2 days ago. After going through some tutorials, i thought id start with a pong game.
so i coded everything, did some graphics and now i have one problem:
My game has a menu with 4 choices. 2 player mode, survival mode, credits and quit. (not everything is finished yet).
everytime i start one of the modes, get out of it and try another one, get out of that one and try to start another one the third time, the psp freezes. :Argh:
Why is it doing that? hope someone knows a solution.Geändert von Cynical (03-26-2007 um 05:18 PM Uhr)
-
03-25-2007, 12:24 AM #6368words 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
Cynical - I havanet looked at your code, but my guessis your not managing your resources correctly. You need to make sure you set ALL variables used in your source to 'nil', without hte quotes, before 'dofile'ing to another script (in your case that is). Than you need to call collectgarbage() to erase all traces of you varaibles from RAM.
ex:
Code:image = Image.load("bg.png") function Cleanup() image = nil collectgarbage() end while true do screen:blit(0,0,image) pad = Controls.read() if pad:cross() then Cleanup() dofile("game.lua") end screen.waitVblankStart() screen.flip() end
...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
-
03-25-2007, 01:12 AM #6369Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
Yeah, its definately a memory issue, whats happening is you're loading the same images over and over without releasing them. What I usually like to do is set all my loads into a function, i.e.
function loadmultiplayergfx()
(load all your images)
end
and the immediately create an unload function, i.e.
function unloadmultiplayergfx()
(set all your images to null and collect garbage)
end
This way its as easy as calling the load at the beginning of the mode, and unloading as you leave the mode. Plus in your loading function you can blit a loading screen, making your app that much more presentable. Also remember that if you store images into an array of any type, you have to null the array before you collect garbage or lua actually won't free the memory.
-
03-25-2007, 01:20 AM #6370words 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
Oh - good point. Make sure you free any references to any variable. By this, i mean:
Klepto - I did the same as well in my Call of Duty 2 - PSP Edition. My unload function was quite long asthe Lua doesnt support the multiple variable setting (var1 = var2 = var3 = nil doesnt work). I didnt know about the collectgarbage function until i read the Lua manual. Good thing too *sigh of relief*Code:bg1 = Image.load("bg.png") -- why waste time reloading the same image? just feed the same image data into a new image variable bg2 = bg1 ... function Cleanup() bg1 = nil -- collectgarbage() <- BOO BOO! bg2 still is referencing to bg1, and freeing bg1 will have bg2 reference to nothing (nothing as in, not an 'Image' type) bg2 = nil collectgarbage() end
...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
-
03-25-2007, 04:21 AM #6371QJ Gamer Green
- Registriert seit
- Jul 2006
- Ort
- Middle Europe
- Beiträge
- 1.281
- Points
- 11.800
- Level
- 71
- Downloads
- 0
- Uploads
- 0
font is located in flash0:/font/ not flash0:/vsh/font/ ;)
Zitat von ai3gtmc
-= Double Post =-
that collectgarbage() function is inside lua player or you have to create it??
Zitat von SG57
Geändert von myschoo (03-25-2007 um 04:54 AM Uhr) Grund: Automerged Doublepost
[1 Year QJ Member]
[LUA Coder and C Learner]
[Ball Revamped Clone v0.1]
[Phil's Shooting Range v0.3]
[HideFile PRX v2]
[SSR PRX v1.1]
-
03-25-2007, 05:01 AM #6372QJ Gamer Green
- Registriert seit
- Jan 2007
- Ort
- beside my PC
- Beiträge
- 520
- Points
- 6.446
- Level
- 52
- Downloads
- 0
- Uploads
- 0
Zitat von myschoo
oh man thx:) u rock:Punk:
-
03-25-2007, 05:06 AM #6373QJ Gamer Green
- Registriert seit
- Jul 2006
- Ort
- Middle Europe
- Beiträge
- 1.281
- Points
- 11.800
- Level
- 71
- Downloads
- 0
- Uploads
- 0
but still does it work?
[1 Year QJ Member]
[LUA Coder and C Learner]
[Ball Revamped Clone v0.1]
[Phil's Shooting Range v0.3]
[HideFile PRX v2]
[SSR PRX v1.1]
-
03-25-2007, 05:29 AM #6374QJ Gamer Green
- Registriert seit
- Jan 2007
- Ort
- beside my PC
- Beiträge
- 520
- Points
- 6.446
- Level
- 52
- Downloads
- 0
- Uploads
- 0
Zitat von myschoo
hmm i still dont have a psp
-
03-25-2007, 05:39 AM #6375QJ Gamer Green
- Registriert seit
- Jul 2006
- Ort
- Middle Europe
- Beiträge
- 1.281
- Points
- 11.800
- Level
- 71
- Downloads
- 0
- Uploads
- 0
ok , tell me then
[1 Year QJ Member]
[LUA Coder and C Learner]
[Ball Revamped Clone v0.1]
[Phil's Shooting Range v0.3]
[HideFile PRX v2]
[SSR PRX v1.1]
-
03-25-2007, 07:53 AM #6376
- Registriert seit
- Dec 2006
- Beiträge
- 18
- Points
- 3.542
- Level
- 37
- Downloads
- 0
- Uploads
- 0
Zitat von SG57
Oh ok, I kinda suspected it had something to do with the the memory. (at one point I got a "mikmod critical error, out of memory"). Anyways,
should i also do the garbage collecting for variables that contain for examples just integers or strings? or is it only necessary for variables containing images?
I will try some garbage collection right now, thanks for the answers :)
@ Klepto - do you mean it like this:
function loadgfx()
image = Image.load("image.png")
end
function unloadgfx()
image = nil
collectgarbage()
end
loadgfx()
while true do
...
unloadgfx
dofile "..."
end
EDIT:
Now, the problem, that the psp freezes the third time trying to load a mode, is solved. but now i have the problem, that after a few times i try to load a mode, i get the following error:
LuaPlayer's mikmod has a critical error:
_mm_critical 0
_mm_errno 5
Sample load failed - Out of memoy
???Geändert von Cynical (03-25-2007 um 10:13 AM Uhr)
-
03-25-2007, 04:11 PM #6377
I've been looking into coroutines, can someone tell my why this isn't working?
when the program gets to the coroutine.resume it exits with the error:Code:chain = {} chain.img = {} chain.img[1] = Image.load("./gfx/tiles/1.png") chain.img[2] = Image.load("./gfx/tiles/2.png") chain.img[3] = Image.load("./gfx/tiles/3.png") chain.head = {x = 30, y = 30} chain.anchor = {x = 240, y = 136} chain.segment = {} chain.segment[1] = coroutine.create(function (head, anchor) local newX = ((head.x + anchor.x)/2)+ math.random(-3, 3) local newY = ((head.y + anchor.y)/2) + math.random(-3, 3) screen:blit(newX, newY, chain.img[2]) coroutine.yield() end) loop = true while loop do pad = Controls.read() if pad:left() and chain.head.x > 10 then chain.head.x = chain.head.x -1 end if pad:right() and chain.head.x < 470 - chain.img[1]:width() then chain.head.x = chain.head.x +1 end if pad:up() and chain.head.y > 10 then chain.head.x = chain.head.x -1 end if pad:down() and chain.head.y < 262 - chain.img[1]:height() then chain.head.x = chain.head.x +1 end screen:clear() coroutine.resume(chain.segment[1](chain.head, chain.anchor)) screen:blit(chain.head.x, chain.head.y, chain.img[1]) screen:blit(chain.anchor.x, chain.anchor.y, chain.img[3]) screen:waitVblankStart() screen:flip() if pad:start() then loop = false end end
attempt to call field '?' (a thread value)
-
03-25-2007, 11:46 PM #6378Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
From what I read, the benefit of coroutines is that it allows you to stop a function, and resume it at a later time. Concerning your code, when you call the resume, you cannot pass new variables, you simply must pass the thread name, ie.
coroutine.resume(chain.se gment[1])
another problem with your code is that your yield is at the end of your coroutine, so basically once its resumed, it runs through completely and is then killed. You will have to create a new thread if you want to have it run again, so in a sense you have just created a standard function.
Does anyone know how lua handles dead thread by the way?
-
03-26-2007, 02:59 AM #6379
Actually, according the "Programming in Lua" you can pass arguments. And looking at that page again, I see what I did wrong. I was trying to pass the arguments inside brackets like you would with a normal function, but when resuming coroutines you're already inside a set of brackets so you just use commas, like this:
And as for putting yield at the end, the simple solution too keep the thread from dying is to put the whole code block into a loop.Code:coroutine.resume(chain.segment[1](chain.head, chain.anchor)) -- bad coroutine.resume(chain.segment[1], chain.head, chain.anchor) -- good
According to what I've read, resuming a coroutine takes up less processor time than calling a regular function so I was thinking that they would be perfect for keeping track of enemy ai
-
03-26-2007, 03:21 AM #6380Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
gotcha, the loop makes a lot of sense. I'll have to read up some more on this, any idea as to how much of a performance gain you would achieve by using this method?
-
03-26-2007, 03:32 AM #6381
No idea really, and in fact it might be possible that I mis-read something.
-
03-26-2007, 07:36 AM #6382
If I receive data from the ir port, how can I print it to the screen and how do I save it to a file?
-
03-26-2007, 08:14 AM #6383I'm Baaaack!

- Registriert seit
- May 2006
- Ort
- Nowhere
- Beiträge
- 2.186
- Points
- 17.067
- Level
- 83
- Downloads
- 0
- Uploads
- 0
Well, I've never used the IR port, but I assume that it's stored in a variable. So, to print it, do something like this:
To write it to a file:Code:foo = IR.read() --not actual function, just used as example screen:print(10,10,foo,black)
Code:file = io.open("file.txt","w") file:write(foo) file:close()
-
03-26-2007, 09:05 AM #6384Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
Page 598, IR was discussed a little bit. Heres the link:
Link
-
03-26-2007, 10:52 AM #6385
Thanks!
-= Double Post =-
OK, but how can I read from a saved file and send the content of this file over ir?Geändert von sony psp player (03-26-2007 um 11:36 AM Uhr) Grund: Automerged Doublepost
-
03-26-2007, 12:27 PM #6386
file = io.open("file.txt","r")
System.irdaSend(file:read ("*a"))
file:close()
I forgot the IR function.牧来栠摩琠敨映汩獥
PSN: youresamFrom Earth the Frozen Ipaqs shall rise and be silenced and all will live free.
--Mike Hollingsworth
-
03-26-2007, 03:36 PM #6387
I've been struggling with this since yesterday...
What I want to do is have the Move 25 pixels event happen 3 times over a period of 1 second...not have the character move 75 pixels instantly...
I've tried timers and counters and the animation script but I only started coding 4 days ago and still don't know how to get this to work. I wish Lua worked like RPG Maker where you could just go "Wait(100)" for it to wait a second before proceeding...
Code:--MOB COLLISION-- if MobX - 15 <= CleoX and CleoX <= MobX + 10 and MobY - 35 <= CleoY and CleoY <= MobY + 15 then Kill:play( ) if cleodown then CleoY = CleoY -25 end if cleoleft then CleoX = CleoX +25 end if cleoright then CleoX = CleoX -25 end if cleoup then CleoY = CleoY +25 end screen:blit(CleoX,CleoY,cleo[direction]) HP = HP - 1 end
-
03-26-2007, 04:26 PM #6388Developer

- Registriert seit
- Oct 2006
- Ort
- San Diego
- Beiträge
- 177
- Points
- 4.205
- Level
- 41
- Downloads
- 0
- Uploads
- 0
screen:waitVblankStart(60 ) will pause the screen for 1 second. just make sure you slip the screen in between each call.
Zitat von ClearTranquil
-
03-26-2007, 04:28 PM #6389
What do you mean slip the screen?
-
03-26-2007, 04:30 PM #6390
i think he meant flip


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