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; if y &gt; 470 then x = x + 10 end...

 Tweet
1. if y > 470 then x = x + 10 end

2. Im still having the problem. I just can not get the text that is going off screen to re route into a new line below the prior line. This time here is my actual code. Does anyone see what I'm missing ??? Its been driving me insane.

Also I am on LPHMV2 if that matters any.(even though i know it doesnt)

y = 10
x = 10
y = y + 10
x = x + 10
screen.startDraw()
screen.clear(0)
Image.blit(0,0,back)
screen.print(x,y,moon,.6, red,0)
if x > 470 then y = y + 10
screen.endDraw()
screen.flipscreen()
file:close()

Ive tried all kinds of different ways of coding this with different variables and such and just cannot get it to work.

3. Code:
```red = Color.new(255,0,0,255)
y = 10
x = x + 10
y = y + 10
while true do
screen.startDraw()
screen.clear(0)
Image.blit(0,0,back)
screen.print(x,y,moon,0.6,red,0)
if x > 470 then y = y + 10  end
screen.endDraw()
screen.flipscreen()
screen.waitVblankStart()
end```
try that

4. Originally Posted by mootjeuh
Code:
```red = Color.new(255,0,0,255)
y = 10
x = x + 10
y = y + 10
while true do
screen.startDraw()
screen.clear(0)
Image.blit(0,0,back)
screen.print(x,y,moon,0.6,red,0)
if x > 470 then y = y + 10  end
screen.endDraw()
screen.flipscreen()
screen.waitVblankStart()
end```
try that
Code:
```red = Color.new(255,0,0,255)

y = 10

while true do
screen.startDraw()
screen.clear(0)
y = y + 10

Image.blit(0,0,back)
screen.print(x,y,moon,0.6,red,0)

if y >= 480 then
x = x + 10
y = 0;
end

screen.endDraw()
screen.flipscreen()
screen.waitVblankStart()
end```
Should work

5. ill give it a try and let u know how it goes
-=Double Post Merge =-
still no dice. this is driving me crazy. guess ill keep trying. if anyone has any other ideas please let me know.

6. This is my problem. see how the text goes off the screen? i wanna relocate that text to a new line under the first line. Heres my code that isnt working.

7. I can seriously name about 5-6 things wrong there.

-Shouldn't be reading every loop, put it outside of the main loop.
-You reset the x pos to 10 each loop, again outside the main loop.
-Same for Y.
-No 'end' to end the if statement.
-'red' seems to be un declared (in other words, not there) though i'm guessing it is in the code

8. ya thats not the whole code or wats in my program. just trying 2 show what im trying 2 do and the part im having issues with. but thanx ill give it a try. the top shot is a screenshot from my program. the code im working on is for obviously a text reader. we' ll see if i can re-route the text.

9. Originally Posted by thecraziness1
This is my problem. see how the text goes off the screen? i wanna relocate that text to a new line under the first line. Heres my code that isnt working.
When you read a line in the text file, you have to find out how long that line is pixels. If it exceeds the width of the string, you have to split that line into two at the end of a word that nearest to the edge of the screen but not yet off screen.

Rinse and repeat.

10. thats what i ended up doing but now i have a new problem. when i try 2 run "dofile" out of my filebrowser the program freezes up. its finding the file it just freezes up and i dont know why.
-=Double Post Merge =-
also "break end" ends the browser and takes me back 2 the menu but then it gives "".lua doesnt exist when trying 2 run other functions.

11. the problem can be caused by many things. maybe you should post your code

12. the part thats problematic is ..... system.message("chosen file loaded",0)
end
and
it finds the files and then just freezes .
if i do:
if
pad:start() then break end

it goes back to the menu but wont run any of the options.
gives cannot find(pages/file.lua)
had this prob w/ another program 2

13. if you putted ''system.message'' then it's an obvious error, it should be
Code:
`System.message`

14. no thats just an example . I do know how to program and know all the basics plus some. The browser runs fine and the message does come up. Im not getting any errors. Just when i do the command to "dofile" the screen goes black just like when switching to any other lua file but then freezes instead of doing what the file commands it to do. I have another program with a different file browser that does the same thing. Cant run "dofile" out of it. P.s.the other file im trying 2 go to is programmed right.
-=Double Post Merge =-
Im just trying to redirect the program back 2 the main menu when you exit the browser

15. im trying to make a maze game, i what it to switch from one maze to the other when you finish it, but i get a an error of : ERROR: (null) Press start to restart, i'm on LPE

Spoiler for Main:
Code:
```--	Scrolling Map - PreIsometric
--	6/1/2009-X/YY/2009
--	Modifyed by Cmbeke
--	Taken From :
--						Origninaly By Osgeld

--	Made Using PSPWrite on PSP & SciEdit on PC

--		Runs On LuaPlayer Euphoria By Zach

dofile("system\\system\\colors.lua")
dofile("system\\system\\functions.lua")

-- Checks if Finished
finished = 0
-- What Maze File
dofile_maze = 0

-- Move Map X
mmx = 192
-- Move Map Y
mmy = 88

--FramesPerSecond Instructions
fps = Timer.new()

-- Wall
wallS = 32
wall = Image.createEmpty(wallS, wallS)
wall:clear(grey)

-- Finish
finishS = 32
finish = Image.createEmpty(finishS,finishS)
finish:clear(blue3)

-- Collision Player
Player = {
X = 2,
Y = 2,
}

--Pastes Player
playerS = 24
see = Image.createEmpty(playerS, playerS)
see:clear(red)
screen:blit(228,124,see)

--Randomizes Finish From Four Prechoosen Spots
wahh = 0
wahh = math.randomseed(os.time())
wahh = math.random(3,6)
print(wahh)

while dofile_maze == 0 do

--Count Frames
fps:reset()
fps:start()

--Starts GU
System.startGu()
--Refreshes Screen
screen:clear()
select_file()
maze_draw()
player_paste()
player_move()
player_win()
--Ends GU
System.endGu()
--Other Needed Crap
screen.flip()
end```

Spoiler for Functions:

Code:
```-- Visible Player
function player_paste()
playerS = 24
see = Image.createEmpty(playerS, playerS)
see:clear(red)
screen:blit(228,124,see)
end

--Load Moze File, 0=Ground 1=Wall 3,4,5 or 6=Finish according to wahh
function select_file()
if dofile_maze == 0 then
dofile("System\\Mazes\\maze.lua")
end
if dofile_maze == 1 then
dofile("System\\Mazes\\maze2.lua")
end
end

-- Draws The Map
function maze_draw()
for Y = 1, #Map do
for X = 1, #Map[Y] do
--      Shows Where Map is                Moves Map
local x = (X - 1) * wallS + mmx
local y = (Y - 1) * wallS + mmy

if Map[Y][X] == 1 then
screen:blit(x, y, wall)
end
if Map[Y][X] == wahh then
screen:blit(x, y, finish)
end
end
end
-- Paste FPS to screen
screen:print( 415, 10, "FPS = "..math.floor(1000/fps:time()), white)
end

-- Moves The Map
function player_move()
if Map[Player.Y - 1][Player.X] ~= 1 then -- Checks If You Can Move(Makes Sure Not Equal To Wall, Wall =1)
-- Moves Map and Moves Invisible Collision Player
mmy = mmy + 32
Player.Y = Player.Y - 1
end
end
if Map[Player.Y + 1][Player.X] ~= 1 then -- Checks If You Can Move
-- Moves Map and Moves Invisible Collision Player
mmy = mmy - 32
Player.Y = Player.Y + 1
end
end
if Map[Player.Y][Player.X - 1] ~= 1 then -- Checks If You Can Move
-- Moves Map and Moves Invisible Collision Player
mmx = mmx + 32
Player.X = Player.X - 1
end
end
if Map[Player.Y][Player.X + 1] ~= 1 then -- Checks If You Can Move
-- Moves Map and Moves Invisible Collision Player
mmx = mmx -32
Player.X = Player.X + 1
end
end
if Map[Player.Y][Player.X] == wahh then  -- Warps To Next Level (wahh is randmonly seclected from 3,4,5,6 as a finish point)
--do something
finished = 1
end

end

function player_win()
if finished == 1 then
screen:print(5,5,"You Finish The Maze, Its Not Done Yet",blue4)
dofile_maze = 1
end
end

function player_won_temp()
screen:print(5,5,"You Finish The Maze, Its Not Done Yet",blue4)
end```

16. Which line does it say that the error is on?

17. it doesnt, i copied exactly what it said on my psp

18. Originally Posted by cmbeke
it doesnt, i copied exactly what it said on my psp
I don't think its an error. Its more so that when you've finished the loop breaks so LPE has nothing to control.
You print your text then immediately set dofile_maze to 1 so the loop breaks causing an error in LPE. Go to retroemu.com & report it as a bug.
Note: You wouldn't of seen the text print because it gets processed fast.

19. the text was only there to test if a hittest on one of my randomly generated points; thanks didnt think anything was wrong will report it

20. I've never used LPE, but I'm sure that it's not "\\" that you're supposed to use in a dofile.
it's supposed to be "/".
and tell me how exactly do you want to blit an image that you didn't even load ?

21. it is the \\ and i have no idea about what you mean by bliting an image that i didn't load, wait ill link to my maze thing so you can try it

Maze Madness(i know the graphics suck)

22. Hello, I want to create an online order for my game, but unfortunately I know nothing about wireless in the PSP Lua. If someone could help me and give me some basis for creating an online mode please. I am using LuaPlayerHm 7.

The game is Battle Fantasy, take a look -> [Preview] Battle Fantasy - Timing X

23. Hi all,
Need help for Implementing A-Star Pathfinding Code in a TS.

Well, i've used Altair's Pathfinding Algorithm.Its works well, so i'm pretty sure the problem is not int its code.Anyway, here you are:

No need to add more information, there are coments so it is easier to understand.

Code:
```--[[
A* algorithm for LUA
Ported to LUA by Altair
21 septembre 2006
--]]

function CalcMoves(mapmat, px, py, tx, ty)	-- Based on some code of LMelior but made it work and improved way beyond his code, still thx LMelior!
--[[ PRE:
mapmat is a 2d array
px is the player's current x
py is the player's current y
tx is the target x
ty is the target y

Note: all the x and y are the x and y to be used in the table.
By this I mean, if the table is 3 by 2, the x can be 1,2,3 and the y can be 1 or 2.
--]]

--[[ POST:
closedlist is a list with the checked nodes.
It will return nil if all the available nodes have been checked but the target hasn't been found.
--]]

-- variables
local openlist={}                 				-- Initialize table to store possible moves
local closedlist={}						-- Initialize table to store checked gridsquares
local listk=1                   				-- List counter
local closedk=0                					-- Closedlist counter
local tempH=math.abs(px-tx)+math.abs(py-ty)
local tempG=0
openlist[1]={x=px, y=py, g=0, h=tempH, f=0+tempH ,par=1}   	-- Make starting point in list
local xsize=table.getn(mapmat[1]) 				-- horizontal map size
local ysize=table.getn(mapmat)					-- vertical map size
local curbase={}						-- Current square from which to check possible moves
local basis=1							-- Index of current base

-- Growing loop
while listk>0 do

-- Get the lowest f of the openlist
local lowestF=openlist[listk].f
basis=listk
for k=listk,1,-1 do
if openlist[k].f<lowestF then
lowestF=openlist[k].f
basis=k
end
end

closedk=closedk+1
table.insert(closedlist,closedk,openlist[basis])

curbase=closedlist[closedk]				 -- define current base from which to grow list

local rightOK=true
local leftOK=true           				 -- Booleans defining if they're OK to add
local downOK=true             				 -- (must be reset for each while loop)
local upOK=true

-- Look through closedlist
if closedk>0 then
for k=1,closedk do
if closedlist[k].x==curbase.x+1 and closedlist[k].y==curbase.y then
rightOK=false
end
if closedlist[k].x==curbase.x-1 and closedlist[k].y==curbase.y then
leftOK=false
end
if closedlist[k].x==curbase.x and closedlist[k].y==curbase.y+1 then
downOK=false
end
if closedlist[k].x==curbase.x and closedlist[k].y==curbase.y-1 then
upOK=false
end
end
end

-- Check if next points are on the map and within moving distance
if curbase.x+1>xsize then
rightOK=false
end
if curbase.x-1<1 then
leftOK=false
end
if curbase.y+1>ysize then
downOK=false
end
if curbase.y-1<1 then
upOK=false
end

-- If it IS on the map, check map for obstacles
--(Lua returns an error if you try to access a table position that doesn't exist, so you can't combine it with above)
if curbase.x+1<=xsize and mapmat[curbase.y][curbase.x+1]~=0 then
rightOK=false
end
if curbase.x-1>=1 and mapmat[curbase.y][curbase.x-1]~=0 then
leftOK=false
end
if curbase.y+1<=ysize and mapmat[curbase.y+1][curbase.x]~=0 then
downOK=false
end
if curbase.y-1>=1 and mapmat[curbase.y-1][curbase.x]~=0 then
upOK=false
end

-- check if the move from the current base is shorter then from the former parrent
tempG=curbase.g+1
for k=1,listk do
if rightOK and openlist[k].x==curbase.x+1 and openlist[k].y==curbase.y and openlist[k].g>tempG then
tempH=math.abs((curbase.x+1)-tx)+math.abs(curbase.y-ty)
table.insert(openlist,k,{x=curbase.x+1, y=curbase.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
rightOK=false
end

if leftOK and openlist[k].x==curbase.x-1 and openlist[k].y==curbase.y and openlist[k].g>tempG then
tempH=math.abs((curbase.x-1)-tx)+math.abs(curbase.y-ty)
table.insert(openlist,k,{x=curbase.x-1, y=curbase.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
leftOK=false
end

if downOK and openlist[k].x==curbase.x and openlist[k].y==curbase.y+1 and openlist[k].g>tempG then
tempH=math.abs((curbase.x)-tx)+math.abs(curbase.y+1-ty)
table.insert(openlist,k,{x=curbase.x, y=curbase.y+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
downOK=false
end

if upOK and openlist[k].x==curbase.x and openlist[k].y==curbase.y-1 and openlist[k].g>tempG then
tempH=math.abs((curbase.x)-tx)+math.abs(curbase.y-1-ty)
table.insert(openlist,k,{x=curbase.x, y=curbase.y-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
upOK=false
end
end

-- Add points to openlist
-- Add point to the right of current base point
if rightOK then
listk=listk+1
tempH=math.abs((curbase.x+1)-tx)+math.abs(curbase.y-ty)
table.insert(openlist,listk,{x=curbase.x+1, y=curbase.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

-- Add point to the left of current base point
if leftOK then
listk=listk+1
tempH=math.abs((curbase.x-1)-tx)+math.abs(curbase.y-ty)
table.insert(openlist,listk,{x=curbase.x-1, y=curbase.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

-- Add point on the top of current base point
if downOK then
listk=listk+1
tempH=math.abs(curbase.x-tx)+math.abs((curbase.y+1)-ty)
table.insert(openlist,listk,{x=curbase.x, y=curbase.y+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

-- Add point on the bottom of current base point
if upOK then
listk=listk+1
tempH=math.abs(curbase.x-tx)+math.abs((curbase.y-1)-ty)
table.insert(openlist,listk,{x=curbase.x, y=curbase.y-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

table.remove(openlist,basis)
listk=listk-1

if closedlist[closedk].x==tx and closedlist[closedk].y==ty then
return closedlist
end
end

return nil
end

function CalcPath(closedlist)
--[[ PRE:
closedlist is a list with the checked nodes.
OR nil if all the available nodes have been checked but the target hasn't been found.
--]]

--[[ POST:
path is a list with all the x and y coords of the nodes of the path to the target.
OR nil if closedlist==nil
--]]

if closedlist==nil then
return nil
end
local path={}
local pathIndex={}
local last=table.getn(closedlist)
table.insert(pathIndex,1,last)

local i=1
while pathIndex[i]>1 do
i=i+1
table.insert(pathIndex,i,closedlist[pathIndex[i-1]].par)
end

for n=table.getn(pathIndex),1,-1 do
table.insert(path,{x=closedlist[pathIndex[n]].x, y=closedlist[pathIndex[n]].y})
end

closedlist=nil
return path
end```
No need to add more information, there are coments so it is easier to understand.

I've stored this whole algorithm in aLua File name "Pathfinding.lua".

Well, Now, for my RTS game, first i load the previous Algorithm.Then i create many variables for before the main loop.

Code:
```dofile("pathfinding.lua")

map_back=Image.load("img/map.png") --a tile for the background
houseimg=Image.load("img/house.png") --a house picture
selector=Image.load("img/selector2.png") -- the selector picture
img={} --[[a table of 12 items, each item containing the same unit picture but facing many directions]]
for i=1,12 do
end

--Here is the world Map.This world is divide into Squares.each square is 50pixels-width and 50 pixels-height.By default, each square is set 0,meaning walkable, so that units can cross'em.

map=	{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
}

--as you see, the world map size is larger than the PSP's screen.So i define these offsets for the scrolling.
mapX,mapY=0,0

--Now I create Tables for some houses.Each table contains the house's X and Y coordinates, and these coordinates are created within the World Map.
math.randomseed(os.time())
house={}
for i=1,60 do
house[i]={y=50*math.random(1,table.getn(map)), x=50*math.random(1,table.getn(map[1]))}
end

--Now before the main loop, this For-Loop writes into the World map grid.If any of the previous house is on a square, this quare is then set 1, so unwalkable.
for i=1,table.getn(house) do
v=(house[i].y/50)
u=(house[i].x/50)
if v+1> table.getn(map) then v=table.getn(map)-1 end
if u+1>table.getn(map[1]) then u=table.getn(map[1])-1 end
map[v+1][u+1]=1
end

--define variables
x=100 --cursor's X coordinate
y=100 --cursor's Y coordinate
player={x=0, y=0, xmove=0, ymove=0, speed=5, path={}, cur=1, pathLength=0, move=false,timer=0,direction="right"}  --A Unit's Table
orderMove=false --used for starting the pathfinding algorithm
Player=img[1] --The current player's picture.
xInterval=50
yInterval=50

--debug mode
debug=false

Right then, here is the main loop code.

Code:
```while true do
screen:clear()
--This is the map browsing code, and it works fine.I'm pretty sure the problem is not here.
if 480 >= mapX and 0 <= mapX+480 and 272 >= mapY and 0 <= mapY+272 then screen:blit(0-mapX,0-mapY,map_back) end --1
if 480*2 >= mapX and 480 <= mapX+480 and 272 >= mapY and 0 <= mapY+272 then screen:blit(480-mapX,0-mapY,map_back) end --2
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272 >= mapY and 0 <= mapY+272 then screen:blit(480*2-mapX,0-mapY,map_back) end --3
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272 >= mapY and 0 <= mapY+272 then screen:blit(480*3-mapX,0-mapY,map_back) end --4
if 480>= mapX and 0 <= mapX+480 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(0-mapX,272-mapY,map_back) end--5
if 480*2 >= mapX and 480 <= mapX+480 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(480-mapX,272-mapY,map_back) end--6
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(480*2-mapX,272-mapY,map_back) end --7
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(480*3-mapX,272-mapY,map_back) end --8
if 480 >= mapX and 0 <= mapX+480 and 272*3 >= mapY and 272*2 <= mapY+272*2 then screen:blit(0-mapX,272*2-mapY,map_back) end --9
if 480*2 >= mapX and 480 <= mapX+480 and 272*3 >= mapY and 272*2<= mapY+272*2 then screen:blit(480-mapX,272*2-mapY,map_back) end --10
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*3 >= mapY and 272*2 <= mapY+272*2 then screen:blit(480*2-mapX,272*2-mapY,map_back) end --11
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*3 >= mapY and 272*2 <= mapY+272*2 then screen:blit(480*3-mapX,272*2-mapY,map_back) end --12
if 480 >= mapX and 0 <= mapX+480 and 272*4 >= mapY and 272*3 <= mapY+272*3 then screen:blit(0-mapX,272*3-mapY,map_back) end --13
if 480*2 >= mapX and 480 <= mapX+480 and 272*4 >= mapY and 272*3<= mapY+272*3 then screen:blit(480-mapX,272*3-mapY,map_back) end --14
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*4 >= mapY and 272*3 <= mapY+272*3 then screen:blit(480*2-mapX,272*3-mapY,map_back) end --15
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*4 >= mapY and 272*3 <= mapY+272*3 then screen:blit(480*3-mapX,272*3-mapY,map_back) end --16
if 480 >= mapX and 0 <= mapX+480 and 272*5 >= mapY and 272*4 <= mapY+272*4 then screen:blit(0-mapX,272*4-mapY,map_back) end --17
if 480*2 >= mapX and 480 <= mapX+480 and 272*5 >= mapY and 272*4<= mapY+272*4 then screen:blit(480-mapX,272*4-mapY,map_back) end --18
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*5 >= mapY and 272*4 <= mapY+272*4 then screen:blit(480*2-mapX,272*4-mapY,map_back) end --19
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*5 >= mapY and 272*4 <= mapY+272*4 then screen:blit(480*3-mapX,272*4-mapY,map_back) end --20
if 480 >= mapX and 0 <= mapX+480 and 272*6 >= mapY and 272*5 <= mapY+272*5 then screen:blit(0-mapX,272*5-mapY,map_back) end --21
if 480*2 >= mapX and 480 <= mapX+480 and 272*6 >= mapY and 272*5<= mapY+272*5 then screen:blit(480-mapX,272*5-mapY,map_back) end --22
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*6 >= mapY and 272*5 <= mapY+272*5 then screen:blit(480*2-mapX,272*5-mapY,map_back) end --23
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*6 >= mapY and 272*5 <= mapY+272*5 then screen:blit(480*3-mapX,272*5-mapY,map_back) end --24
if mapX>=  1440 then mapX = 1440 end if mapY >= 1360 then mapY = 1360 end

if pad:right() and mapX+480 <= 480*4 then mapX = mapX + 4*2 end
if pad:left() and mapX >= 0 then mapX = mapX - 4*2 end
if pad:up() and mapY >= 0 then mapY = mapY - 4*2 end
if pad:down() and mapY+272 <= 272*6 then mapY = mapY + 4*2 end
end
if mapX <= 0 then mapX = 0 end
if mapY <= 0 then mapY = 0 end
if mapX+480>= 480*4 then mapX=480*3 end
if mapY+272>=272*6 then mapY=272*5 end
---end of the map browsing code

--This for-loop blits houses
for i=1,table.getn(house) do
screen:blit(house[i].x-mapX,house[i].y-mapY,houseimg)
end
--blitting unit & cursor
screen:blit(player.x+1-mapX,player.y+1-mapY,Player)
screen:blit(x,y,selector)

--debug lines
if pad:l() and not oldpad:l() then debug=true end
if pad:r() and not oldpad:r() then debug=false end

--when debug mode is activated, the value of each square within the map world is printed on the screen.So we can check out if any square under any house was set 1 before the main loop.
if debug then
for c=1,table.getn(map[1]) do
for i=1,table.getn(map) do
screen:print(-mapX+math.floor((480*4)/table.getn(map[1]))*(c-1),-mapY+math.floor((272*6)/table.getn(map))*(i-1),map[i][c],Color.new(255,255,255))
end
end
end

--selector control
if math.abs(dx) > 32 then
x = x + dx / deler
end

if math.abs(dy) > 32 then
y = y + dy / deler
end

if pad:up() then y=y-8
elseif pad:down() then y=y+8 end
if pad:left() then x=x-8
elseif pad:right() then x=x+8 end

if x<0 then  x=0 end
if y<0 then  y=0 end
-- end of selector control code

--Now here is where the problem seems to be.
--First i create local variables mx and my corresponding to the target location when i order the unit to move.
if x>=table.getn(map[1])*xInterval then x=table.getn(map[1])*xInterval-1 end
if y>=table.getn(map)*yInterval then y=table.getn(map)*yInterval-1 end
local mx=math.floor((mapX+x)/xInterval)+1
local my=math.floor((mapY+y)/yInterval)+1

if pad:cross() then	orderMove=true end
if pad:start() then break end
end
--then if a unit was ordered to move, he uses the pathfinding function.If there is no way to reach the target, in theory it doesn't move from its current location.
if orderMove==true then
player.path=CalcPath(CalcMoves(map, math.floor(player.x/xInterval)+1, math.floor(player.y/yInterval)+1, mx, my))
if player.path==nil then orderMove=false end
if player.path~=nil then
player.pathLength=table.getn(player.path)
player.cur=1
player.xmove=(player.path[player.cur].x*xInterval)-xInterval
player.ymove=(player.path[player.cur].y*yInterval)-yInterval
orderMove=false
player.move=true
end
end
-- This is the unit Movement code, with animation
if player.move==true then
player.timer=player.timer+1
if player.xmove>player.x then
player.direction="right"
player.x=player.x+player.speed
if player.timer>=0 and player.timer <= 5 then Player=img[1]
elseif player.timer>5 and player.timer <=10 then Player=img[2]
elseif player.timer>10 and player.timer <=15 then Player=img[3]
elseif player.timer >15 then player.timer = 0
end
elseif player.xmove<player.x then
player.direction="left"
player.x=player.x-player.speed
if player.timer>=0 and player.timer <= 5 then Player=img[4]
elseif player.timer>5 and player.timer <=10 then Player=img[5]
elseif player.timer>10 and player.timer <=15 then Player=img[6]
elseif player.timer >15 then player.timer = 0
end
end
if player.ymove>player.y then
player.direction="down"
player.y=player.y+player.speed
if player.timer>=0 and player.timer <= 5 then Player=img[10]
elseif player.timer>5 and player.timer <=10 then Player=img[11]
elseif player.timer>10 and player.timer <=15 then Player=img[12]
elseif player.timer >15 then player.timer = 0
end
elseif player.ymove<player.y then
player.direction="up"
player.y=player.y-player.speed
if player.timer>=0 and player.timer <= 5 then Player=img[7]
elseif player.timer>5 and player.timer <=10 then Player=img[8]
elseif player.timer>10 and player.timer <=15 then Player=img[9]
elseif player.timer >15 then player.timer = 0
end
end
if player.y==player.ymove and player.x==player.xmove and player.cur<player.pathLength then
player.cur=player.cur+1
player.xmove=(player.path[player.cur].x*xInterval)-xInterval
player.ymove=(player.path[player.cur].y*yInterval)-yInterval
end
if player.y==player.ymove and player.x==player.xmove and player.cur>=player.pathLength then
player.move=false
end
end

screen.waitVblankStart()
screen:flip()
end```
Some screens

The problem is:
When i click on a location, the player moves there.Fine.
But,when I click on a house, the game crashes.
Seems the problem is the way i use the algorithm, or maybe some errors in the code.
May anybody gimme a hand ?

If anythings seems to be unclear just tell.I can even send anyone a downloadable archive with all stuff for debugging on his computer.

Thanks.

24. When you say 'crashes' do you mean hang (i.e it becomes unresponsive) or it crashes with an error? If the latter, what is the error message and on what line?

I suspect the error is to do with the house being an obstacle, the A* algorithm can never reach the target square. Ideally, you need to check that if you are clicking on an obstacle, don't run the A* algorithm or change the target destination to be a point nearest to the obstacle that you can walk to.

25. Thanks for the answer.
No it doesn't returns any error.
I just mean it becomes unresponsive.

Right, i'm currently trying what you've proposed.

EDIT:
Yeah, that was the problem.I've modified the code so when the target is an obstacle, the units move randomly on a nearest square.

Code:
```	local mx=math.floor((mapX+x)/xInterval)+1
local my=math.floor((mapY+y)/yInterval)+1
repeat
if map[my][mx] ~= 0 then orderMove = false
local loc = math.random(1,9)
if loc == 1 then my=my-1 mx=mx-1 loc = 0
elseif loc == 2 then my = my-1 loc = 0
elseif loc == 3 then my = my-1 mx = mx+1 loc = 0
elseif loc == 4 then mx = mx-1 loc = 0
elseif loc == 5 then loc = 0
elseif loc == 6 then mx = mx+1 loc = 0
elseif loc == 7 then my = my+1 mx = mx-1 loc = 0
elseif loc == 8 then my = my+1 loc = 0
elseif loc == 9 then my = my+1 mx = mx+1 loc = 0
end
else
orderMove=true
end
until orderMove == true
end
if pad:start() then break end
end
Thanks.
Now the problem will be how to use it with many units and grouped units at the same time.
I'll try to find a way out,and tell yer later.

26. ugh ugh ugh ugh ugh

Code:
```if 480 >= mapX and 0 <= mapX+480 and 272 >= mapY and 0 <= mapY+272 then screen:blit(0-mapX,0-mapY,map_back) end --1
if 480*2 >= mapX and 480 <= mapX+480 and 272 >= mapY and 0 <= mapY+272 then screen:blit(480-mapX,0-mapY,map_back) end --2
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272 >= mapY and 0 <= mapY+272 then screen:blit(480*2-mapX,0-mapY,map_back) end --3
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272 >= mapY and 0 <= mapY+272 then screen:blit(480*3-mapX,0-mapY,map_back) end --4
if 480>= mapX and 0 <= mapX+480 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(0-mapX,272-mapY,map_back) end--5
if 480*2 >= mapX and 480 <= mapX+480 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(480-mapX,272-mapY,map_back) end--6
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(480*2-mapX,272-mapY,map_back) end --7
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*2 >= mapY and 272 <= mapY+272 then screen:blit(480*3-mapX,272-mapY,map_back) end --8
if 480 >= mapX and 0 <= mapX+480 and 272*3 >= mapY and 272*2 <= mapY+272*2 then screen:blit(0-mapX,272*2-mapY,map_back) end --9
if 480*2 >= mapX and 480 <= mapX+480 and 272*3 >= mapY and 272*2<= mapY+272*2 then screen:blit(480-mapX,272*2-mapY,map_back) end --10
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*3 >= mapY and 272*2 <= mapY+272*2 then screen:blit(480*2-mapX,272*2-mapY,map_back) end --11
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*3 >= mapY and 272*2 <= mapY+272*2 then screen:blit(480*3-mapX,272*2-mapY,map_back) end --12
if 480 >= mapX and 0 <= mapX+480 and 272*4 >= mapY and 272*3 <= mapY+272*3 then screen:blit(0-mapX,272*3-mapY,map_back) end --13
if 480*2 >= mapX and 480 <= mapX+480 and 272*4 >= mapY and 272*3<= mapY+272*3 then screen:blit(480-mapX,272*3-mapY,map_back) end --14
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*4 >= mapY and 272*3 <= mapY+272*3 then screen:blit(480*2-mapX,272*3-mapY,map_back) end --15
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*4 >= mapY and 272*3 <= mapY+272*3 then screen:blit(480*3-mapX,272*3-mapY,map_back) end --16
if 480 >= mapX and 0 <= mapX+480 and 272*5 >= mapY and 272*4 <= mapY+272*4 then screen:blit(0-mapX,272*4-mapY,map_back) end --17
if 480*2 >= mapX and 480 <= mapX+480 and 272*5 >= mapY and 272*4<= mapY+272*4 then screen:blit(480-mapX,272*4-mapY,map_back) end --18
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*5 >= mapY and 272*4 <= mapY+272*4 then screen:blit(480*2-mapX,272*4-mapY,map_back) end --19
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*5 >= mapY and 272*4 <= mapY+272*4 then screen:blit(480*3-mapX,272*4-mapY,map_back) end --20
if 480 >= mapX and 0 <= mapX+480 and 272*6 >= mapY and 272*5 <= mapY+272*5 then screen:blit(0-mapX,272*5-mapY,map_back) end --21
if 480*2 >= mapX and 480 <= mapX+480 and 272*6 >= mapY and 272*5<= mapY+272*5 then screen:blit(480-mapX,272*5-mapY,map_back) end --22
if 480*3 >= mapX and 480*2 <= mapX+480*2 and 272*6 >= mapY and 272*5 <= mapY+272*5 then screen:blit(480*2-mapX,272*5-mapY,map_back) end --23
if 480*4 >= mapX and 480*3 <= mapX+480*3 and 272*6 >= mapY and 272*5 <= mapY+272*5 then screen:blit(480*3-mapX,272*5-mapY,map_back) end --24
if mapX>=  1440 then mapX = 1440 end if mapY >= 1360 then mapY = 1360 end```
why dont you do something like (untested)

Code:
```local tempX, tempY
tempX = (math.floor(mapX / 480) - 1) * 480
tempY = (math.floor(mayY / 272) - 1) * 272
screen:blit(tempX - mapX, tempY - mapY, map_back)```
or munge it into a single line to reduce readability...

27. Seriously i got to agree _df_, that is the ugliest code i've ever saw.
That code is more or less the same but with 272, multiplied every other 4 statements.
Your animation code is very..... well it can't handle more than 1 object without it being hard-coded again.

28. Right _df_, that was the same.I just wrote many lines.
Thanks.

One solution brings another trouble.

Well, as i said, the problem was due to the fact that Astar was not able to reach an obstacle.I've fixed it.But now, how may i move many units ?
I tried this.It works but with only one unit.When i create more , only the first unit can move, not all of them.

Any Idea about what's wrong ?

Code:
```--loads pathfinding algorithm
dofile("./library/pathfinding.lua")

-----------unit pics
img={}
for i=1,12 do
end
--------------Then i create 2 units for pathfinding test.by default, both of them are not selected by the player.
unit={}
unit[1]={selected=false,x = 100, y=100, exist = true, health = 100, destX=0, destY = 0, move = false, ordermove = false, path={}, cur = 1, pathLength = 0,direction ="right",speed = 5, timer = 0,img = img[1]}
unit[2]={selected=false,x = 200, y=100, exist = true, health = 100, destX=0, destY = 0, move = false, ordermove = false, path={}, cur = 1, pathLength = 0,direction ="right",speed = 5, timer = 0,img = img[1]}

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

--blitting units
for i=1,#unit do
if mapX < unit[i].x+unit[i].img:width() and mapX+480 > unit[i].x and mapY < unit[i].y+unit[i].img:height() and mapY+272 > unit[i].y then
if unit[i].exist then
screen:blit(unit[i].x-mapX,unit[i].y-mapY,unit[i].img,0,0,unit[i].img:width(),unit[i].img:height())
end
end
if key:start() and not oldkey:start() then unit[i].selected = false end
end

--these keys enable to alternate the current selected unit for debug issues.
if key:triangle() and not oldkey:triangle() then unit[1].selected = true unit[2].selected = false end
if key:circle() and not oldkey:circle() then unit[2].selected = true unit[1].selected = false end

---Pathfinding loop: Assume that collide is a 2D matrix containing 0 for walkable squares and 1 for unwalkable squares.
if cursor.x>=#collide[1]*xInterval then cursor.x=#collide[1]*xInterval-1 end
if cursor.y>=#collide*yInterval then cursor.y=#collide*yInterval-1 end
mx=math.floor((mapX+cursor.x)/xInterval)+1
my=math.floor((mapY+cursor.y)/yInterval)+1
local Xlimit = #collide[1]
local Ylimit =#collide
for i=1,#unit do
if key~=oldkey then
if key:cross() then
if unit[i].selected then
repeat
if collide[my][mx] ~= 0 then unit[i].ordermove = false
local loc = math.random(1,9)
if loc == 1 then if my > 1 and mx > 1 then my=my-1 mx=mx-1 loc = 0 else loc = 0 end
elseif loc == 2 then if my > 1 then my = my-1 loc = 0 else loc = 0 end
elseif loc == 3 then if my > 1 and mx < Xlimit then my = my-1 mx = mx+1 loc = 0 else loc = 0 end
elseif loc == 4 then if mx > 1 then mx = mx-1 loc = 0 else loc = 0 end
elseif loc == 5 then loc = 0
elseif loc == 6 then if mx < Xlimit then mx = mx+1 loc = 0  else loc = 0 end
elseif loc == 7 then if my < Ylimit and mx  > 1 then my = my+1 mx = mx-1 loc = 0 else loc = 0 end
elseif loc == 8 then if my < Ylimit then my = my+1 loc = 0 else loc = 0 end
elseif loc == 9 then if my < Ylimit then my = my+1 mx = mx+1 loc = 0 else loc = 0 end
end
else
unit[i].ordermove=true
end
until unit[i].ordermove == true
end
end
end
oldkey=key
if unit[i].ordermove then
unit[i].path=CalcPath(CalcMoves(collide, math.floor(unit[i].x/xInterval)+1, math.floor(unit[i].y/yInterval)+1, mx, my))
if unit[i].path==nil then unit[i].ordermove=false end
if unit[i].path~=nil then
unit[i].pathLength=table.getn(unit[i].path)
unit[i].cur=1
unit[i].destX=(unit[i].path[unit[i].cur].x*xInterval)-xInterval
unit[i].destY=(unit[i].path[unit[i].cur].y*yInterval)-yInterval
unit[i].ordermove=false
unit[i].move=true
end
end

-- Movement
if unit[i].move==true then
unit[i].timer=unit[i].timer+1
if unit[i].destX>unit[i].x then
unit[i].direction="right"
unit[i].x=unit[i].x+unit[i].speed
if unit[i].timer>=0 and unit[i].timer <= 5 then unit[i].img=img[1]
elseif unit[i].timer>5 and unit[i].timer <=10 then unit[i].img=img[2]
elseif unit[i].timer>10 and unit[i].timer <=15 then unit[i].img=img[3]
elseif unit[i].timer >15 then unit[i].timer = 0
end
elseif unit[i].destX<unit[i].x then
unit[i].direction="left"
unit[i].x=unit[i].x-unit[i].speed
if unit[i].timer>=0 and unit[i].timer <= 5 then unit[i].img=img[4]
elseif unit[i].timer>5 and unit[i].timer <=10 then unit[i].img=img[5]
elseif unit[i].timer>10 and unit[i].timer <=15 then unit[i].img=img[6]
elseif unit[i].timer >15 then unit[i].timer = 0
end
end
if unit[i].destY>unit[i].y then
unit[i].direction="down"
unit[i].y=unit[i].y+unit[i].speed
if unit[i].timer>=0 and unit[i].timer <= 5 then unit[i].img=img[10]
elseif unit[i].timer>5 and unit[i].timer <=10 then unit[i].img=img[11]
elseif unit[i].timer>10 and unit[i].timer <=15 then unit[i].img=img[12]
elseif unit[i].timer >15 then unit[i].timer = 0
end
elseif unit[i].destY<unit[i].y then
unit[i].direction="up"
unit[i].y=unit[i].y-unit[i].speed
if unit[i].timer>=0 and unit[i].timer <= 5 then unit[i].img=img[7]
elseif unit[i].timer>5 and unit[i].timer <=10 then unit[i].img=img[8]
elseif unit[i].timer>10 and unit[i].timer <=15 then unit[i].img=img[9]
elseif unit[i].timer >15 then unit[i].timer = 0
end
end
if unit[i].y==unit[i].destY and unit[i].x==unit[i].destX and unit[i].cur<unit[i].pathLength then
unit[i].cur=unit[i].cur+1
unit[i].destX=(unit[i].path[unit[i].cur].x*xInterval)-xInterval
unit[i].destY=(unit[i].path[unit[i].cur].y*yInterval)-yInterval
end
if unit[i].y==unit[i].destY and unit[i].x==unit[i].destX and unit[i].cur>=unit[i].pathLength then
unit[i].move=false
end
end
end

--------------
oldkey=key
screen:waitVblankStart()
screen.flip()
end```

29. Im back w/another question.my program has a password and im tryin 2 read the password from another file so u can change it. CODE:
user = system.startOSK(etc)
if pass ~= user
quit
but it wont read the password from the file. any ideas? and the real code is fine tuned.

30. what is the password? Characters & integers or just integers?

#### 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 08:35 AM.

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