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, ...
-
12-19-2008, 01:52 AM #9601QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
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)
-
12-19-2008, 04:52 AM #9602
-
12-19-2008, 05:02 AM #9603QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
-
12-19-2008, 08:41 AM #9604QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
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]
-
12-19-2008, 08:51 AM #9605Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
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
so that x can be modified to a new value but not affect the origional value outside of the functionCode:x = 15 function blah(x) local x = x x = x + 1 screen:print(0, 0, x) end------ 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).
-
12-19-2008, 08:53 AM #9606QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
no, no you do not need to. local variables take precedence over globals when you are inside their scope.
run this code:
here: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)
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]
-
12-20-2008, 09:47 AM #9607QJ Gamer Green
- Registriert seit
- May 2008
- Ort
- The Netherlands
- Beiträge
- 330
- Points
- 3.567
- Level
- 37
- Downloads
- 0
- Uploads
- 0
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
-
12-20-2008, 01:10 PM #9608QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
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]
-
12-20-2008, 04:26 PM #9609
Hey, how do I use the Mp3me.info() Fuction in LPHMv2. Thanks for your help.
-
12-20-2008, 06:31 PM #9610QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
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]
-
12-20-2008, 11:22 PM #9611QJ Gamer Bronze
- Registriert seit
- Aug 2007
- Ort
- Everywhere
- Beiträge
- 206
- Points
- 5.826
- Level
- 49
- Downloads
- 0
- Uploads
- 0
Thanks man, this is basically what I was looking for.
I know this sounds a bit far fetched, but sometimes it's more convenient not to.
So...
Is that right?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
Sorry I didn't respond earlier. My internet was down, and kinda still is.
-
12-21-2008, 02:58 AM #9612QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
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
-
12-21-2008, 07:30 AM #9613QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
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]
-
12-21-2008, 08:23 AM #9614QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
Oh, right right right.
I didn't notice "print", I feel silly :-(
-
12-21-2008, 12:38 PM #9615QJ Gamer Bronze
- Registriert seit
- Aug 2007
- Ort
- Everywhere
- Beiträge
- 206
- Points
- 5.826
- Level
- 49
- Downloads
- 0
- Uploads
- 0
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)
-
12-21-2008, 01:27 PM #9616
@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...
-
12-21-2008, 01:31 PM #9617Banned for LIFE
- Registriert seit
- Oct 2006
- Ort
- East London, England
- Beiträge
- 2
- Points
- 18.744
- Level
- 86
- Downloads
- 0
- Uploads
- 0
Best advice would be to switch away from HM if the developer is too lazy to provide docs.
-
12-30-2008, 07:11 PM #9618xMod.
- Registriert seit
- Oct 2008
- Ort
- Melbourne, Australia
- Beiträge
- 675
- Points
- 4.576
- Level
- 43
- My Mood
-
- Downloads
- 0
- Uploads
- 0
any way to access (i.e read&write) to the idstorage from lua?
~!SlasheR~!
-
12-30-2008, 10:22 PM #9619QJ Gamer Bronze

- Registriert seit
- Jul 2006
- Beiträge
- 550
- Points
- 5.381
- Level
- 47
- Downloads
- 1
- Uploads
- 0
No
-
01-03-2009, 04:26 AM #9620QJ Gamer Bronze

- Registriert seit
- Dec 2007
- Ort
- B.F.
- Beiträge
- 328
- Points
- 7.257
- Level
- 56
- My Mood
-
- Downloads
- 1
- Uploads
- 0
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
so i've defined two units, with their own x,y position on a map, and their own health.Code:units = {} unit[1] = {x = 10, y=10, health = 100} unit[2] = {x = 20, y=30,health = 100}
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:
how can i fix that ?Code:for i=1,#units do if unit[i].health <= 0 then table.remove(units,i) end end
-
01-03-2009, 04:57 AM #9621Banned for LIFE
- Registriert seit
- Oct 2006
- Ort
- East London, England
- Beiträge
- 2
- Points
- 18.744
- Level
- 86
- Downloads
- 0
- Uploads
- 0
Whats the error?
Does #units actually return the number of units ?
-
01-03-2009, 05:19 AM #9622QJ Gamer Bronze

- Registriert seit
- Dec 2007
- Ort
- B.F.
- Beiträge
- 328
- Points
- 7.257
- Level
- 56
- My Mood
-
- Downloads
- 1
- Uploads
- 0
yeah it does..the fact is that the function table.remove() doesn't works for tables as dictionnaries...
any idea ?
-
01-03-2009, 06:21 AM #9623QJ Gamer Green
- Registriert seit
- May 2008
- Ort
- The Netherlands
- Beiträge
- 330
- Points
- 3.567
- Level
- 37
- Downloads
- 0
- Uploads
- 0
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]
-
01-03-2009, 07:01 AM #9624Banned for LIFE
- Registriert seit
- Oct 2006
- Ort
- East London, England
- Beiträge
- 2
- Points
- 18.744
- Level
- 86
- Downloads
- 0
- Uploads
- 0
animlib or this
Dode Kikker
NEUS
-
01-03-2009, 08:42 AM #9625QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
first of all, you might consider fixing this:
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 continueCode:for i=1,#units do if unit[i].health <= 0 then table.remove(units,i) end end
yes, it does work like that, but the problem is this;
lets say you have 5 units before the loop starts
for example, run this code here at the online demo executerCode: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
you'll see it prints counter 4 times and then errors on the fifth when there is no fifth entryCode: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
now, if we were to use either a while loop or ipairs as shown in these two snippets:
that simply outputs counter 4x and never errorsCode: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
and with ipairs:
it also works fine.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
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]
-
01-03-2009, 12:57 PM #9626QJ Gamer Green
- Registriert seit
- May 2008
- Ort
- The Netherlands
- Beiträge
- 330
- Points
- 3.567
- Level
- 37
- Downloads
- 0
- Uploads
- 0
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]
-
01-03-2009, 12:59 PM #9627Developer and Tutor.
- Registriert seit
- Jul 2007
- Ort
- Widnes, England
- Beiträge
- 1.649
- Points
- 8.736
- Level
- 62
- My Mood
-
- Downloads
- 0
- Uploads
- 0
------ 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).
-
01-03-2009, 02:16 PM #9628QJ Gamer Green
- Registriert seit
- May 2008
- Ort
- The Netherlands
- Beiträge
- 330
- Points
- 3.567
- Level
- 37
- Downloads
- 0
- Uploads
- 0
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]
-
01-03-2009, 05:13 PM #9629
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)
-
01-03-2009, 07:47 PM #9630QJ Gamer Green
- Registriert seit
- Jan 2008
- Beiträge
- 612
- Points
- 3.721
- Level
- 38
- Downloads
- 0
- Uploads
- 0
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]


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