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 9315047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:17:15+00:00 2026-06-19T02:17:15+00:00

I have a situation where troops can attack buildings. Each troop keeps a pointer

  • 0

I have a situation where troops can attack buildings. Each troop keeps a pointer to its target.

@property (nonatomic, weak) Building *target;

In an update loop, the troops periodically cause damage to their target.

if (_target)  
{
    if (/*enough time has passed since last attack, attack again*/)
    {
        [_target attack];
        if (_target.health <= 0)
        {
            [_target removeFromParentAndCleanup:YES];   //Cocos2d
            _target = nil;
        }
    }
}
else /* Find new target */ 

The problem is:

  • troop1 deals the blow that fells building1 and moves on to building2
  • troop2 was attacking building1 but waits until its next attack to determine that building1 is now nil.

I realise the problem is that troop2‘s pointer has not been set to nil and instead I should be checking that the value of the pointer is nil.

I tried using if (*_target) but was met with the message

Statement requires expression of scalar type

If there a way to achieve this kind of comparison in Objective-C? What other options are there for determining when a value has changed? KVO? Some extensive delegate pattern?

  • 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-19T02:17:17+00:00Added an answer on June 19, 2026 at 2:17 am

    It is the pointer itself that is set to nil when the object it points to is deallocated. if (objectPointer == nil) is always the way to check if an object is nil in Objective-C/Cocoa. If the pointer is not nil, it means the object in question has not in fact been deallocated. If you dereference a pointer to an object, you get a struct, hence the compiler error about needing a scalar value in the if expression.

    So, in your case, if if(self.target != nil) is not giving you the result you expect, you should look for remaining strong references to the target (from other objects).

    More broadly, as hinted at by trojanfoe’s answer, you’re relying on ARC’s zeroing weak reference behavior for real program logic. In theory this is OK, as (contrary to his initial statement), ARC’s zeroing weak behavior is reliable/deterministic. But, it does mean that you have to ensure that targets are always deallocated when they’re no longer on the playing field (or whatever). This is a bit fragile. Zeroing weak references are intended as a way to avoid retain cycles (essentially a form of memory leak), rather than as a way to implement logic the way you’re doing. The gist of trojanfoe’s solution, where you explicitly register and unregister targets as necessary, is probably a more robust solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation where a user can manipulate a large set of data (presented
I have a situation with autofac registration where services are related to each other.
I have situation in which i'm compelled to retrieve 30,000 records each to 2
I have situation where a publisher application essentially keeps a view model up to
I can't make git push origin B. I have situation something like this _____________________________________
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in
I have situation in which I read a record from a database. And if
I have situation where I need to change the order of the columns/adding new
I have situation like this: user submits form with action='/pay' in '/pay' I have

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.