How do I Create 1 Object for Every X Objects

Get help using Construct 2

Post » Sat Oct 15, 2016 2:24 pm

I may have over looked a post on this subject.

I would like to create 1 Blue box for every 20 Red Boxes created.

I tried the 'for every' expression but i may be using it wrong.

If anyone could share some help it would be greatly appreciated.
B
38
S
18
G
19
Posts: 664
Reputation: 14,107

Post » Sat Oct 15, 2016 3:01 pm

You could do the following:

Code: Select all
Repeat RedBox.count times
      (if) loopindex % 20 = 0
             (then) Create object BlueBox


This procedure would be repeated as many times as there are Red Boxes.
When the division loopindex / 20 returns a number without any digits behind the point (the loopindex is dividable by 20), a Blue Box is being created.

This is the way without a variable.

There also is a way with a variable, but I guess this one is sufficient for the moment.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Sat Oct 15, 2016 3:33 pm

@randomly

Thanks for the reply. I did end up using a variable.

Redcount=0
On Red destroyed > add 1 to Redcount
Redcount=20 --
random (2,5) sec > create blue box

Blue box on destroyed > reset Redcount

Works good. Now i just need to figure out how to create a random number of those blue boxes during the time the 1st blue box is created and then destroyed (which is around 3 secs before the Global Variable Redcount resets).

Thanks for the reply, again.
B
38
S
18
G
19
Posts: 664
Reputation: 14,107

Post » Sat Oct 15, 2016 3:57 pm

Well what you did is actually the simplest option you can go with.
But it doesn't work if there are more than 20, say for example 40 Red Boxes....but if that's not needed in you project, no problem with your solution.

What I don't understand though, is:
1) why does your Redcount variable go up when one instance is destroyed? Didn't you want to create one blue box for every 20 red created?

2) Why don't you just use RedBox.count? That would make the variable redundant.


And if you might explain your second issue (with the random stuff) again, more detailed (or create a new thread for it), I'm sure I can help you with that too.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Sat Oct 15, 2016 5:07 pm

@randomly

Ok, try and follow, i hope to explain this right.

Globals: RedboxCount=0, BlueboxCount=0, GreenboxCount=0

On the start of the layout there are 30 or so Redboxes, 1/3 of that is blue, 1/3 of that are green (math may be off- just as an example)

Redbox destroyed > add 1 to RedboxCount


RedboxCount = random (3,6)
(SubEvent) every random (2,6) secs
(Sub) trigger once > create Bluebox

BlueBox on destroyed > subtract 2 from RedboxCount
Add 1 to BlueBoxCount

BlueBoxCount = random (4,8)
(Sub) every random (4,7) secs
(Sub) trigger once > create Greenbox

Greenbox on destroyed > subtract 2.1 from BlueBoxCount

This may not be the easiest (not too good with variables, functions... All the hard stuff, but it works.

I watched for maybe 5-6 mins, Bluebox and Greenbox had different number of instances created, respectively.


My only big problem is trying to create a random number of each box randomly across the layout on Start of Layout. So it can be different each time.
B
38
S
18
G
19
Posts: 664
Reputation: 14,107

Post » Sat Oct 15, 2016 7:33 pm

I don't follow the last post, it looks different than what was originally asked. Here's my solution for the op.

Global redboxcounter=0

On redbox created
--- add 1 to redboxcounter

Redboxcounter >= 20
--- subtract 20 from redboxcounter
--- create bluebox
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455


Return to How do I....?

Who is online

Users browsing this forum: plinkie, tarek2, zenox98 and 17 guests