#### Archived

This topic is now archived and is closed to further replies.

# Interaction world-agent

This topic is 5225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I''m new here at the forums, so... Hi! Well... I''m developing a strategy game where you tell your troops what to do and they decide how to do it. The game is being made in Java, as I intend to make a not too complex interface for the game n HTML and use servlets so that anyone with a browser can play the game. But i digress. My question is actually about ellegant coding and design: What I don''t know is how should the agents interact with the world. First I thought about placing a pointer to the world as one of the agent''s attributes. Then the agent would call the proper world methods to do whatever it wants to do. However, with this approach not only I will have hundreds of pointers to the same thing but each agent will have more information than it needs (Of course it would only be able to call a few methods, not all of them but its not easy to make public just the right ones). My other idea was to let the world coordinate the actions (of course the world has a pointer to the agents, in the right cell), so that the agent would act in response to a world call (Its a really hard restriction, the actions would be encoded in the return value of a method). However this approach makes agent-agent interaction really complicated, cause they would have the world between them coordinating the "talk". What is the best/most used strategy? Is there a third one that is even better? Thanks! Cesar

##### Share on other sites
Well, I think the idea of having the agent have a ptr to the world seems fine to me. For starters, you seem to mainly concerned about memory. So you have to ask, how many agents will there be loaded at one time? Unless it is like 10 million, i dont see how those extra ptr''s will really consume all the much ram, this of coz assumes you target computer isnt something with like 16MB ram

As for your other concern, the agent having to much information, you dont have to give the agent a ptr to the "real" world, but the world could implement an interface that is minimal subset of functionality the agent needs, and then that agent can have a ptr to that interface (which is in turn implemented by the world).

You alternative idea, with the world calling a func in the agent is not too bad (this would be some kind of quantum based system) but the idea of using the return value the way you suggests strikes me as a very bad design decision. Also I dont see how that would limit agent to agent interaction.

An advantage to your first idea is that it would technically support "multiple" worlds, if you ever needed that.

Ultimately though it comes down to a trade off of memory vs speed in many ways.

And then finally, you overlooked one simple solution, if you know your game will only have one world, then why not make the world a singleton, and then no agent needs a ptr to that world at all. Even if you had multiple worlds you could make some global func like GetWorld(x), although I do not code in java so I don''t know if global funcs/variables are allowed.

BTW: I know the global func idea might be "bad design" but in this case, if the memory overhead is so bad, it is the only alternative i see.

Jeff

##### Share on other sites
why not make the world an agent, you could use a fixed index in the agent-array to access it, eg 0
i think its also done in q3, but i''m not sure (ok there it''s an ''entity'', but i think this is just a naming problem)
in my opinion this a quite elegant solution, because now you have a system relying only on agents and you dont have the ptr. problem, but still there''s the global var problem, because you would need a ptr to the agent-array, but it could be passed as an argument, so there is just one ptr for this array.

##### Share on other sites
Yes, I thought about the idea of the world being an agent. In fact, the 2 approach, of the world coordinating everything, is almost that. And its a good thing to have the world policing agent actions because there will be several different agents and I want to make their creation easy. So if i can prevent an agent coding error to bug the world, its good. And easier to implement with the world being the active agent, the others being reactive.

I really like the idea of giving each agent a vision of the world. With the agent approach, every turn, the world gives the agent its vision of the world, which is no different then having the agent asking the world for its vision. But when I think about the concepts involved, I like better the idea of the agent being active and not the world.

##### Share on other sites
for startes i''m the same ap
i would also use the active agents, and i think it''s not harder to implement, you just have to loop throught the whole array and call a function like void think(cWorld* world), where the world just returns from this function.

##### Share on other sites
yeah... I think in the end this is the best option. I''ll try to convince the other developer to agree with us!

Thanks!

##### Share on other sites
me again
i thougt about it, and i came up with a slightly different version: use void think(void) and a function called cAgent* getAgentByName(string name), this way you don''t have to treat any agent specially, all agents seem basicly the same, and based on your implementation of these function there is no really big performance hit

##### Share on other sites
I''m not sure I understand what you mean. If I call a function void think(void) from inside the agent, this means it will have a world pointer inside it, from which it will get sensory information. What would the getAgentByName method do? Where would it be? Its sounds interesting!

##### Share on other sites
I am not sure if you realize how small a pointer is. If each agent has a pointer to the world, that is still going to be an insignificant amount of meomory. In pretty much all games, you really only have to worry about how much memory the graphics are taking up; data is virtually free.

##### Share on other sites
i don''t know exactly how it works in java but in c++ i would write:
class iAgent{public:  virtual void think(void) = 0; // so this is an abstract class//  interface functions for the agent array  static void addAgent(iAgent*);    void remove(void); //  removes the current agentprotected:  iAgent* getAgentByName(string& name);private:   stl::vector<iAgent*> m_AgentArray;};//  now the think implementation of an example agent:void cExampleAgent::think(){  cWorld* world = dynamic_cast<cWorld*>(iAgent::getAgentByName("world"));  if(world)   {    // do some stuff with the world  }}

@Unsuspected i consider it more as a design issue than as a memory problem, because:
1) the world pointer could change
2) the world is no agent, but i would try to create a system only based on agents
3) if you would pass the world pointer as an parameter, you would treat some agents specially, but now you have to ask, wich agent should be special, wich not

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×