That's it. And I still haven't changed it; it works. Haha.
Printable View
That's it. And I still haven't changed it; it works. Haha.
Hi everyone,
just need a little advice, i recently picked up lua for coding
(since i can't be arsed to install cygwin and all the other stuff
for coding in C for the psp), since this is a new language for me
i rely a lot on tutorials for learning how lua works, but for the problem
i have at the moment i cant find anything on the net, and the searchfunction
wasn't helpfull either...
oh, yeah, i almost forgot :-) here my problem:
i load several sets of sounds in variables which looks like this:
s1 = Sound.load("1a.wav")
s2 = Sound.load("1b.wav")
s3 = Sound.load("1c.wav")
s4 = Sound.load("1d.wav")
my problem now is, in some sound sets there might not be a "1b.wav",
my question is, how can i check if the file exists before i load it, so i can
load another file if there is none ?
greetings
Eelim
this is untested but I'm sure that upon an error luaplayer returns nil so this may work
oh ye and could you please use (code) and (/code) tags it makes things easier to read thanks - replace the brackets with square bracketsCode:if s2 == nil then
s2 = whichever default file you want it to be
end
if that works then i just had an idea this code may be better than that code as it checks for an error or not
to make it more efficient you could make a function that checks if the result is nil and if it is to use the default file and if not then load the file originally suggestedCode:if s2 == nil then
s2 = Sound.load("default.wav")
else
s2 = Sound.load("1b.wav")
end
to call that function for example you would useCode:function sound_check(variable, sound_file)
if variable == nil then
variable = Sound.load("default.wav")
else
variable = Sound.load(sound_file)
end
end
to make it so you only have to type the name of the file the code would be thisCode:sound_check(s2, "b1.wav")
to call that function for example you would useCode:function sound_check(variable, sound_file)
if variable == nil then
variable = Sound.load("default.wav")
else
variable = Sound.load("sound_file")
end
end
this would mean that if the file does not exist then it would set that variable as default.wav but if that file does exist then it will set the variable to the sound file you told it to originally and you don't have to set the variable at the beginning of the program i don't think so then with this code you could just useCode:sound_check(s2, b1.wav)
all of the above are untested if they don't work come back here to get some more helpCode:sound_check(s2, b1.wav)
s2:play()
thanks
No. You would type
Code:if s1 = nil then
put rest of code here
ok sorry i will change that thanks
EDIT:
revised and improved code with more options
thanks
sorry, thats not it, your code would work if i just don't know if i have
loaded a file into the variable or not.
But the error occurs while trying
to load a file, not while trying to play it.
stops the player if the file is not on the memorystick, so i can't check ifCode:s2 = Sound.load("sound2.wav")
s2 would be nil, cause the has player already stopped.
so what i need is either
a) The syntax to access the file system and look if the filename exists
or
b) the syntax for global errorhandling so i can prevent the player from halting.
greetings
Eelim
hmm i have had many problems with music files and such before sometimes i dont know why but if you load a file e.g. a music file or a picture file after line 32 it crashes so separate your files into different lua files for example
sounds.lua - load all sounds in this file
pictures.lua - load all pictures in this file
and then in your main file normally your index.lua put
dofile("sounds.lua")
dofile("pictures.lua")
and it should work if not it is probably an error because of how long the file takes to load
as you cannot do something like this
s2 = Sound.load("sound2.wav")
s2:play()
also this code
will check if the file exists or not - just call the function and pass the variable name and file name and it will see if the file is there if it isn it returns nil so then will handle the error accordingly to the code thereafterCode:function sound_check(variable, sound_file)
if variable == nil then
variable = Sound.load("default.wav")
else
variable = Sound.load("sound_file")
end
end
to handle errors the error will display a message to the user but a nil statement will be passed before hand so you can ask if nil has been passed as shown above
doesn't work,
if i use that code it always plays the default file, no matter if
the wanted file exists or not, cause since the variable is "new"
it is always nil, if i declare it before i call the function the player
stops if my wanted file does not exist. if i declare the variable
with a placeholder it is never nil, now the player tries again to
load the file and stops if it doesnt exist.
so again:
i need to know if the file exists on the memory stick BEFORE i
try to load it with "Sound.load".
how do i do that??
greetings
Eelim
well you could replace that function to detect a file it is quite simple you could have modified it yourself but here it is
Code:function file_check(path)
if path == nil then
s2 = nil
end
end
and then you could just leave s2 as a blank variable
or change it to a default file
hope that helps come back if not
thanks
Eldiablov - No. It's
In comparisons a double = is used. Single is for setting a variable equal to something.Code:if blah == nil
However, that's not even the best way to do it. A better way to do it would be
Code:if not blah
dont worry tux i realized that it was two not one thanks ne way and i think it was because that is what i originally had mine as but then i changed it
and how would we use an if not statement for this situation - enlighten me please
He already gave you a example. Use your Lua skills since your an expert now.
i didn't find if not blah very useful i only asked how it could be used if it makes you fell better ill ask again in more detailZitat:
Zitat von Anti-QuickJay
hi TuxThePenguin could you please give me a detailed example of how the if not statement could be used to find out if a file exists
thanks
happy now A-QJ ??????
Learn how to check if a file exists. Then learn how to use a if not statement.
Learn Lua.
Now I'm happy :)
hi, me again
this cannot work, same reasons as above, you are telling theCode:function file_check(path)
if path == nil then
s2 = nil
end
end
variable it is something when you call the function, so it is never nil,
and it does not check the memory stick if a file exists or not.
what i want to do is a "File Operation" on the "File System", classical
Input Output operations, and after more than 10 hours of searching
the net, i finally found the syntax for doing so.
so here is the code to check if a file exists on the memorystick or not
if someone should happen to have the same problem
greetingsCode:
function file_check(filename)
f = io.open(filename)
if f == nil then
screen:print(100,100,"No such file exists")
else
screen:print(100,100,"File Found")
f:close()
end
end
file_check("test.wav")
Eelim
i can cope with thatZitat:
Zitat von Anti-QuickJay
well done Eelim for solving your problem
and thanks for posting the answer not many people bother too
Eelim i dont know if you will check this but i feel so stupid now that i know your problem and the result that you wanted this will let you check for a file
so to make a file check functionCode:assert()
by using assert there is no need to actually open the file or close it so it should be much quickerCode:function file_check(file_path)
file = assert(io.open(file_path))
if file == nil then
screen:print(10, 10, "the file path was invalid")
else
screen:print(10, 10, "the file path was valid")
end
end
you can also return custom error messages using this command and create an error handler for your programs
- i dont have an example error handler but this is sort of how assert is used
this code means that if the condition is met then it will carry out the assert command so say if file == nil then you would useCode:if condition then
assert(test, "message")
end
if there is no message then the command can be used to check values and will return either false or nil so if the error or condition is false it wont be executed, but if it is nill it will assert the command givenCode:if file == nil then
assert(file == nil, "this file is invalid")
end
- an example of this is the code used at the top of my post
that is the part of the code that is seen as the test
in the top post no message was passedCode:io.open(file_path)
the error messages come out the same was as if an error is returned in lua player with a black screen and a white error message
thanks
hi,
i just didn't know lua had an assert command, for the things
i do now, it's ok the way i do it, cause if the file exists i have to
open it anyways, but for more complex structures assert is a
big win.
thx for the info!!!
i am a software engineer for over 10 years now, and i know several languages,
but i am new to lua, and one of my biggest problems is, i can't find
anything decent on the net on lua syntax, sure there are thousands
of tutorials, but 99,9% deal with the basic hello world, almost no
advanced tutorials, and if you manage to find something more advanced,
its usually unreadable (explaining every line of code, but not displaying,
the whole thing, so that you loose track of what the code actually does,
or yellow text on white background.. stuff like that..)
but anyways, thx a again for the assert info, that is a big help
greetings
Eelim
You've been a software engineer for over 10 years, and can't figure out something as simple as going to Lua.org and learning the syntax? That's pretty hard to believe.Zitat:
Zitat von Eelim
VERY hard to believe
Anyway you could go to www.evilmana.com for information on lua etc
~Sullivan~
on lua.org everything concerning the io system has a big fat "TODO"
written under the topic, so no help there, evilmana is nice,
but not very readable (but good tutorials none the less), but
also nothing on the io system.
i wouldn't have asked here, if it was as simple as going to lua.org, think before you diss.
greetings
Eelim
not to offend you but where have you been looking dont use lua.org use the official lua users wiki
http://lua-users.org/wiki/IoLibraryTutorial
go here for all of the directories and examples - most are filled in
http://lua-users.org/wiki/TutorialDirectory
IO defo is
http://www.lua.org/manual/5.1/manual.html#5.7Zitat:
Zitat von Eelim
Lua.org -> Documentation -> Reference Manual -> IO
http://www.lua.org/pil/21.html
Lua.org -> Documentation -> Programming in Lua -> The first edition is available online! -> The IO library
http://lua-users.org/wiki/IoLibraryTutorial
Lua.org -> Community -> Wiki -> LuaTutorial -> TutorialDirectory -> IoLibraryTutorial
http://www.evilmana.com/tutorials/lua_tutorial_09.php
Evilmana.com -> Tutorials -> Lua for Beginners -> Reading and Writing Files
ok then do use lua.org its more specific thanks yaustar we can always count on you
I'm hoping someone can help me out here :)
This script works perfect on LUAplayer on my computer, but when testing it on the PSP it's a bit flawed.
What this does is read the color of the base map (PNG is not attached, but any PNG image image will do that's 480*272 and is only Black and white, other colors won't be seen anyway :p) or you could always replace it with any other shape blitted to the screen instead of loading the image. According to the collor spotted it will either "collide" and not be able to move, or get damaga or heal.
It's something I'm working on so people who don't know LUA can easely "paint" their own levels with collision detection.
The only problem is, the collisions get detected a bit slow on the PSP. On the computer it works just fine, but on PSP sometimes it detects the collision as soon as you enter a black field, but somtimes it detects it afther a few pixels and you will get stuck as a result :(
It seems to be happening most when you have been moving around for a few seconds without colliding with an object, when you stand still in front of an object and then press an arrowbutton it'll collide instantly.
Does anyone know how to solve this?
Spoiler for Collision Script:
Instead of having so many variables, use more functions.
This could help:
Code:function CollisionCheckLineColour(x1,y1,x2,y2,colour,img) --Only works with straight lines
img = img or screen
local colliding = false
local test1;
local test2;
local NotTest;
local step;
if x1 == x2 then
test1 = y1
test2 = y2
NotTest = x1
elseif y1 == y2 then
test1 = x1
test2 = x2
NotTest = y1
end
if test1 < test2 then
step = 1
else
step = -1
end
for i=test1,test2,step do
if img:Pixel(NotTest,i) == colour then
colliding = true
end
end
return colliding
end
function CollisionCheckBoxColour(x,y,width,height,colour,img)
if CollisionCheckLineColour(x,y,x + width,y,colour,img) or
CollisionCheckLineColour(x + width,y,x + width,y + height,colour,img) or
CollisionCheckLineColour(x,y + height,x + width,y + height,colour,img) or
CollisionCheckLineColour(x,y,x,y + height,colour,img) then
return true
else
return false
end
end
Thx Nielke, I'll take a look at it tomorow, it's a tad to late right now for me ^^ Thx tho :D
Does anyone think they could convert this into an EBoot for me?
http://forums.qj.net/f-psp-developme...ml#post1915912
It needs LUAPlayer 0.16 MOD4. Last time I tried doing it, it didn't work at all. ;_;
These should work. I haven't tested.
http://www.sendspace.com/file/uvn88u
Hi folks !
Im trying to make a little game, now i'm working on the beginmenu. I'm trying to load a background an tell the psp that if the cross button is pressed it should do file ./game.lua . But i get this error : Bad argument #2 to blit (image expected, got nil)
This is the code :
I really hope you guys can help me !Zitat:
white = Color.new(255,255,255)
red = Color.new(255,0,0)
blue = Color.new(0,0,255)
orange = Color.new(255, 102, 0)
background = Image.load("images/begin.jpg")
oldpad = Controls.read()
while true do
pad = Controls.read()
screen:clear()
screen:blit(0,0,begin, true)
if pad:cross() then
dofile("./game.lua")
end
screen.waitVblankStart()
screen.flip()
end
Cheers.
You need the right variable name :SCode:white = Color.new(255,255,255)
red = Color.new(255,0,0)
blue = Color.new(0,0,255)
orange = Color.new(255, 102, 0)
background = Image.load("images/begin.jpg")
oldpad = Controls.read()
while true do
pad = Controls.read()
screen:clear()
screen:blit(0,0,begin)
if pad:cross() then
dofile("./game.lua")
end
screen.waitVblankStart()
screen.flip()
end
OMFG xD
How the f*** did'nt I see that ? xD
Thx Yongobongo :tup:
Thanks alot QuickJay! ^_^
I've completed the game (almost :P ) , now I want a music file playing in the main-menu. I tried everything but it says : "cant open sound at ......... " or "error loading sound" . I tried both formats ( .xm & .wav ) , but it keeps giving me the same error :s
EDIT : I figured it out and i dont get any errors. But how does it come that I dont hear any sound of the song ??
the first error you mentioned means an incorrect file path and the second is probably because you need to have a popper .wav file not just take an .mp3 for example and change the extension to .wav as this is still an mp3 file which cannot be played with sound:play()
and the answer to your edit
TURN THE VOLUME UP !!!!!!!!!!!!!
Alright thx !Zitat:
Zitat von FaT3oYCG
BTW : Lol, the volume was already turned up.
EDIT : Last question : The song lags when it starts to play , why ??
it depends what your program is doing at the time it starts playing increasing the cpu speed will allow the song to play without lagging so either get the lua player with cpu speed functions or tell me what version you want and i will make you one with cpu functions and an example
Simple request
how i can see if a file exists?
are you blind a quote from me - reference post 8177Zitat:
Zitat von FaT3oYCG
Unfortunately, not everyone has the time to read through the entire thread. This results in many, many duplicate questions.
You may want to post snippets like that in the scripts/examples forum as well, as long as it hasn't already been posted there.