Instead of asking, why don't you have a look at the Lua Manual? Notably the chapters on visibility, variables, and functions.
Printable View
Instead of asking, why don't you have a look at the Lua Manual? Notably the chapters on visibility, variables, and functions.
functions automatically make variables local. take out the "local " and it will work as expected.
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
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.
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
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
Hey, how do I use the Mp3me.info() Fuction in LPHMv2. Thanks for your help. :D
I would recommend asking on HM's forums if his nonexistent documentation didn't explain it to you.
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.
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
actually it goes
4
5
15
16
6
print doesn't add 1 to x like blah does.
Oh, right right right.
I didn't notice "print", I feel silly :-(
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?
@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...
Best advice would be to switch away from HM if the developer is too lazy to provide docs.
any way to access (i.e read&write) to the idstorage from lua?
~!SlasheR~!
No
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
Whats the error?
Does #units actually return the number of units ?
yeah it does..the fact is that the function table.remove() doesn't works for tables as dictionnaries...
any idea ?
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:
animlib or this
Dode Kikker
NEUS
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
Does somebody know the answer on my question above?
Oh sorry didn't see that:lol:
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.
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.
Nice TurtlesPwn,
i've did it just like this:
I guess i did it according to what u said before.Anyway, this works fine.Code:--table for units
unit = {}
unit[1] = {x=10,y=10,health=100, exist = true}
unit[2] = {x=10,y=10,health=100, exist = true}
unit[3] = {x=10,y=10,health=100, exist = true}
--counter
c = 1
--main loop
while true do
---
for i=1,#unit do
----
if unit[i].health<=0 then unit[i].exist = false end --this makes the unit disappear from the screen when dead
end
--this cleans the sub-table containing the dead unit parameters.
while c<=#unit do
if not unit[c].exist then table.remove(unit,c) end
c=c+1
end
if c>=#unit then c=1 end
no, that's not what I said, thats one of the worse ways to do it.
replace
withCode:for i=1,#unit do
----
if unit[i].health<=0 then unit[i].exist = false end --this makes the unit disappear from the screen when dead
end
--this cleans the sub-table containing the dead unit parameters.
while c<=#unit do
if not unit[c].exist then table.remove(unit,c) end
c=c+1
end
if c>=#unit then c=1 end
Code:for i, thisunit in ipairs(unit) do
if thisunit.health <= 0 then
table.remove(unit,i)
end
end
tables in lua don't start at 0?
no, they start at 1. they're weird....but to me starting at 1 is the more logical way to do it.
well tables dont naturally index at 0 but you can use 0 as a key foo[0] = "x" will work just fine, as long as you understand the difference between using a table like an array and using it like a dictionary.
So i need a bit of help with table.insert().
I have to variables, mapX, mapY. These are entered by the player. Then i need to insert the starting elements which are(will be) indexed to 0. So i was wondering about how i'd insert that info into a array. MapX is the length, MapY is the height of the array.
table.insert(atable,athin gtoinsert,ifyouwantthepos itionatwhichtoinsertit)
atable[index] = athingtoinsert
I'm still stuck :(. So i have the table say called 'map'. Then i have this,
Would that work? (btw thats off the top of my head)Code:for i = 1, MapX do
for j = 1, MapY do
table.insert(map, 0);
end
end
Just test it ! You ask way too many questions. I worked out how to use table.insert within two minutes of testing and a bit of printing to the screen. Better yet, look at the DOCUMENTATION.