Seite 321 von 342 ErsteErste ... 221 271 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 ... LetzteLetzte
Zeige Ergebnis 9.601 bis 9.630 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; Instead of asking, why don't you have a look at the Lua Manual ? Notably the chapters on visibility, variables, ...

  
  1. #9601
    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

    Instead of asking, why don't you have a look at the Lua Manual? Notably the chapters on visibility, variables, and functions.


    Geändert von Nielkie (12-19-2008 um 02:45 AM Uhr)

  2. #9602
    QJ Gamer Bronze
    Points: 5.583, Level: 48
    Level completed: 17%, Points required for next Level: 167
    Overall activity: 0%

    Registriert seit
    May 2007
    Beiträge
    127
    Points
    5.583
    Level
    48
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von chrisp6825 Beitrag anzeigen
    I know that if there is a way to do that, then that isn't the way to do it.
    But can you do this? Or do functions already make arguments local?
    why not just change the name of the parameter so your not using 'x' again?
    -- Code Monkey : Sarien, Fishguts, Cracks and Crevices --

    "Did IQ's just drop sharply while I was away?" (Ripley)

  3. #9603
    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 _df_ Beitrag anzeigen
    why not just change the name of the parameter so your not using 'x' again?
    So instead of getting an answer to his question and learning more about scoping in programming, he should just blindly work around a potential problem? No.

  4. #9604
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    functions automatically make variables local. take out the "local " and it will work as expected.
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]

  5. #9605
    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

    but also when you modify a variable inside of a function it needs to be set to local, if it is a function argument then it is automatically local like TP said but when you want to modify the value from within the function this would be appropriate

    Code:
    x = 15
    
    function blah(x)
        local x = x
        x = x + 1
        screen:print(0, 0, x)
    end
    so that x can be modified to a new value but not affect the origional value outside of the function
    ------ 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).

  6. #9606
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    no, no you do not need to. local variables take precedence over globals when you are inside their scope.
    run this code:
    Code:
    x = 15
    
    function blah(x)
        x = x + 1
        print(x)
    end
    
    blah(3)
    blah(4)
    print(x)
    x = x + 1
    print(x)
    blah(5)
    here:
    http://www.lua.org/cgi-bin/demo

    and it will work as I expect it to, with no need to redeclare variables inside a function as locals.
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]

  7. #9607
    QJ Gamer Green
    Points: 3.567, Level: 37
    Level completed: 45%, Points required for next Level: 83
    Overall activity: 0%

    Registriert seit
    May 2008
    Ort
    The Netherlands
    Beiträge
    330
    Points
    3.567
    Level
    37
    Downloads
    0
    Uploads
    0

    Standard

    Somebody knows what's wrong with this code. I'm trying to make a mouse cursor but my screen stays black:

    Code:
    cursor = Image.load("./system/images/general/cursor.png")
    background = Image.load("./system/images/general/desktop.png")
    
    cursor = {}
    cursor.x = 240
    cursor.y = 136
    
    cursorspeed = io.open("./system/cursor.txt", "r")
    speed = cursorspeed:read()
    cursorspeed:close()
    
    actuel = speed
    
    while true do
    function main_cursor()
    if pad:analogY() < -64 and muis.y > 0 then
    cursor.y = cursor.y - speed
    end
    if pad:analogY() > 64 and muis.y < 260 then
    cursor.y = cursor.y + speed
    end
    if pad:analogX() > 64 and muis.x < 470 then
    cursor.x = cursor.x + speed
    end
    if pad:analogX() < -64 and souris.x > 0 then
    cursor.x = cursor.x - cursor
    end
    end
    
    function cursor_example()
    if cursor.x >= 0 and cursor.x <= 480 and cursor.y >= 0 and cursor.y <= 272 then
    --
    end
    
    screen:clear()
    pad = Controls.read()
    screen:blit(0,0,background)
    main_cursor()  
    cusor_example()
    screen:blit(cursor.x,cursor.y,cursor)
    screen.waitVblankStart()
    oldpad = pad
    screen.flip()
    end
    end

  8. #9608
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    all youre doing is redeclaring the functions over and over. you need to call them, simply by doing thefunctionname()

    declare the functions (function blah() blah blah end) outside the main while true do loop and then call them (blah()) inside
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]

  9. #9609
    Points: 3.116, Level: 34
    Level completed: 44%, Points required for next Level: 84
    Overall activity: 0%

    Registriert seit
    Nov 2007
    Ort
    Germany
    Beiträge
    3
    Points
    3.116
    Level
    34
    Downloads
    0
    Uploads
    0

    Standard

    Hey, how do I use the Mp3me.info() Fuction in LPHMv2. Thanks for your help.

  10. #9610
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    I would recommend asking on HM's forums if his nonexistent documentation didn't explain it to you.
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]

  11. #9611
    QJ Gamer Bronze
    Points: 5.826, Level: 49
    Level completed: 38%, Points required for next Level: 124
    Overall activity: 0%

    Registriert seit
    Aug 2007
    Ort
    Everywhere
    Beiträge
    206
    Points
    5.826
    Level
    49
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von TurtlesPwn Beitrag anzeigen
    functions automatically make variables local. take out the "local " and it will work as expected.
    Thanks man, this is basically what I was looking for.



    Zitat Zitat von _df_ Beitrag anzeigen
    why not just change the name of the parameter so your not using 'x' again?
    I know this sounds a bit far fetched, but sometimes it's more convenient not to.


    So...
    Code:
    x = 15
    
    function blah(x)
        x = x + 1
        print(x)
    end
    
    blah(3) -- prints 4
    blah(4) -- prints 5
    print(x) -- prints 16
    x = x + 1
    print(x) -- prints 16?
    blah(5) -- prints 6
    Is that right?


    Sorry I didn't respond earlier. My internet was down, and kinda still is.
    Pokemon Islands in Development : Qj Forum | Google Forum | Probords Forum

  12. #9612
    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

    Code:
    x = 15
    
    function blah(x)
        x = x + 1
        print(x)
    end
    
    blah(3) -- prints 4
    blah(4) -- prints 5
    print(x) -- prints 16
    x = x + 1
    print(x) -- prints 16?--Would print 17, as the global x was incremented to 16
    blah(5) -- prints 6

  13. #9613
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    actually it goes
    4
    5
    15
    16
    6

    print doesn't add 1 to x like blah does.
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]

  14. #9614
    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

    Oh, right right right.

    I didn't notice "print", I feel silly :-(

  15. #9615
    QJ Gamer Bronze
    Points: 5.826, Level: 49
    Level completed: 38%, Points required for next Level: 124
    Overall activity: 0%

    Registriert seit
    Aug 2007
    Ort
    Everywhere
    Beiträge
    206
    Points
    5.826
    Level
    49
    Downloads
    0
    Uploads
    0

    Standard

    Hah oh yeah.. me too
    Thanks for clearing that up guys. This helps me a bit.

    Edit:
    Ok, what if you wanted to actually change the global 'x' from within the 'blah' function?
    Would you use something like 'return print(x)' or 'print(x); return x' ? Would one of those work?

    Also, what happens if you blit too far off screen? Does it actually blit it?
    Like, if I blit 4,000 images with an x coordinate of 500, would they take up any video memory or anything? What would it do?
    Geändert von chrisp6825 (12-21-2008 um 02:33 PM Uhr)
    Pokemon Islands in Development : Qj Forum | Google Forum | Probords Forum

  16. #9616
    Points: 3.116, Level: 34
    Level completed: 44%, Points required for next Level: 84
    Overall activity: 0%

    Registriert seit
    Nov 2007
    Ort
    Germany
    Beiträge
    3
    Points
    3.116
    Level
    34
    Downloads
    0
    Uploads
    0

    Standard

    @TurtlesPwn: The LuaPlayerHM Forums seems to be down at the Moment (SQL Error) and I still have no Idea how to use the Mp3me.info() function...

  17. #9617
    Banned for LIFE
    Points: 18.744, Level: 86
    Level completed: 79%, Points required for next Level: 106
    Overall activity: 0%

    Registriert seit
    Oct 2006
    Ort
    East London, England
    Beiträge
    2
    Points
    18.744
    Level
    86
    Downloads
    0
    Uploads
    0

    Standard

    Best advice would be to switch away from HM if the developer is too lazy to provide docs.

  18. #9618
    xMod.
    Points: 4.576, Level: 43
    Level completed: 13%, Points required for next Level: 174
    Overall activity: 0%

    Registriert seit
    Oct 2008
    Ort
    Melbourne, Australia
    Beiträge
    675
    Points
    4.576
    Level
    43
    My Mood
    Daring
    Downloads
    0
    Uploads
    0

    Standard

    any way to access (i.e read&write) to the idstorage from lua?

    ~!SlasheR~!

  19. #9619
    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

    No

  20. #9620
    QJ Gamer Bronze
    Points: 7.257, Level: 56
    Level completed: 54%, Points required for next Level: 93
    Overall activity: 0%

    Registriert seit
    Dec 2007
    Ort
    B.F.
    Beiträge
    328
    Points
    7.257
    Level
    56
    My Mood
    Psychedelic
    Downloads
    1
    Uploads
    0

    Standard Help About table.remove() function

    I've been unavailable for a while, but now i'm back to lua coding.
    i'm looking for some tuts about tables as dictionnaries...
    This concern my RTS game "Age of nations"..remember ?

    here is the basis

    Code:
    units = {}
    unit[1] = {x = 10, y=10, health = 100}
    unit[2] = {x = 20, y=30,health = 100}
    so i've defined two units, with their own x,y position on a map, and their own health.
    I want to make it so when any unit is dead (so when its health reaches 0) the unit disappears from the map, and its table is cleaned from memory...

    The only way i found to do it doesn't works.It returns an error.
    I wrote in the main loop:

    Code:
    for i=1,#units do     if unit[i].health <= 0 then table.remove(units,i) end   end
    how can i fix that ?

  21. #9621
    Banned for LIFE
    Points: 18.744, Level: 86
    Level completed: 79%, Points required for next Level: 106
    Overall activity: 0%

    Registriert seit
    Oct 2006
    Ort
    East London, England
    Beiträge
    2
    Points
    18.744
    Level
    86
    Downloads
    0
    Uploads
    0

    Standard

    Whats the error?

    Does #units actually return the number of units ?

  22. #9622
    QJ Gamer Bronze
    Points: 7.257, Level: 56
    Level completed: 54%, Points required for next Level: 93
    Overall activity: 0%

    Registriert seit
    Dec 2007
    Ort
    B.F.
    Beiträge
    328
    Points
    7.257
    Level
    56
    My Mood
    Psychedelic
    Downloads
    1
    Uploads
    0

    Standard

    yeah it does..the fact is that the function table.remove() doesn't works for tables as dictionnaries...


    any idea ?

  23. #9623
    QJ Gamer Green
    Points: 3.567, Level: 37
    Level completed: 45%, Points required for next Level: 83
    Overall activity: 0%

    Registriert seit
    May 2008
    Ort
    The Netherlands
    Beiträge
    330
    Points
    3.567
    Level
    37
    Downloads
    0
    Uploads
    0

    Standard

    Does somebody know where to find a tutorial about sprites? I don't mean tutorials
    like change your sprite when he goes left or right, because I can do that already,
    but for example I have 4 sprites for the direction left, but how can you let the
    sprite change if the pad left is hold?

    Sorry for my english I'm from the Dutch:lol:
    [spoiler=My Releases So Far]
    PSP_Operator v2.0 Final (and earlier; stopped with this one)
    Mario's Road v2.0 (working on 2.5:D)
    UMD_Operator v0.0.2 (and earlier; stopped with this one)
    PSP-Quiz v0.1 (working on v0.2 already:D)
    [/spoiler]

  24. #9624
    Banned for LIFE
    Points: 18.744, Level: 86
    Level completed: 79%, Points required for next Level: 106
    Overall activity: 0%

    Registriert seit
    Oct 2006
    Ort
    East London, England
    Beiträge
    2
    Points
    18.744
    Level
    86
    Downloads
    0
    Uploads
    0

    Standard

    animlib or this

    Dode Kikker

    NEUS

  25. #9625
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von seanpaul223 Beitrag anzeigen
    yeah it does..the fact is that the function table.remove() doesn't works for tables as dictionnaries...


    any idea ?
    first of all, you might consider fixing this:
    Code:
    for i=1,#units do     if unit[i].health <= 0 then table.remove(units,i) end   end
    you're accessing the table called unit while looping through the table called units. now, assuming this isn't the problem but instead unchecked iterators are, i will continue

    yes, it does work like that, but the problem is this;

    lets say you have 5 units before the loop starts
    Code:
    for i = 1, #units do
        do stuff for unit 1
        do stuff for unit 2
        do stuff for unit 3, he is dead, take unit 3 out
        do stuff for unit 4, which is actually unit 5
        do stuff for unit 5, which doesn't exist
    end
    for example, run this code here at the online demo executer
    Code:
    unit = {}
    for i = 1, 5 do
        table.insert(unit, {x=0,y=0,dead=false})
    end
    unit[3].dead = true
    for i = 1, #unit do
        if unit[i].dead then table.remove(unit,i) end
        print("counter")
    end
    you'll see it prints counter 4 times and then errors on the fifth when there is no fifth entry

    now, if we were to use either a while loop or ipairs as shown in these two snippets:
    Code:
    unit = {}
    for i = 1, 5 do
        table.insert(unit, {x=0,y=0,dead=false})
    end
    unit[3].dead = true
    i = 1
    while i <= #unit do
        if unit[i].dead then table.remove(unit,i) end
        print("counter")
        i = i + 1
    end
    that simply outputs counter 4x and never errors
    and with ipairs:
    Code:
    unit = {}
    for i = 1, 5 do
        table.insert(unit, {x=0,y=0,dead=false})
    end
    unit[3].dead = true
    for i, thisunit in ipairs(unit) do
        if thisunit.dead then table.remove(unit,i) end
        print("counter")
    end
    it also works fine.

    a for loop only evaluates the upper and lower limits once at the beginning. a while loop checks the logic every time, and ipairs runs through each entry on its own to prevent going overboard. personally, I'd use ipairs, while loops require a separately managed iterator which is messy.

    TLDR: don't use for i = 1, #sometable do
    use for i, entry in ipairs(sometable) do

    edit: fixed a tiny typo, added a sentence
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]

  26. #9626
    QJ Gamer Green
    Points: 3.567, Level: 37
    Level completed: 45%, Points required for next Level: 83
    Overall activity: 0%

    Registriert seit
    May 2008
    Ort
    The Netherlands
    Beiträge
    330
    Points
    3.567
    Level
    37
    Downloads
    0
    Uploads
    0

    Standard

    Does somebody know the answer on my question above?
    [spoiler=My Releases So Far]
    PSP_Operator v2.0 Final (and earlier; stopped with this one)
    Mario's Road v2.0 (working on 2.5:D)
    UMD_Operator v0.0.2 (and earlier; stopped with this one)
    PSP-Quiz v0.1 (working on v0.2 already:D)
    [/spoiler]

  27. #9627
    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 eldiablov Beitrag anzeigen
    animlib or this

    Dode Kikker

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

  28. #9628
    QJ Gamer Green
    Points: 3.567, Level: 37
    Level completed: 45%, Points required for next Level: 83
    Overall activity: 0%

    Registriert seit
    May 2008
    Ort
    The Netherlands
    Beiträge
    330
    Points
    3.567
    Level
    37
    Downloads
    0
    Uploads
    0

    Standard

    Oh sorry didn't see that:lol:
    [spoiler=My Releases So Far]
    PSP_Operator v2.0 Final (and earlier; stopped with this one)
    Mario's Road v2.0 (working on 2.5:D)
    UMD_Operator v0.0.2 (and earlier; stopped with this one)
    PSP-Quiz v0.1 (working on v0.2 already:D)
    [/spoiler]

  29. #9629
    QJ Gamer Bronze
    Points: 5.583, Level: 48
    Level completed: 17%, Points required for next Level: 167
    Overall activity: 0%

    Registriert seit
    May 2007
    Beiträge
    127
    Points
    5.583
    Level
    48
    Downloads
    0
    Uploads
    0

    Standard

    rather than table.remove I'd just set the entry to nil and at once the ipairs has completed its passes, compact the table at the end, rather than do a lot of costly table.remove's.
    -- Code Monkey : Sarien, Fishguts, Cracks and Crevices --

    "Did IQ's just drop sharply while I was away?" (Ripley)

  30. #9630
    QJ Gamer Green
    Points: 3.721, Level: 38
    Level completed: 48%, Points required for next Level: 79
    Overall activity: 0%

    Registriert seit
    Jan 2008
    Beiträge
    612
    Points
    3.721
    Level
    38
    Downloads
    0
    Uploads
    0

    Standard

    If several enemies were killed all in the same frame quite frequently, I would agree, but going through and checking for stuff to compact EVERY SINGLE TIME and enemy dies would use probably more CPU over time than the table.remove. Besides, table.remove is much simpler and leaving holes in a table isn't exactly clean. What if we want to check collision between enemies? Do we then have to check if each of the enemies we decide to check is nil or not? Keeping the table empty of nil values cuts a lot of possible overlooked problems and will hardly use more CPU. table.remove is only called when an enemy etc dies, and this doesn't happen very much.
    [size=3][url=http://luaplayer.org/forums/index.php?topic=507]Complete Lua development cycle outline[/url][/size]


 

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:07 PM Uhr.

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