BUg in my code, proapbly to do with local/global things.

Discuss any general programming issues here
Post Reply
Bugala
Posts: 759
Joined: Sun Feb 14, 2010 7:11 pm

BUg in my code, proapbly to do with local/global things.

Post by Bugala » Sun Apr 21, 2013 8:27 pm

I just encountered strangest bug in my code i have ever had. I believe it has something to do with local / Globa, which i havent understood completely yet on how they 100 percent work (i understand maybe 90 percent of the concept)

In my code there is this line:

Code: Select all

goaldistancemap = p_MakeDistanceMap(currentmap, attacktarget.x, attacktarget.y)
Strange thing is, that if i keep using that "goaldistancemap" as the table name, it fails. If i change it to something else, then it is no problem at all.

What makes this even stranger is that I am not doing anything with goaldistancemap table inside that p_MakeDistanceMap function.

What that function does is to make temporary local "newdistancemap" variable.


I do make another goaldistancemap earlier in the code, actually i make it several times as my AI keeps using it. What I dont understand is why did it suddenly decide to stop working when working with another AI (I am makin several AIs, like just go to goal and attack nearest...)


Anyway. Does anyone have any idea how could such thing as changing that target table receiving the functions returned table crash my code?

What it complains in the end is that one of the x, y cordinate squares wouldnt be existing, which once again makes no sense, as regardless what name is the receiving table, it should make no differece upon what kind of cordinates it goes through.


In case you want to check that function in question, it is here:

Code: Select all

Function p_MakeDistanceMap(mapdata, curspotx, curspoty)

width = mapdata.width
height = mapdata.height


Local newdistancemap = {}
Local x
Local y

For x = 0 To width - 1
	newdistancemap[x] = {}
	For y = 0 To height - 1
	    newdistancemap[x][y] = { spotchecked = 0, distance = 0, tilemovementcost = mapdata[x][y].tilemovementcost, pathfromdirection="none", wayblockedbycreature=0  }
	Next
Next



startcoords = {x = curspotx, y = curspoty}
ToCheckTable = { startcoords }

Repeat
	
spot = RemoveItem(ToCheckTable, 0)

DebugPrint(spot.x.." / "..spot.y)
center = newdistancemap[spot.x][spot.y]

If currentmap.creaturedata[spot.x][spot.y].creature > 0 
   If Not (spot.x = curspotx)
	If Not (spot.y = curspoty)
	    center.wayblockedbycreature = 1
	EndIf
   Else
	If Not (spot.y = curspoty)
	    center.wayblockedbycreature = 1
	EndIf
   EndIf
EndIf


If center.spotchecked = 0
     If spot.y > 0
	  p_CompareRoute(  center,  newdistancemap[spot.x][spot.y-1], spot.x, spot.y-1, "down", startcoords) 
	  If spot.x > 0         Then p_CompareRoute(  center,  newdistancemap[spot.x-1][spot.y-1], spot.x-1, spot.y-1, "downright", startcoords, True)
	  If spot.x < width-1   Then p_CompareRoute(  center,  newdistancemap[spot.x+1][spot.y-1], spot.x+1, spot.y-1, "downleft", startcoords, True)
     EndIf
     If spot.y < height-1
	  p_CompareRoute(  center,  newdistancemap[spot.x][spot.y+1], spot.x, spot.y+1, "up", startcoords)
	  If spot.x > 0         Then p_CompareRoute(  center,  newdistancemap[spot.x-1][spot.y+1], spot.x-1, spot.y+1, "upright", startcoords, True)
	  If spot.x < width-1   Then p_CompareRoute(  center,  newdistancemap[spot.x+1][spot.y+1], spot.x+1, spot.y+1, "upleft", startcoords, True)
     EndIf     
     If spot.x > 0         Then p_CompareRoute(  center,  newdistancemap[spot.x-1][spot.y], spot.x-1, spot.y, "right", startcoords) 
     If spot.x < width-1   Then p_CompareRoute(  center,  newdistancemap[spot.x+1][spot.y], spot.x+1, spot.y, "left", startcoords)

     center.spotchecked = 0

EndIf

Until RawGet(ToCheckTable, 0) = Nil

Return(newdistancemap)	 

EndFunction

Post Reply