Accessing tiles in a tilemap

Started by
16 comments, last by PierreNilsson 12 years, 11 months ago
Those results aren't really surprising when you're using % instead of /.

edit: Unless of course the column labels are in the wrong place and it simply means you're not handling negative numbers correctly.
f@dzhttp://festini.device-zero.de
Advertisement

however this would slow it down and I don't want to turn to this.

Anything you do will slow down something. The question is whether it is too slow.

How many times are you calling it, and why?
What are you calling it for? How are you calling it?

[quote name='coderWalker' timestamp='1306120567' post='4814434']
however this would slow it down and I don't want to turn to this.

Anything you do will slow down something. The question is whether it is too slow.

How many times are you calling it, and why?
What are you calling it for? How are you calling it?
[/quote]

This.

Seriously, unless you profile to know for sure, there is a very real chance that you are merely wasting your time and disrupting your code for something that might not make a difference.
Important safety tip: the modulo operator in C and C++ (%) returns a negative value for a negative dividend.

Since you're using power-of-two sizes, you can use bitwise operations instead:
  • (x / 16) becomes (x >> 4), though the compiler most likely performs that optimization on its own
  • (x % 16) becomes (x & 15), without the aggravation of dealing with negative values

Seriously, unless you profile to know for sure, there is a very real chance that you are merely wasting your time and disrupting your code for something that might not make a difference.
[/quote]

This is called a large amount of times per frame.
So any speedup in this code would definitely help.

Do you mean use a Code Profiler?

I am making a game where every tile is an element and reacts with other elements, so this is largely contributing to the speed of the game.

Basically I am about to rewrite how the engine accesses the tiles with this % algorithm opposed to the while statements which is thousands of lines with hundreds needing changing.

Is there a faster routine to access the tiles then this?
I need to decide now, to prevent me from having to rewrite it again.
If this post was helpful please +1 or like it !

Webstrand

This is called a large amount of times per frame.
So any speedup in this code would definitely help.

Most of the time the fastest speedups are not optimizing one piece of code, but rethinking on a larger scale why and how you use that code.

I am making a game where every tile is an element and reacts with other elements[/quote]
Does every tile really need to react every frame? I seriously doubt it. Why can't each element react once a second, with the elements' updating being spread out over that second? That'll instantly be about 30 times faster.

Example: (pretending your game only runs at 10 frames a second, and that you only need to update 1000 tiles)
Frame 1: Tiles 1 through 100 update and react.
Frame 2: Tiles 101 through 200 update and react.
Frame 3: Tiles 201 through 300 update and react.
Frame 4: Tiles 301 through 400 update and react.
Frame 5: Tiles 401 through 500 update and react.
Frame 6: Tiles 501 through 600 update and react.
Frame 7: Tiles 601 through 700 update and react.
Frame 8: Tiles 701 through 800 update and react.
Frame 9: Tiles 801 through 900 update and react.

Frame 10: Tiles 901 through 1000 update and react.

Frame 11: Tiles 1 through 100 update and react.

This is just one quick thought that pops into my mind, there are many many more ways you can resolve your problem. Optimizing that one piece of code only saves minuscule amounts of time. Optimizing things on a larger scale saves larger chunks of time. Think outside the function - how can the process of each frame as a whole be faster?

Basically I am about to rewrite how the engine accesses the tiles with this % algorithm opposed to the while statements which is thousands of lines with hundreds needing changing.[/quote]
Never do the same thing in more than one or two spots. Make it so you do it once, and re-use that code in a function or class. Don't copy and paste the code a hundred times; just use the function a hundred times. You can then change the inside of the function all you want without having to rewrite the areas that use the function.

I need to decide now, to prevent me from having to rewrite it again.[/quote]
What you ought to do is write it in a way that changing one area does not force you to change 100 other areas.

Does every tile really need to react every frame?[/quote]
No. When a tile is modified I add that and the 6 touching tiles to a "Watch List".
Then when updating it just goes threw the list.
So basically lets say you light wood on fire.
The wood and fire would be added.
When updating 1 frame would just change the wood to fire and add an adjacent wood.
And would repeat until stops reacting.
Thats the best method I can think of, know of a better one?


Frame 1: Tiles 1 through 100 update and react.
Frame 2: Tiles 101 through 200 update and react.
Frame 3: Tiles 201 through 300 update and react.
Frame 4: Tiles 301 through 400 update and react.
Frame 5: Tiles 401 through 500 update and react.
Frame 6: Tiles 501 through 600 update and react.
Frame 7: Tiles 601 through 700 update and react.
Frame 8: Tiles 701 through 800 update and react.
Frame 9: Tiles 801 through 900 update and react.
[/quote]
I have considered this, but wont this make it look like scanlines going across the multiple chunks?
I mean if a wood path is going back and forth between chunks when burning then one chunk may be behind

Again thanks! All of this is making me have new ideas of how it should work to be optimal!
If this post was helpful please +1 or like it !

Webstrand
I use the speed trick Servant suggested in batching updates across multiple frames for my NPCs AI and it works really great. Basically I separated the functions into periodic and constant AI. Constant = Needs to update on every frame such as movement, animation and sensory input. Periodic = Goal and Need driven AI and Pathfinding. I've read if from the book AI Game Programming Wisdom. If you didn't know it, you would think that I update the AI on every frame but I only update 20 NPCs per frame and i have a couple of hundreds of them. But then again, it largely depends on how often you really need to update.

This topic is closed to new replies.

Advertisement