Seite 270 von 342 ErsteErste ... 170 220 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 320 ... LetzteLetzte
Zeige Ergebnis 8.071 bis 8.100 von 10238

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...

  
  1. #8071
    lol
    Points: 20.859, Level: 91
    Level completed: 2%, Points required for next Level: 491
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    Whittier, CA
    Beiträge
    5.791
    Points
    20.859
    Level
    91
    Downloads
    0
    Uploads
    0

    Standard

    load sound

    if pad cross then
    play sound1
    elseif pad circle then
    play sound2
    end



  2. #8072
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    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()
    end
    Geändert von Nielkie (11-03-2007 um 01:56 PM Uhr)

  3. #8073
    QJ Gamer Blue
    Points: 3.563, Level: 37
    Level completed: 43%, Points required for next Level: 87
    Overall activity: 54,0%

    Registriert seit
    Sep 2007
    Beiträge
    149
    Points
    3.563
    Level
    37
    Downloads
    0
    Uploads
    0

    Standard

    Tanks a lot.

  4. #8074
    QJ Gamer Green
    Points: 6.520, Level: 52
    Level completed: 85%, Points required for next Level: 30
    Overall activity: 0%

    Registriert seit
    Nov 2005
    Ort
    Sweden
    Beiträge
    460
    Points
    6.520
    Level
    52
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von Nielkie
    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()
    end
    Does "sound1:play" really work? I made a cleaner alternative, removed unnecessary "(" and ")"s.

    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
    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"
    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]

  5. #8075
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    Sorry, I have got a habit of over-bracketing my code. Oh, and yeah, it is : play().

  6. #8076
    QJ Gamer Blue
    Points: 5.636, Level: 48
    Level completed: 44%, Points required for next Level: 114
    Overall activity: 0%

    Registriert seit
    Sep 2005
    Beiträge
    300
    Points
    5.636
    Level
    48
    Downloads
    0
    Uploads
    0

    Standard

    Does anyone here have any experience with the 3D lighting functions?

  7. #8077
    QJ Gamer Blue
    Points: 3.726, Level: 38
    Level completed: 51%, Points required for next Level: 74
    Overall activity: 27,0%

    Registriert seit
    Jun 2007
    Beiträge
    82
    Points
    3.726
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    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?

  8. #8078
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    Zitat Zitat von drag_93
    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?

    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

  9. #8079
    QJ Gamer Blue
    Points: 3.726, Level: 38
    Level completed: 51%, Points required for next Level: 74
    Overall activity: 27,0%

    Registriert seit
    Jun 2007
    Beiträge
    82
    Points
    3.726
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    thanks

  10. #8080
    Developer and Tutor.
    Points: 8.736, Level: 62
    Level completed: 96%, Points required for next Level: 14
    Overall activity: 0%

    Registriert seit
    Jul 2007
    Ort
    Widnes, England
    Beiträge
    1.649
    Points
    8.736
    Level
    62
    My Mood
    Happy
    Downloads
    0
    Uploads
    0

    Standard

    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

    Code:
    -- 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()
    i know i have a few extra ends in there at the moment but the main error araises before that

    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. #8081
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    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" loop
    Geändert von Nielkie (11-10-2007 um 04:53 PM Uhr)

  12. #8082
    QJ Gamer Bronze
    Points: 8.665, Level: 62
    Level completed: 72%, Points required for next Level: 85
    Overall activity: 0%

    Registriert seit
    Mar 2007
    Beiträge
    758
    Points
    8.665
    Level
    62
    Downloads
    0
    Uploads
    0

    Standard

    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()
    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:
    1. One of your functions is "pictouse". That means absolutely nothing. Putting a random variable in a functions is... WTF?
    2.
    Code:
    pictouse = screen:blit(244, 37, origpic)
    ???? You are trying to assign something to "pictouse" using screen:blit? Does screen:blit even return anything?
    3. As Nielkie said, why is there a random until in the middle of nowhere?
    4.
    Code:
    return pictuose = newpicture
    I'm pretty sure that is not how return statements work...

    There are an unbelievable amount of problems with your code.

  13. #8083
    Developer and Tutor.
    Points: 8.736, Level: 62
    Level completed: 96%, Points required for next Level: 14
    Overall activity: 0%

    Registriert seit
    Jul 2007
    Ort
    Widnes, England
    Beiträge
    1.649
    Points
    8.736
    Level
    62
    My Mood
    Happy
    Downloads
    0
    Uploads
    0

    Standard

    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).

  14. #8084
    Ænima
    Points: 6.447, Level: 52
    Level completed: 49%, Points required for next Level: 103
    Overall activity: 0%

    Registriert seit
    Sep 2007
    Beiträge
    587
    Points
    6.447
    Level
    52
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von FaT3oYCG
    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
    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.
    [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.

  15. #8085
    QJ Gamer Bronze
    Points: 8.665, Level: 62
    Level completed: 72%, Points required for next Level: 85
    Overall activity: 0%

    Registriert seit
    Mar 2007
    Beiträge
    758
    Points
    8.665
    Level
    62
    Downloads
    0
    Uploads
    0

    Standard

    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:
    Code:
    repeat 
    block of code
    until expression
    http://lua-users.org/wiki/ControlStructureTutorial

    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.

  16. #8086
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    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)

  17. #8087
    QJ Gamer Bronze
    Points: 8.665, Level: 62
    Level completed: 72%, Points required for next Level: 85
    Overall activity: 0%

    Registriert seit
    Mar 2007
    Beiträge
    758
    Points
    8.665
    Level
    62
    Downloads
    0
    Uploads
    0

    Standard

    You put a semi-colon at the end of your second line.
    Code:
    function load_img_scaled(image,width,height)
    	local retimg;

  18. #8088
    Developer and Tutor.
    Points: 8.736, Level: 62
    Level completed: 96%, Points required for next Level: 14
    Overall activity: 0%

    Registriert seit
    Jul 2007
    Ort
    Widnes, England
    Beiträge
    1.649
    Points
    8.736
    Level
    62
    My Mood
    Happy
    Downloads
    0
    Uploads
    0

    Standard

    thanks a load i modified it slightly to make it work but thanks

    here is the finished and working code

    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
    
    resized_image = load_img_scaled("image.png",230,136)
    
    while true do
    
    screen:blit(244, 37, resized_image)
    
    
    screen.flip()
    screen.waitVblankStart()
    
    end
    thank you so much neilkie
    ------ 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).

  19. #8089
    QJ Gamer Blue
    Points: 4.561, Level: 43
    Level completed: 6%, Points required for next Level: 189
    Overall activity: 0%

    Registriert seit
    May 2006
    Beiträge
    224
    Points
    4.561
    Level
    43
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von michaelp
    You put a semi-colon at the end of your second line.
    Code:
    function load_img_scaled(image,width,height)
    	local retimg;
    you are allowed to do that so it won't mess anything up

  20. #8090
    Points: 3.143, Level: 34
    Level completed: 62%, Points required for next Level: 57
    Overall activity: 9,0%

    Registriert seit
    Jul 2007
    Beiträge
    9
    Points
    3.143
    Level
    34
    Downloads
    0
    Uploads
    0

    Standard

    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

  21. #8091
    QJ Gamer Blue
    Points: 4.369, Level: 42
    Level completed: 10%, Points required for next Level: 181
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    246
    Points
    4.369
    Level
    42
    Downloads
    0
    Uploads
    0

    Standard

    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

  22. #8092
    Points: 3.143, Level: 34
    Level completed: 62%, Points required for next Level: 57
    Overall activity: 9,0%

    Registriert seit
    Jul 2007
    Beiträge
    9
    Points
    3.143
    Level
    34
    Downloads
    0
    Uploads
    0

    Standard

    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)?

  23. #8093
    QJ Gamer Blue
    Points: 3.726, Level: 38
    Level completed: 51%, Points required for next Level: 74
    Overall activity: 27,0%

    Registriert seit
    Jun 2007
    Beiträge
    82
    Points
    3.726
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    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?

  24. #8094
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    Zitat Zitat von nobodyimportant
    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)?
    I don't think so, but you could do this:
    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
    end
    Geändert von Nielkie (11-15-2007 um 11:10 PM Uhr)

  25. #8095
    Points: 3.143, Level: 34
    Level completed: 62%, Points required for next Level: 57
    Overall activity: 9,0%

    Registriert seit
    Jul 2007
    Beiträge
    9
    Points
    3.143
    Level
    34
    Downloads
    0
    Uploads
    0

    Standard

    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:

    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
    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.

  26. #8096
    Developer and Tutor.
    Points: 8.736, Level: 62
    Level completed: 96%, Points required for next Level: 14
    Overall activity: 0%

    Registriert seit
    Jul 2007
    Ort
    Widnes, England
    Beiträge
    1.649
    Points
    8.736
    Level
    62
    My Mood
    Happy
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von Nielkie
    I don't think so, but you could do this:
    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
    end
    making a slight change to the way nielkie set out the code would make i work

    by changing this code to

    Code:
    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
    as you cannot use a function as a paramete or inside another function as the error you got explained

    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).

  27. #8097
    QJ Gamer Blue
    Points: 4.369, Level: 42
    Level completed: 10%, Points required for next Level: 181
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    246
    Points
    4.369
    Level
    42
    Downloads
    0
    Uploads
    0

    Standard

    Why not just use something like this:

    Code:
    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
    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 lines

  28. #8098
    QJ Gamer Bronze
    Points: 5.381, Level: 47
    Level completed: 16%, Points required for next Level: 169
    Overall activity: 0%

    Registriert seit
    Jul 2006
    Beiträge
    550
    Points
    5.381
    Level
    47
    Downloads
    1
    Uploads
    0

    Standard

    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:
    Code:
     width = myfont:getTextSize(text)["width"]
    ) (from here)

    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?

  29. #8099
    QJ Gamer Blue
    Points: 4.561, Level: 43
    Level completed: 6%, Points required for next Level: 189
    Overall activity: 0%

    Registriert seit
    May 2006
    Beiträge
    224
    Points
    4.561
    Level
    43
    Downloads
    0
    Uploads
    0

    Standard

    why not just use the .20 version?

  30. #8100
    lol
    Points: 20.859, Level: 91
    Level completed: 2%, Points required for next Level: 491
    Overall activity: 0%

    Registriert seit
    Aug 2006
    Ort
    Whittier, CA
    Beiträge
    5.791
    Points
    20.859
    Level
    91
    Downloads
    0
    Uploads
    0

    Standard

    .16 is better than .20 IMO.


 

Tags for this Thread

Forumregeln

  • Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
  • Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
  • Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
  • Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
  •  





Alle Zeitangaben in WEZ -8. Es ist jetzt 09:00 PM Uhr.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © , Caputo Media, LLC. All Rights Reserved. Cluster .