Author Topic: REMOVED  (Read 461 times)

Offline Cyndanera

  • Jr. Member
  • **
  • Posts: 13
REMOVED
« on: June 20, 2018, 03:03:59 AM »
REMOVED

Offline Matty

  • Hero Member
  • *****
  • Posts: 573
    • MattiesGames
Re: Need help! with check for tiles
« Reply #1 on: June 20, 2018, 06:23:10 AM »
It's hard to be precise but is it possible that add_data is local and is not actually reaching '2' - because it goes to '1' then goes out of scope when the function is exited?

Offline Cyndanera

  • Jr. Member
  • **
  • Posts: 13
REMOVED
« Reply #2 on: June 20, 2018, 06:27:12 AM »
REMOVED

Offline Matty

  • Hero Member
  • *****
  • Posts: 573
    • MattiesGames
Re: Need help! with check for tiles
« Reply #3 on: June 20, 2018, 06:40:38 AM »
Yes...but does it actually reach '2'?

If it is going out of scope before it is added to again it will revert to 0 and only alternate between 0 and 1.

If it is global then it will continue to add each time you enter that function correctly (when you click) and go to 3 and so on.

But I'm guessing it is going out of scope.

Offline Cyndanera

  • Jr. Member
  • **
  • Posts: 13
REMOVED
« Reply #4 on: June 20, 2018, 10:05:37 AM »
REMOVED

Offline Derron

  • Hero Member
  • *****
  • Posts: 1511
Re: Need help! with check for tiles
« Reply #5 on: June 20, 2018, 10:22:57 AM »
I haven't checked your code but my eyes were glued to this:

Code: BlitzMax
  1. For t:tile = EachIn tile_list
  2. If add_data = 2 And MouseX()/tsize = t.x And MouseY()/tsize = t.y Then DestoryTile(t)
  3. Next
  4. If add_data = 2 Then
  5. Select tiletype
  6. Case 1
  7. CreateTile(1, MouseX()/tsize, MouseY()/tsize)
  8. End Select
  9. EndIf
  10. EndIf
  11.  

Does "DestoryTile" (sic!) alter "add_data"? If not, you could save some comparisons:

Code: BlitzMax
  1. If add_data = 2
  2.   local mouseTileCol:int = MouseX() / tsize
  3.   local mouseTileRow:int = MouseY() / tsize
  4.   For t:tile = EachIn tile_list
  5.     If mouseTileCol = t.x and mouseTileRow = t.y Then DestoryTile(t)
  6.   Next
  7.  
  8.   Select tiletype
  9.     Case 1
  10.       CreateTile(1, mouseTileCol, mouseTileRow)
  11.    End Select
  12. EndIf
  13.  

And even this can have issues: you remove a tile in "DestoryTile" so the "eachIn"-iterator might create trouble (skipping items in the for-loop). Do avoid concurrent list modifications when compiling with "vanilla" (NG and maxmods/brl.mod might have some checks for this issue).
Others do not do it and seem to not have trouble but in my game TVTower we experienced issues with concurrent list modifications which is why Brucey added some stuff to the linkedlist (TList)-module.

Aside of that: what did my optimization do:
- only check "add_data" once
- avoid calculating "col/row" for each tile in the list and also for the tile-creation call by using a calculated-once local variable.


There is one part which could get optimized a bit more:
But this only works if there is only one tile per "grid cell" (col/row)
Code: BlitzMax
  1.   For t:tile = EachIn tile_list
  2.     If mouseTileCol = t.x and mouseTileRow = t.y Then DestoryTile(t)
  3.   Next
  4.  
  5. 'becomes
  6.  
  7.   For t:tile = EachIn tile_list
  8.     If mouseTileCol = t.x and mouseTileRow = t.y
  9.       DestoryTile(t)
  10.       exit 'no need to check other tiles, we already found one
  11.     EndIf
  12.   Next
  13.  

bye
Ron

Offline Cyndanera

  • Jr. Member
  • **
  • Posts: 13
REMOVED
« Reply #6 on: June 20, 2018, 05:48:10 PM »
REMOVED

Offline Derron

  • Hero Member
  • *****
  • Posts: 1511
Re: Need help! with check for tiles
« Reply #7 on: June 20, 2018, 10:55:42 PM »
You know the problem so you might als know the solution.

if you do not want a variable to increment, only increment in the cases in which it should. So here: only increase if there is a tile.


bye
Ron