Friendly and Enemy unit same family hit condition

0 favourites
  • 14 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hi everyone,

    I am currently playing around to create a simple game where the player can create units that run from the left to the right to hit the enemy.

    Setup: multiple unit types(soldier, catapult etc..) in one family "Unit". An instance variable isEnemy is tagging such units as coming from the opponent.

    How can I check that a Unit attacks if another Unit in range and this Unit is an enemy? As soon as I have checks on x variable <Unit.BBoxLeft-Unit.Range Units> & condition <Unit is not friendly> the units start to attacking immediatly after spawning.

    I read the tutorials but I really hoped to avoid checking unit against all object types/uuids or saving evyerthing in local variables somehow.

    Any tips?

    Many thanks

    Nico

  • Can't you just compare it like this:

    is in range and variable is NOT equal to self.variable ----> attack

    Then both your own and the enemy units will attack all units that doesn't have the same "team variable".

  • Can't you just compare it like this:

    is in range and variable is NOT equal to self.variable ----> attack

    Then both your own and the enemy units will attack all units that doesn't have the same "team variable".

    Interestingly it is not possible to compare the current Unit boolean to self.<boolean> - it only works with numbers/self so i will change to a 0/1 instance variable setup then

    Many thanks!

  • Hi again,

    i am afraid that didnt work (they pass through each other), what am i doing something wrong?

    Thanks

    Nico

  • Is there any elegant solution for this (I assume very common) problem?

    How can I check conditions between two objects of same family without looping through uids or all objects

  • You don't need to make it so hard for yourself by grouping Enemies and Friendlies into one Family then trying to separate them out. Just reference the enemies in an Enemies family if there are multiple enemy units and Friendlies in their family, otherwise you're going to constantly stumble across weird problems with the picking that need investigation. Your picking here doesn't really reference the Unit correctly, the isFriendly condition is referencing the Unit that is passing the range, so itself, and not the Unity that was just passed. If you have separate families as suggested then you can reference them much easier such as if Friendly X > Enemy then Friendly set anim etc.

  • Thank you very much,

    yes this is something I played with. The odd thing is that I cannot create the same variable names on Family level.

    Means I would need to introduce a second set of family instance variables now, this was something i wanted to avoid.

    Also, if i dynamically create such new units now, how can i create an object of a certain family if it is in two? System.CreateObject doesnt allow a selection of Family.Object?

    Many thanks

    Nico

  • Even though it is in a family you can still pick it as an object so you just create the object type.

  • I understand but in my case i need to know if an object is "Enemy" family or "friendly" family. So how can i spawn an object specifically of the ENemy family or friendly family?

    Thanks

    Nico

  • Sorry for the late reply. It is a bit complicated to make it work and getting the picking right is frustrating. But it is possible.

    But as plinkie said, it is so much easier to just make different objects/families. It saves you a ton of time because it gets really complicated to pick between the same objects all the time.

    I don't know if the plugin "nickname" is available for C3 but that plugin makes it possible to create objects by name which makes it super easy to spawn exactly the unit you want from a family.

    But you can of course also just spawn the object you want instead of spawning a family. If you need to spawn a knight the just spawn the knight object.

  • Thanks so much for your reply.

    Am i doing something fundamentally wrong? The Create object by name (via system) doesnt allow to spawn it from a certain family. I want to avoid having friendly objects and enemy objects (as this could add up and the units do not differ in terms of features).

    I added objects into these two families. I cannot use the same family variables again and the objects in enemy will have features of the objects in Friendly Family...

    My hope is i dont need to create features for Enemy units and Friendly units as the units are the same (features and behaviours), just the direction of the movement is different..

    My best case would be to have something like the below:

    Create Soldier of family Friendly

    Create Soldier of family Enemy

    Friendly in <range> of Enemy: Fire animation etc...

    Many thanks for your help

  • The instance variable defines whether they are friendly, so you create the object and set its variable as such. Families are used to stop repeating events that apply to all members of a family, as seen in the logic if Friendly is in range of Enemy, which replaces if Friendly Soldier is in range of Enemy, if Friendly Catapult is in range of Enemy etc

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The instance variable defines whether they are friendly, so you create the object and set its variable as such. Families are used to stop repeating events that apply to all members of a family, as seen in the logic if Friendly is in range of Enemy, which replaces if Friendly Soldier is in range of Enemy, if Friendly Catapult is in range of Enemy etc

    as a poster before suggested to introduce a second family for enemies i thought i could get rid of the extra variable for isFriendly. Why do i need two families then if i cannot spawn an object as part of a certain family?

    Many thanks

    Nico

  • Hi again,

    i am afraid that didnt work (they pass through each other), what am i doing something wrong?

    Thanks

    Nico

    That doesnt work because you check the isFriendly Boolean for the same object, its like checking 1<>1 and expecting it to be true

    what you can do: (your setup is i think: catapult and melee object, both in 2 families right?)

    (when you check if in range you do this with a "for each friendly" loop right?)

    (here in code so the indent is there)

    for each friend (friend = family):
        condition: 
             INVERTED: pick enemy with UID friend.UID            //dont pick enemy with the same UID as the friendly you are just looping through
             pick by evaluate: enemy.isFriendly <> friend.isFriendly              //pick enemies that are really enemies (isFriendly Bool is different)
             pick nearest enemy (enemy = other family)
                  here check range from friendly to enemy family (bbox or what you want)
    [/code:2vsxg5ty]
    
    note that you can do this here because you use 2 objects (family which you loop through, and enemy)
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)