The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## 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; no i understand the workings of the browser I just was overlooking one small variable(Which I tend to do a ...

 Tweet
1. no i understand the workings of the browser I just was overlooking one small variable(Which I tend to do a lot too) but I figured it out and its all good now.

2. ## 2D & Isometric Tile Based Map

Hi, i guess this should not be rough to fix..
I'm trying to make a 2D Isometric map using tiles.

Here are a kinda map i'm trying to reproduce using tiles.

Now here is the code.Assume i'm using two different tiles, but they are both
40x20 pixels sized.

Code:
tile={}
for i=1,2 do tile[i]=Image.load("./"..i..".png") end

--This matrix represents the world
map={
{1,1,1,1},
{1,1,1,1},
{2,1,1,2},
{2,2,2,1},
{2,2,2,1},
{2,2,2,1}}

--[[this draws the map.I've considered that for odd index entries in the map table , tiles are blitted correctly.But for even index entries tiles are blitted shifted to the right of 40/2 pixels and shifted up of 20/2 pixels]]

function drawmap(matrix)
local X=tile[1]:width()
local Y=tile[1]:height()
for j=1,#matrix do
for i=1,#matrix[1] do
if (j%2)==1 then screen:blit((X)*(i),(j)*Y,tile[matrix[j][i]])
elseif (j%2)==0 then screen:blit(X*i+X/2,Y*j-(Y/2),tile[matrix[j][i]])
end
end
end
end

while true do
screen:clear()
drawmap(map)
screen.waitVblankStart()
screen.flip()
end
Here is is the result, very far from my goal..

3. in your second loop you need to change the #matrix[1] to #matrix[j] but that wont make a difference to how it displays, ill take a look now for you

4. Originally Posted by FaT3oYCG
in your second loop you need to change the #matrix[1] to #matrix[j] but that wont make a difference to how it displays, ill take a look now for you
that won't affect the code, just because basically all tables in the matrix table have the same number of indexes.

Alright, just have a look at it.Thanks in advance.

5. ok im looking now and i said the same as you but your code would be neater if it was a j but nvm it doesnt matter.

the tiles that you are using seem to be from a perspective i tried to duplicate them but it doesnt look right, could you post the two image files so i can use them with the code please, thanks

6. Any pics, with colored lozenges should fit nicely.But just take these ones.

7. this works like you want it to but i think it might need some tweaking

Code:
function drawmap(matrix)
local X = tile[1]:width()
local Y = tile[1]:height()

for i = 1, #matrix do
for j = 1, #matrix[i] do
if (i % 2) == 1 then
screen:blit((X * j), (Y * i) - (Y * math.ceil(i / 2)), tile[matrix[i][j]])
elseif (i % 2) == 0 then
screen:blit((X * j) + (X / 2), ((Y * i) - (Y / 2)) - (Y * math.ceil(i / 2)), tile[matrix[i][j]])
end
end
end
end

8. Yep it works.I can see my own error.Thanks mate.

9. i'm trying to make a script to allow me to cycle through some images, and it skips my fourth image. I think that it is doing the 4 image but as soon is it cycles to the fourth one it then goes back to the second or third as if you have pressed a button on the dpad. Is there any way to stop this?
Code:
if currentpic == 1 then
currentpic = 2
end
currentpic = 3
end
end

if currentpic == 2 then
currentpic = 1
end
currentpic = 4
end
end

if currentpic == 3 then
currentpic = 1
end
currentpic = 4
end
end

if currentpic == 4 then
currentpic = 2
end
currentpic = 3
end
end

currentpic = 4
end

10. I'd rather propose you to modify a bit your code.It would work nicer if you use tables and a lot of iteration to cycle through your images.

Assuming you have four images to cycle through using left & right keys, and they are named pic1.png, pic2.png, pic3.png and pic4.png

Code:
--this table will store pictures
image = {}

--using a for loop to load once all images
for i=1,4 do image[i]=Image.load("./pic"..i..".png") end

--this var will take values betwen 1 and 4, and will be used to display a specific image.
local currentImage=1

--here is the main function: x & y are where images should be bltted on the screen, and Pic is a table containing images
function cycle(x,y,Pic)
if key:left() and not oldkey:left() then
if currentImage-1 >= 1 then currentImage=currentImage-1
else currentImage=1
end
end
if key:right() and not oldkey:right() then
if currentImage+1 <= #Pic then currentImage=currentImage+1
else currentImage=#Pic
end
end
screen:blit(x,y,Pic[currentImage])
end

--the main loop
while true do
screen:clear()

--Using the previous functio to iterate over image table.
cycle(10,100,image)

oldkey=key
screen.waitVblankStart()
screen.flip()
end
I guess this fits for you.

11. Now back with another matter,

Now i'm using 9 tiles.The goal is the same, draw an isometric 2D map with tiles.
Tiles are 65 by 33 pixels.

Here is the current rendering:

Just nice.As you can see, when i try this on my Laptop, the framerate goes up to 200 fps...
The matter comes when i try it on a PSP.The scrolling is very very slow.Any idea ?

Code:
math.randomseed(os.time())
tile={}
for i=1,9 do tile[i]=Image.load("./G150000"..i..".png") end

---Creates Map
function createMap(X,Y)
for i=1,X,1 do
map[i]={}
for j=1,Y do
map[i][j]=math.random(1,9)
end
end
end

--Stores the map generated in a text file
function mapsave(mat)
file=io.open("map.txt","w")
file:write("map={\n")
for i=1,#mat do
file:write("{")
for j=1,#mat[i] do
if j ~= #mat[i] then file:write(mat[i][j]..",")
else file:write(mat[i][j].."},\n")
end
end

end
file:write("}")
file:close()
end

--This Thread Draws the map.It requires some global variables MapX & MapY which are Camera coordinates
--Note: all tiles are NOT blitted on the same time.Only those which should be on screen are rendered.
function draw()
local X = tile[1]:width()-2
local Y = tile[1]:height()-2

for i = 1, #map do
for j = 1, #map[i] do
if (i % 2) == 1 then
if mapX <= (X*j)+X and mapX+480 >= (X*j) and mapY <= (Y*i)+Y-(Y*math.ceil(i/2)) and mapY+272 >= (Y*i)-(Y*math.ceil(i/2)) then screen:blit(-mapX+(X * j), -mapY+(Y * i) - (Y * math.ceil(i / 2)), tile[map[i][j]]) end
elseif (i % 2) == 0 then
if mapX <= (X*j)+X+(X/2) and mapX+480 >= (X*j)+(X/2) and mapY <= ((Y * i)+Y - (Y / 2)) - (Y * math.ceil(i / 2)) and mapY+272 >= ((Y * i) - (Y / 2)) - (Y * math.ceil(i / 2)) then screen:blit(-mapX+(X * j) + (X / 2), -mapY+((Y * i) - (Y / 2)) - (Y * math.ceil(i / 2)), tile[map[i][j]]) end
end
end
end
end

--This function will always create the Drawing thread whenever it dies.
drawmap=coroutine.create(draw)
function checkstate(co)
if type(co) == "thread"  then
if coroutine.status(co) == "dead" or coroutine.status(co)== "suspended" then
drawmap = coroutine.create(draw)
coroutine.resume(drawmap)
end
end
end

map={}
createMap(100,50)
mapsave(map)
mapX,mapY=100,50
local fps=Timer.new()

while true do
screen:clear()
fps:reset()
fps:start()
local scroll=10
if pad:up() then mapY = mapY-scroll end
if pad:down() then mapY = mapY+scroll end
if pad:left() then mapX = mapX-scroll end
if pad:right() then mapX=mapX+scroll end

checkstate(drawmap)

screen:print(10,260, "FPS = "..math.floor(1000/fps:time()), Color.new(255,255,255))
screen.waitVblankStart()
screen.flip()
end
I've attached a zip file containing the code & pics.Thanks for trying it.

12. it didnt really work, part of that was my fault i had my pics like this:
....1
.2.....3
....4
all i had to do was rename them to this
....1
.4.....2
....3
and it worked perfectly thank you very much

13. Originally Posted by seanpaul223
<snip>

I've attached a zip file containing the code & pics.Thanks for trying it.
ill take a look for you, maybe you can search into programming techniques, it helps when you are coding because looking at your code im sure i can improve it quite a lot quite quickly by doing something simple, when i post it up you can see and understand.

14. Originally Posted by flggt
ill look at it for ya, but maybe you should learn how to code first cuz sometimes it helps ALOT if u no how to code
directed to anyone but flggt

Who is this guy?

15. Originally Posted by seanpaul223
<snip>

I've attached a zip file containing the code & pics.Thanks for trying it.
ok sorry i didnt reply, i did actually fix this but i cant find where i put it on my hard drive, all i did was clean up the code a little and make the display of the tiles a function, then the displaying of the tiles only needs to be called when scroll changes / a button is pressed that would effect it.

I hope you can replicate what i did because i just cant find where i put it as i was using lua player for windows to test it and then just ran it with usbhost on the psp.

anyway, oh and on a side note your FPS function is not FPS it is FPL (Frames Per Loop), if you want an FPS function you would need a timer that calculates the time that it took since the timer was last reset in the space of a second

16. hey i'm having a problem with the Font.load() function in LPHMv2.
i tried loading all of my fonts but it justs freezes when i start the player.
for ex.

but it doesn't work

i was wondering if i had to input the number as kb or bytes?

because i've tried both, and it still freezes.

17. I think you have read the doc if you didn't and if it is included.The way you have to use this func should be explained there.

18. isnt the number the size? try putting 10 and see what happens, trying to lad the font at size 49 probably would mke it freeze as it would use all of the memory.

19. You really don't need the number, leaving it out will put it at the default, which I think is 10.

20. fonts are always powers of 2.....
49 != power of 2
40 == and 48 == power of 2

21. Can someone tell me how to start tables in PGE?

22. tables = lua, pge =api.

table = {}

23. i am having troubles with a point and click game, i have it set up if click in a certian spot the frame you are viewing changes and then there are different things to click, but the click from the frame before effects my new frame. this is hard to explain so i have a gif video to show what i mean,
its going kinda fast (first time using the recording prx), the black bar is the begining. I have a flashlight in one frame and when you click on it it should dissapaire. you can see (i hope) from the image that when I click on the box the first time the flashlight was there, but when went back and did it a second time right where the flashlight would be in the next frame it is gone. Is there any way to make my clicks (with X) not effect my next frame?
here is part of my code
Code:
-- First Frame
if currentpic == 1 then
-- Box
if cursor.x >= 201 and cursor.x <= 288 and cursor.y >= 245 and cursor.y <= 272 then
currentpic = 7
end
end
end-- First Frame
-- Box Top
if currentpic == 7 then
if cursor.x >= 116 and cursor.x <= 480 and cursor.y >= 112 and cursor.y <= 235 then
currentpic = 15
end
end
end -- Box Top
-- Flash Light
if currentpic == 15 and f_light == 0 then
if cursor.x >= 219 and cursor.x <= 281 and cursor.y >= 162 and cursor.y <= 249 then
f_light = 1
end
end
end -- Flash Light
i have used the pad and oldpad tecnique but that doesnt effect it. is there anyway to stop this from happening?

24. Set up a delay between presses. Easiest way is to have a int increase by 1 every frame.
If number > int then
number = int
end

then on button presses add, & number == int then. Obviously you should experiment with 'int' to find a delay that suites your game.

25. using a timer or delay function woud be better though

26. Not having such a crappy structure would be better - don't freaking hardcode everything, you'll never get anywhere.

Fix it by only processing one per loop, Jesus.

27. i couldnt make the counter or timer work, but i played around with the order and it works now:o. Niekie what do you mean by hardcoding everything?

28. Most probably you could make a function that accepts the picture and some x, y, width, height values for the place to click and simple call that when cross is pressed instead of coding the same thing 7 times.

I actually havent looked at your code i was just inputting that using a timer is better than incrememnting a counter for time reasons as a counter is dependant on how fast your loop is where a timer is not.

29. actually i didnt really just code it seven times, i have done that exact method of coding for my whole port of a game and i am nearly finished. Thank you for your input though, hopefuly the next time I do a game i can remeber to do that.

30. Originally Posted by dan369
tables = lua, pge =api.

table = {}
I tried what ever you saidbut the game still freezes...
can you turn this in a table used in PGE
Code:
local player1 =
{
{x=10, y=10, health=500, lvl=1 , dmg=50, exp=100 , image="images/Player/playerright.png",weapon="sword", magic="SunFire", mana = 100, casting = 0, attacking = 0}
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 10:00 AM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY