start of layout + For each

0 favourites
  • 14 posts
  • I have variable_1 = 0. On start of layout I create 2 arrays. From on start of layout there is a subevent: For each object(array) do variable_1=variable_1+1. Debug says I have 3 arrays, why value of variable_1=1 and not 3? The same happens when instead of subevent I call function after creating arrays. Function has on call - subevent(For each array do variable_1=variable_1+1).

  • Post your code for best chance of help.

  • (Pic has been cut Right click to see full)

    Additional questions:

    1. How do I write

    Example 1:

    If (condition 1)

    {

    do 1

    If(condition 2)

    {

    do 2

    }

    }

    Example 2: (This is what happening and I want the logic to work as Example 1)

    If (condition 1)

    {

    If(condition 2)

    {

    do 2

    }

    do 1

    }

    2. How can I see code "written" in Construct in text editor?

  • Hello T4U,

    Last things first, you can't see the code ever from within c2 environment (as much as I know, that is). Maybe it's possible to view it from outside, after it's been changed to html5, but I'm not sure about it either. And in time you actually notice what you do inside c2 is the code and you forget the need to see it. I know because I came from XNA and currently I don't find anything I can't do with it. (Except 3d and per pixel data read on sprites, which is actually possible and has been done in C2, it's just that "I" can't do it.)

    The code you want is pretty easy, though;

    While a condition is selected press "b" to create a sub event. Then do it one more time, thus you would have two sub events.

    In the first sub event enter condition 1 and do whatever you want

    In the second sub event, press x first to make it an "else" event, then enter your condition 2.

    You can repeat the process as much as you want. This is actually an if-else if chain statment.

    See you around

  • To the original question(with pics): Any idea why variable_1=1 and not 3?

    About the logic I truly hate it. I know what I want to write but the use of it is so difficult. Maybe I will get used to it but for now <angry face>. It would be nice if there was set of 10-20 code-logic pictures along with Statement it checks.

  • If you take the for each out of the sub event and just have it as a separate event

    so it becomes something like

    start of layout....(set up as before)

    (separate event)

    if variable1=0

    for each array -- set variable1 to variable1+1

    do you get the right answer ?

    (not checked)

  • You have 3 arrays, the one already on the layout, and the two newly created. As to why your variable equals 1, it's simple : Iteration only occurs on objects that are available on the top level condition. So using RamPackWobble suggestion should do the trick !

  • You don't need the For each. Array.Count will give you the number of array objects. Do you really need to create dynamic instances of arrays? If now, adding them on the layout will probably be easier.

  • RamPackWobble:

    It does the trick if you add trigger once.

    Magistross:

    What do you mean by "on the top level condition"? Where is the problem in my thinking. I am creating array before I call For each, so why newly created arrays are not counted?

    blackhornet:

    I am aware of the fact that you can use Array.Count. This topic is spawn child of another problem. I planned to use "for each object" for selecting instances as the construct logic for picking up instances is strange for me. Btw adding things on layout is noobish + I will need my object to do "On created events".

  • Ok - been playing.

    gives

    but, to be honest, I can't see why gvCountBlue2 is different ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • RamPackWobble The for each blue sprite on line 2 runs exactly ten times, once for each iteration of the outer loop. The picked instances list after a "Create object" get limited to that particular instance, so the blue sprite already on the layout never get picked for line 2.

    t4u What I meant is if you create multiple objects in a single event and you want to iterate through them afterwards, you're better off duplicating the creation event and doing the iteration on this one. It's hard to explain, so here's a picture. The "creation event" is a simple "start of layout" in this case.

  • Magistross - thanks for the explanation.

  • Ty. Strange indeed.

  • You could also use "Wait 0" after creation and still use a sub event.

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