Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:55:21+00:00 2026-06-11T13:55:21+00:00

I’m a web dev (game dev as a hobby), and I’ve seen myself use

  • 0

I’m a web dev (game dev as a hobby), and I’ve seen myself use the following paradigm several times. (Both in developing server architecture and with video game dev work.) It seems really ugly, but I don’t know a work around. I’ll give an example in game dev, because it’s where I recently noticed it. This is an RPG that I’ve been working on. Every time a battle is launched, the CombatEngine creates two parties of Combatants. Every Combatant sets up an ArtificialIntelligence object that’s associated with the given Combatant, which is in charge of dictating moves for players which don’t receive an explicit command:

public class Combatant {

    ArtificialIntelligence ai = null;

    public Combatant()
    {
        // Set other fields here.

        this.ai = new ArtificialIntelligence(this);
    }

}

Here’s what I don’t like: the inner field (ArtificialIntelligence) takes a Combatant during construction, because it needs some of the Combatant fields in order to dictate appropriate actions. So, for convenience, I keep a reference to the combatant passed in as an arg to the ArtificialIntelligence object, but that object contains a reference to the ai object itself! It creates this weird recursion, but I don’t know how to work around it. The AI object needs a good deal of the fields that are specific to combatant, so that’s why I passed in the entire object, but I don’t like how the object then contains the reference to the ai field which is contained in the overlying combatant field, which is contained in the overlying ai class. Is this bad practice, or am I simply over thinking it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:55:22+00:00Added an answer on June 11, 2026 at 1:55 pm

    Although there is no “design” problem here – it’s just a reference you’re passing – one important consideration is that you should initialize all of your fields prior to passing this to the other class. Otherwise, the other class will have access to this in a possibly inconsistent state. This is sometimes called letting this “escape” from the constructor.

    Don’t do this…

    public class BadCombatant {
    
        ArtificialIntelligence ai = null;
        String someField;
    
        public BadCombatant() {
            this.ai = new ArtificialIntelligence(this);
            // Don't do this - ArtificialIntelligence constructor saw someField as null
            someField = "something"; 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.