How do I exclude numbers from a "choose" expression

Get help using Construct 2

Post » Wed Sep 23, 2015 11:08 am

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!
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Wed Sep 23, 2015 3:40 pm

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?
B
71
S
41
G
24
Posts: 356
Reputation: 18,464

Post » Wed Sep 23, 2015 5:06 pm

yeah, Use boolean variable to determine which character is dead and then apply effect to only that specific instance.
ImageImageImageImage
B
158
S
67
G
43
Posts: 2,603
Reputation: 36,003

Post » Wed Sep 23, 2015 7:26 pm

compare 2 values player.isalive = 1
pick random instance of player: apply effect
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Wed Sep 23, 2015 7:35 pm

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

..then...of course...Array :)
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Thu Sep 24, 2015 10:49 am

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?
Last edited by Rable on Thu Sep 24, 2015 11:28 am, edited 1 time in total.
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Thu Sep 24, 2015 11:25 am

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.
ImageImageImage
B
31
S
19
G
82
Posts: 1,038
Reputation: 46,205

Post » Thu Sep 24, 2015 11:45 am

@BackendFreak
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?
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Thu Sep 24, 2015 12:33 pm

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:

Image

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.
ImageImageImage
B
31
S
19
G
82
Posts: 1,038
Reputation: 46,205

Post » Thu Sep 24, 2015 1:39 pm

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. :/
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Next

Return to How do I....?

Who is online

Users browsing this forum: RIDETHEPIG and 9 guests