How do I exclude numbers from a "choose" expression

1 favourites
From the Asset Store
Add this awesome sound effects to make your visual novel come to life!
  • Hi,

    I would like to know how I can exclude numbers from a "choose" expression based on what happens in my game. Here is what I want to achieve :

    I have 4 characters and I want an effect to apply to one of them at random. Very easily I wrote

    set Value to floor(random(4))

    Value = 0 --> apply to character 1

    Value = 1 --> apply to character 2

    etc...

    Now I would like to apply the effect only to characters who are alive. Here it gets more complicated. I really can't find a way to do this efficiently.

    I planned to use "choose" but then I need a way to exclude the values corresponding to a dead character...

    Any idea how I can achieve this?

    Thanks a lot!

  • Hey, at risk of sounding foolish (because there is a big discrepancy in our experience), if your characters were in a family with an instance variable of alive/dead, could that be used as an additional condition for the effect?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • yeah, Use boolean variable to determine which character is dead and then apply effect to only that specific instance.

  • compare 2 values player.isalive = 1

    pick random instance of player: apply effect

  • and if Rable has a free version of C2...4 different characters and no family?

    ..then...of course...Array

  • Thanks a lot for the solutions! It will be quite easy to put in place.

    EDIT : not so easy actually, as each instance variable of my characters are implemented as object instance variable, not family instance variable. The problem now is that I can select the correct character using the "character" family, but how can I impact the health of the selected object instead of the health of the family member selected? (the Family has no "health instance variable, only objects).

    I tried to delete the object instance variable and recreate it in the family, but deleting it would delete all actions and conditions, and thus not be possible. Is there any solution? Rewriting my code and put all health and other object instance variables to family instance variable is not an option at this stage.

    Now I also would like to create another effect in the same vein, but this time, instead of picking an instance at random, I would like to select the alive character which has the lowest health.

    I could start the same way by selecting the characters who have their "alive" value to 1, but then I don't see how I can compare the values of their current healths in order to pick the lowest one. Is there a way to do so efficiently?

    I thought I would be able to do so with "pick by evaluate", but it seems like it could only compare different values between them, not picking the instance which has the lowest value of one family instance variable.

    Another way would be to not use families, but the way I described in the first post and do :

    min(char1.health, char2.health, char3.health, char4.health) but only taking into account the values which are higher than 0 (otherwise it means the character is dead).

    Any idea how I could do one of these?

  • I'm not sure what would be the most efficient for you, cause it depends on the number of characters etc. But you could do something like that:

    1. create an instance variable "instanceName" (or "characterName") for your characters object

    2. create a Dictionary "aliveCharacters" and put there all characters names at the beginning of the game/stage

    3. once character dies you simply remove key which equals to dead character "instanceName"

    In that way you will have a dictionary of alive characters. You can loop throu it easily and pick objects by System -> Pick instance by comparsion (Object.instanceName = aliveCharacters.Get("happyDwarf"))

    You can also create disctionaries/lists of dead characters and whatever you need in your game.

  • Thanks for the message. To reply your questions I have most of the time 4 characters to manage. They are all separate objects (but together in a family, though all instance variable are object-based (see EDIT above)). Furthermore, they may die and come back to life later in the same stage.

    I'm sorry to say that I probably don't have enough knowledge to fully understand your proposal. I can sure create such a dictionnary, but I have a hard time understanding how it could relate to picking one character at random, even more since the characters are 4 different object. Could I ask you to ellaborate a little bit further?

  • Ah Sorry I considered champions as different instances of one sprite object.

    So what I would try to pick character with the lowest health (but more than 0) is:

    Where "health" is an integer family instance variable and "isAlive" is a family boolean variable. Then you combine Family -> "Is boolean instance variable set" and Family -> "Pick highest/lowest" conditions.

    Haven't tested - just a guess.

    EDIT: nah I just realized it will never be true because it will probably pick the one with 0 health and check if it's alive or not, and it will obviously not be alive.

    EDIT 2: Well I've just tested it and it works. So first it filters the alive instances and then picks the lowest - so this is a solution for picking the lowest in health alive champion

    EDIT 3: OK... just read what you have edited above. I think rewriting your variables to family would be worth an effort, cause it will probably save you many problems in the future as well.

  • Oh that's awesome, I wasn't aware of this "pick lowest value" in a family condition. I checked for it in system conditions but was unable to find it. Great! Thanks!

    Hmm, yes I'm tempted to do the leap and remove all my "health" object instance variable by a "health2" family instance variable. But, let's face it, I have more than 25000 events in my game right now, working on it for more than one year, and as the game is "Healer Quest" I let you imagine how often I am referencing to the "health" variable... ^^' Probably thousands of times...

    Is there any way to do the job more quickly than creating "health2" as a family instance variable and edit manually ALL instance of the "health" variable to "health2"?

    At first I was thinking about copying health to health2 every tick... but I need to use health2 for the effect we are talking about (the effect will be to make the character gain life), so I doubt it will be of any use. :/

  • Well... that makes sense. It will surely work if you copy health to health2. You can leave all your conditions as they are and use health2 variable only to pick the lowest health alive.

    But it's still a workaround and not a pretty solution, but I would probably also seriously consider this move instead of manually checking/fixing 25 000 events .

    I would definetly not do it with every tick (this might affect your performance) but just copy health to health2 everytime health value change. I believe you have some "updateHealth" function? So I would put the copy part there.

  • It will surely work if you copy health to health2. You can leave all your conditions as they are and use health2 variable only to pick the lowest health alive.

    Actually, once I picked the lowest health character alive in the family, I still don't see how I can modify the health of the object's instance variable... Is this possible?

    I mean, health2 would be the family instance variable, and the family member will be selected, but how can I add health to the object instance variable without knowing which object it is, even if there is only one object picked?

    As far as I know, it is not possible to modify "health" to the family object picked if "health" is not a family instance variable, even though all the object in the family have such an instance variable defined.

  • Ah yea. Well in this case you would need another function - let's call it - "adjustHealth".

    1. Add one more Family instance variable "healthToAdjust".

    2. Everytime champion got hit/healed you set "healthToAdjust" to X or -X.

    3. Right after that you will have to run "adjustHealth" function which will check each champion/sprite (you said you have 4 of them so that should not be a problem) separately if "healthToAdjust" != 0 then "health" += "healthToAjdust". Then set "healthToAdjust" to 0 and "health2" = "health".

    This is getting a bit complicated... but in your situation it might be worth a consideration.

  • Wow, thanks a lot for your time and explanations! I think your solutions should work.

    I'll try it tonight and let you know how it goes.

  • No problem . Very good Job! The only one thing I would change is the place of mana bar. I simply do not see it while playing because my left thumb is over it. This is very important to see it during fight so some top corner place would be better. But again, great game!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)