(Solved)What am i doing wrong in this event?


Can someone tell me what am i doing wrong?
i want to pick a random CowGroup instance with variable value 0
and change it to 1

hi i think you need to ‘pick all’ cowgroup first then select only the ones with hunger 0 below it - then, and it might need to be in a sub event - pick a random from that lot - that might work - dont think you need an ‘and’ in there. I cant see why you’re repeating for each if you’re selecting a random one cowgroup

Your events implement more rules than that.

Those event’s don’t make sense either:
There is a repeat for BarnGroup.TotalFood times. In that repeat, you select all BarnFoodIndicators witin 200 pixels of the player, and if there a CowGroup with Hunger = 0, then all the BarnFoodIndicators animations are set to “Empty”.

Then you pick a random CowGroup object and repeat over it. There’s no need for the repeat as there’s only one CowGroup object selected.

Can you specify all the rules clearly, so we can suggest a correct set of events?

okay so
what i have been trying is first restrict the entire action to happen only within 200 pixels of the player
then pick random CowGroup instance with Hunger 0 and change it to 0 and i want it do it TotalFood times

I have done something similar here


and this one works but not the one i posted above

hi
i am trying to repeat because there are multiple instances of cowgroup
and i want them to be picked with hunger 0 and i want all that to happen totalfood times

Hi - in order to select the cowgroup objects with 0 hunger you need to first use ‘pick all’ cowgroup objects, then next line …hunger=0. Once youve used pick random …you’re only dealing with one object and so repeat for one object makes no sense - the repeat barngroup.cowtotalcount makes sense.
I don’t use pick random very often so im not sure how well it sits under picking all objects with hunger 0. You might need an extra global boolean variable and pick random cowgroup and repeat until you find one thats hunger 0 - then the variable turns off.

1 Like

There is only one CowGroup object because you use pick random CowGroup object. That condition only selects 1 object.

Anyway, those events you posted should work. What you haven’t done is given the content of the events - is there a parent event that they come off?

And you could try this, it should work:


Only if the CowGroup has been filtered by an ancestor event. Otherwise GDevelop will work with all CowGroup objects.

I tried but it’s not working
i will post the entire event


collects food count and animal count

and this is the second part
and I feel like some of these events are interfering with each other

And just to confirm CowGroup is a group of multiple objects

by ancestor event - you mean the event that its a sub event of? i take your point but selecting from multiple instances never seems to work for me properly unless ive used ‘pick all’ first or ‘repeat for each’.

Then it’s a good possibility that there’s a flaw in your event logic. You should look at correcting that, rather than work around it.


@OwO, I think there are a few issues here that are causing the problems you are experiencing. Firstly, these events make no sense, and I’ll explain by way of example:

Say you have 3 BarnFloor objects, the first with 25 cows on it, the second with 16 cows on it and the last with 2 cows on it.

For the first iteration of the repeat over BarnFloor, 25 cows are in collision with it. So CowTotalCount is set to 25.

For the second iteration, there are only 16 cows colliding with the BarnFloor object, and CowTotalCount is set to 16.

Onthe third and final iteration, only 2 cows are in collision with the BarnFloor object, and so all CowTotalCount variables are set to 2.

Is this what you are after, or should the CowTotalCount be 43 (25 + 16 + 2)?


If it’s 43, then you need one event. No repeats. Just this:

image

1 Like

No no doing this gives me the number of cows for each BarnGroup
BarnGroup contains Floor
so for each barngroup i get the count of cows that are in collision with the floor


BarnLayout is made from the object of BarnGroup

Ok, you’re going to have to explain the logic here. Because if BarnFloor is a member of BarnGroup, and you repeat over each BarnFloor, then the repeat for each BarnGroup will only every execute once - for the BarnFloor you are repeating over. All you’re doing is assigning the number of CowGroup objects colliding with the BarnFloor object. You may as well do it as:

What does CowTotalCount and TotalFood represent? What are the counts you are after?


What are you trying to achieve here? An external layout just has objects at set positions. By repeatedly creating from external layout, you appear to be placing the same object at the same position BarnsToCreateCount times.


each time it creates at a distance

CowTotalCount represents number of cows on floor of each barnlayout.
TotalFood represents the amount of food in each barnlayout.

Now let me explain


These are all the objects in barnlayout

These same objects are in BarnGroup it helps me set collision for player

Creates barnlayout according to my requirment

Loads the amount of cows in collision with barnfloor and foodcount in barngroup variable
and it is doing it for each barngroup

Except they don’t. When you create from an external layout, it doesn’t track which layout each object comes from. If BarnLayout has 10 cows, and you create it 5 times, then you are not creating 5 groupings of the 10 cows that were created at the same time. All cows are treated as just being on the scene.

If I have 2 different external layouts based on a scene, and I create the objects from each external layout, you would not be able to identify which the scene from which the objects originated. All an external layout does is specify objects in a scene.


Unfortunately, this is not correct. Because BarnFloor is a member of BarnGroup, only the BarnFloor of the current iteration will have the variables set. All the other BarnGroup objects do not have the variables set.

Also, there is only 1 BarnGroup object group which contains ALL the BarnFloor, BarnInteriorBorder and BarnInterior objects. It does not group them by the BarnLayout iteration in which they were created. It lumps them all together.


I get the feeling you have a misunderstanding of how external layouts and object groups work.

However, if you can specify exactly what you’re trying to achieve, maybe we can work out a solution. If it’s something like you wanting to group together the objects created at each iteration, then include that. I have an inkling of idea that may be a work around to get the external layout grouping objects together.

okay okay let’s leave what i have done
I am trying to create barn system so every time player creates a barn a barn interior is created for it. This is working

The problem is I want to create a feeding system which will compare the number of cows inside barn and how much food it has.
And then what i want to do is
if the food is less than cows few random cows will be fed
if the food is greater than the cows count few random food will be left
if both are equal all food will be empty and hunger will be filled

hope this explains everything

A potential solution (or start of). Add a number variable to BarnGroup, say named id. Then create from layout this way (my variable _i represents your BarnPositionDecider):

This will give each group of BarnGroup objects a group id, that’s different from the other groups.


I also created a structure variable to hold cow and food values for each BarnGroup groupings:




Finally, the events to populate the cow and food counts:


So you now have a structure variable with the numbers of cows and feed for each group of barn objects created per iteration.

Okay i did as you did
just few things
the first event is working



but am i doing the second one right
because i have created barn layout twice
but there are three of these and i have 4 cows in each barnlayout but it adds 4 to only one

There are 3 entries in the structure because of the ‘0’ entry added when the variable is defined in the scene variable editor. It won’t affect the game, but you can fix this by clearing the children of the structure variable at the beginning of the scene, and modifying the repeat to
Repeat VariableChldCount(BarnGroupCounts) -1 times.

As to why the values aren’t being added to the structure, I think it’s because you have the create from layout in the repeat event, and not as a sub event. From what I recall, the event needs to complete before the objects are available. The updating of BarnGroup id is a subevent of the create event, so the event with the create doesn’t flag as completed until the subevent is completed. and the subevent won’t see the objects because they haven’t been instantiated.

Check how I have 2 subevents on the initial repeat. It’s a subtle but big difference to your event structure.

So i changed the first event as you said

and the second but still results are same



it is adding all the instances into one
and we can leave the 0 part if it’s not affecting the game

No, unfortunately you didn’t. Have a look at mine. It has a repeat with 2 subevents. The first subevent creates from layout, the second sets the ID.

You have the create in the repeat event, and set the ID in an event that only executes once all the layouts have been created. So it counts all the objects at once. You want to do it in the repeat loop, straight after the layout has been created.


My creation from layout, a repeat with 2 subevents:



Your creation from layout, a repeat with creation, no subevents: