Entity update method requires World object, but World contains this Entity

Started by
1 comment, last by Thaumaturge 2 years, 1 month ago

Hello , I'm currently working on my 2d tile based game . For the convenience of development, I divided the world into chunks and store them in the world object. In turn, the chuck has an array with pointer objects (blocks).

My block class have virtual Update() method, also the block class is the parent for some others classes(for example furnace or assembly machine), and each of this children classes have their own overwritten Update() methods. Then I'm need to iterate every block in chunks , I just call Update() method for every block in my block array. But some of block class child have Update() methods that need to have access for world or chunk object(for example, for checking neighbor blocks), this causes recursive include problem. Chunk class includes block(or block child), on the other hand block(block child) class includes chunk class.

So how I should handle Update method , if block object should have access to world ?

P.S I'm not sure if I explained the problem well, so let me know if something is not clear

Advertisement

A few options come to mind, but let me mention two in particular:

First, you could alter your “update” methods to take a reference to the “world” object as a parameter. That would then result in the “world” object being available within the scope of those “update” methods.

And second, you could keep a globally-accessible “common” object that stores a reference to the “world” object. This would then provide access to said “world” object from… well, pretty much wherever you want it.

(That said, I do note that you've tagged this with “engines and middleware”, but don't seem to have identified the engine or middleware in question. As a result, I don't know how well or whether the above might work within the architecture of that engine or middleware.)

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement