View previous topic - View next topic |
Author |
Message |
XMark Guitar playin' black mage
Joined: 30 May 2002 Posts: 870 Location: New Westminster, BC, Canada
|
Posted: Wed Aug 17, 2005 8:41 am Post subject: Objects accessing other objects' data |
[quote] |
|
I was just wondering, say you have two different instances of an object, and you want one instance to get data from another instance (for example, a bunch of enemies doing collision detection on each other)
I can think of several ways to do this, though what I'd really like to know is the strict OOP Nazi approach to it. _________________ Mark Hall
Abstract Productions
I PLAYS THE MUSIC THAT MAKES THE PEOPLES FALL DOWN!
|
|
Back to top |
|
|
Mandrake elementry school minded asshole
Joined: 28 May 2002 Posts: 1341 Location: GNARR!
|
Posted: Wed Aug 17, 2005 2:46 pm Post subject: |
[quote] |
|
easiest way->
make x/y public
somewhat strict OO way->
x/y private, make a function that is a friend function that can access the variables.
overly complicated way for strict OOP:
Create a struct that is made for x/y information, and maybe h/w information (for collision). Have a function that returns this struct from private data. That way, other objects can have access to the data without directly getting the data (a basic GET function).
This being, the whole point of public/private data is how much access you want to give to external etinties, and it's only worthwhile when creating a library.
Think of it this way- Why does public and private settings exist OOP terminology? So that when someone else uses your class, they don't write the wrong information into variables. It's protecting the data from getting corrupted. At least, as far as I know, is the intent. So making some data public really isn't un-OO. Just know the risks. Simple is better, IMHO, so just make it public. _________________ "Well, last time I flicked on a lighter, I'm pretty sure I didn't create a black hole."-
Xmark
http://pauljessup.com
|
|
Back to top |
|
|
DrunkenCoder Demon Hunter
Joined: 29 May 2002 Posts: 559
|
Posted: Wed Aug 17, 2005 3:33 pm Post subject: Re: Objects accessing other objects' data |
[quote] |
|
XMark wrote: | I was just wondering, say you have two different instances of an object, and you want one instance to get data from another instance (for example, a bunch of enemies doing collision detection on each other)
I can think of several ways to do this, though what I'd really like to know is the strict OOP Nazi approach to it. |
you do realize that objects of equal type have access to each others internal data as soon as they get to know about eachohter right?
so if you have:
Enemy a,b;
a.colloides( b);//a here have access to all parts of b
now if you ask me the collides function doesn't belong in enemy to begin with but that's another story. _________________ If there's life after death there is no death, if there's no death we never live. | ENTP
|
|
Back to top |
|
|
tcaudilllg Dragonmaster
Joined: 20 Jun 2002 Posts: 1731 Location: Cedar Bluff, VA
|
Posted: Wed Aug 17, 2005 8:52 pm Post subject: |
[quote] |
|
The JS way:
foo.bar = eval(bar.foo.toSource());
|
|
Back to top |
|
|
janus Mage
Joined: 29 Jun 2002 Posts: 464 Location: Issaquah, WA
|
Posted: Wed Aug 17, 2005 9:23 pm Post subject: |
[quote] |
|
LordGalbalan wrote: | The JS way:
foo.bar = eval(bar.foo.toSource()); |
eval?
you're kidding, right?
please tell me you're kidding.
|
|
Back to top |
|
|
XMark Guitar playin' black mage
Joined: 30 May 2002 Posts: 870 Location: New Westminster, BC, Canada
|
Posted: Wed Aug 17, 2005 9:31 pm Post subject: Re: Objects accessing other objects' data |
[quote] |
|
DrunkenCoder wrote: |
now if you ask me the collides function doesn't belong in enemy to begin with but that's another story. |
Well, I was thinking, for example, in an enemy's move method, it would have to check to see if it could move in that direction, and would need to check all other entities' positions, and I can't think of any elegant way to do that. _________________ Mark Hall
Abstract Productions
I PLAYS THE MUSIC THAT MAKES THE PEOPLES FALL DOWN!
|
|
Back to top |
|
|
RuneLancer Mage
Joined: 17 Jun 2005 Posts: 441
|
Posted: Wed Aug 17, 2005 11:00 pm Post subject: Re: Objects accessing other objects' data |
[quote] |
|
XMark wrote: | Well, I was thinking, for example, in an enemy's move method, it would have to check to see if it could move in that direction, and would need to check all other entities' positions, and I can't think of any elegant way to do that. |
How's this sound...
Assuming your game is tile-based, why not simply define a tile as being "occupied" or "free". Your map object would have a "MoveTo" method which takes a position. An enemy trying to move would send a request to move. The map object checks if the position is free. If it is, it marks it as occupied and returns a successful state ("true" or whatever you fancy.) Otherwise the states don't change and a failliure is returned ("false"...)
If you're not working with tiles, then you can have a dynamic list of regions and instead of checking against positions, you'd check if the bounding region of the actor falls into an occupied region in your map's list.
The advantages are that the objects are independant of each other. But the disadvantage is that data can be desynchronised easily enough (for instance, if an enemy is removed from the map but doesn't say so. The tile would remain occupied.) A "garbage collector" could be run during idle moments to check if occupied tiles really do have enemies on them, but that shouldn't be necessary if the game is properly coded not to fall in a situation where enemies would just be deleted without giving your game world some kind of notification. ;) _________________ Endless Saga
An OpenGL RPG in the making. Now with new hosting!
|
|
Back to top |
|
|
XMark Guitar playin' black mage
Joined: 30 May 2002 Posts: 870 Location: New Westminster, BC, Canada
|
Posted: Wed Aug 17, 2005 11:50 pm Post subject: |
[quote] |
|
ah yes, I actually do something similar to that in the ARC engine. When an NPC moves, it places a temporary collision tile in the space it's going to move into, then gobbles it up when it arrives, though the rest of the collision detection between NPC's is done by the NPC object calling a collision-checking function outside of the NPC's object. It works good, but it just feels like a cheap hack to me.
Anyway, the reason I'm asking is that I'm beginning to work on a turn-based strategy game, but at the same time I'm using this as an excercise to improve my coding style (prompted by my dismal performance at that job interview). _________________ Mark Hall
Abstract Productions
I PLAYS THE MUSIC THAT MAKES THE PEOPLES FALL DOWN!
|
|
Back to top |
|
|
js71 Wandering DJ
Joined: 22 Nov 2002 Posts: 815
|
Posted: Thu Aug 18, 2005 1:07 am Post subject: |
[quote] |
|
Quote: | When an NPC moves, it places a temporary collision tile in the space it's going to move into, then gobbles it up when it arrives, though the rest of the collision detection between NPC's is done by the NPC object calling a collision-checking function outside of the NPC's object. It works good, but it just feels like a cheap hack to me. |
Cheap hack? I'm not sure, if you'll notice FF6 did a similar (if not identical) thing with its NPCs.
|
|
Back to top |
|
|
RuneLancer Mage
Joined: 17 Jun 2005 Posts: 441
|
Posted: Thu Aug 18, 2005 2:00 am Post subject: |
[quote] |
|
Josiah Tobin wrote: | Cheap hack? I'm not sure, if you'll notice FF6 did a similar (if not identical) thing with its NPCs. |
Actually... I'm not sure how that doesn't make it a cheap hack. :P Considering how terrible Square's SNES programmers were, I wouldn't consider them such a good reference when it comes to SNES algorithms. We're talking about the kind of people whose code has BEQ/BNEs branching to JMP instructions, who not only have multiple ways of doing the same thing but actually rewrite code multiple times (the many random number generators and random number tables FF6 has spread between C0-C3...), and who leave their games with so many bugs a whole thread on a pretty high-end ROM hacking message board is dedicated to them...
That code does not do a thing for maps with multiple heights, such as bridges. ;) _________________ Endless Saga
An OpenGL RPG in the making. Now with new hosting!
|
|
Back to top |
|
|
biggerUniverse Mage
Joined: 18 Nov 2003 Posts: 326 Location: A small, b/g planet in the unfashionable arm of the galaxy
|
Posted: Thu Aug 18, 2005 6:52 am Post subject: |
[quote] |
|
NOTE: only works for tile-based engines.
Well, you should probably use something in between. When an NPC moves onto a tile, it should register with that tile. Say: tile.setOccupier(object);
(Don't gobble this on arrival)
Then when another NPC comes along, it just asks the tile it's trying to move to: tile.getOccupier();. Null here would be significant- it means there is no occupier, and the tile is free. When an NPC moves OFF a tile, it simply calls tile.setOccupier(null); on its old home.
This way, you're not checking every NPC every time you want to move. For tile engines, this is very optimal.
For free-moving NPCs/Objects, use recursive quadrants (Quadtrees) to handle the lookups quickly. _________________ We are on the outer reaches of someone else's universe.
|
|
Back to top |
|
|
RuneLancer Mage
Joined: 17 Jun 2005 Posts: 441
|
Posted: Thu Aug 18, 2005 5:45 pm Post subject: |
[quote] |
|
Personally, I'd give every area on the map a single byte to determine occupation, and have each bit represent one "height" unit. Then I'd just work with bits.
Suppose you have a bridge, technically two people should be able to occupy the same space. The previously mentionned methods would not allow this, and walking across the bridge when someone's underneath it would lead to walking on occupied tiles. But with this method, suppose the bridge goes up 3 units above the ground and you're walking underneath an NPC on this bridge, the tile occupation would be "8" (00001000) Bit 1 (ground level) isn't set, so you can freely pass underneath the NPC.
This isn't even an issue if you compare with ranges, but that means comparing against every occupied region (hardly a problem on a modern PC; even with 50 objects testing against each other every frame, that's only 2,450 comparaisons.) _________________ Endless Saga
An OpenGL RPG in the making. Now with new hosting!
|
|
Back to top |
|
|
tcaudilllg Dragonmaster
Joined: 20 Jun 2002 Posts: 1731 Location: Cedar Bluff, VA
|
Posted: Thu Aug 18, 2005 8:01 pm Post subject: |
[quote] |
|
janus wrote: | LordGalbalan wrote: | The JS way:
foo.bar = eval(bar.foo.toSource()); |
eval?
you're kidding, right?
please tell me you're kidding. |
Do you know a better way? I actually found the eval call on the 'Net, it had never occured to me that eval could be used in that way. (mostly because I try to avoid it...) A better way, of course, would be to copy the memory wholesale, but in JS that doesn't appear possible save without toSource(). The array copy functions (and everything else), just store pointers to the data, not copies of the data itself.
|
|
Back to top |
|
|
biggerUniverse Mage
Joined: 18 Nov 2003 Posts: 326 Location: A small, b/g planet in the unfashionable arm of the galaxy
|
Posted: Fri Aug 19, 2005 3:15 am Post subject: |
[quote] |
|
Galby, although JS is generally a useful programming language, I think that in this thread it's doing little more than getting in the way of helping XMark. I'm going to assume that XMark is not interested in how the problem would be solved in JS, since I don't think his code is written in JS.
I believe that it is unnecessary to continue talking about JS (Janus, you too) in this thread. If you find that you can take the knowledge you have of JS and apply it to C++, please feel free to post that application of your knowledge (test it first).
EDIT: The downside of your method, RuneLancer, is that you don't know who is occupying a tile. Further, I'd use layers of tiles if you are going to have bridges etc. _________________ We are on the outer reaches of someone else's universe.
|
|
Back to top |
|
|
RuneLancer Mage
Joined: 17 Jun 2005 Posts: 441
|
Posted: Fri Aug 19, 2005 4:07 am Post subject: |
[quote] |
|
biggerUniverse wrote: | The downside of your method, RuneLancer, is that you don't know who is occupying a tile. Further, I'd use layers of tiles if you are going to have bridges etc. |
Yeah, there is that. Kinda slept my mind when I came up with that algorithm. ;) _________________ Endless Saga
An OpenGL RPG in the making. Now with new hosting!
|
|
Back to top |
|
|
|
Page 1 of 2 |
All times are GMT Goto page 1, 2 Next
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|