Seeking clarity about 'else' statements in C3

  • I am a moderately experienced Construct developer who is assembling a sample platform game as part of a lesson for my game design students. As part of this game, I've added the ability to toggle the visibility of collision boxes by hitting the TAB key. The code works, but I'm confused by how C3 visually depicts the ELSE statement.

    This is the code that works:

    Figure 1. ELSE statement on a separate line

    This is the code that does not work:

    Figure 2. ELSE statement is visually indented.

    Visually, it seems like the option in Figure 2 makes more sense; indentation suggests that the ELSE statement is linked to the condition check that happened on the first line. Of course, when the code in Figure 2 is executed, C3 says 'An else condition cannot be placed here. Make sure the event follows a non-triggered event and is the first condition."

    I can totally work with this, but I'm trying to figure out how to explain the visual notation to the students. When I do it the *correct* way, as in the Figure 1, the ELSE statement appears as an independent line on the event sheet. In many programming languages, it seems like there would be some sort of visual indication that it is linked to the previous condition check as part of a code block.

    For now, I'm just going to say "If C3 sees an ELSE statement on a separate line, C3 will assume that the ELSE is connected to the condition check on the previous line. But you cannot just randomly drop an ELSE statement into the code *anywhere* on the page because C3 cannot figure out what logical check the ELSE is connected to."

    Does this make sense? If anyone has other useful ways that you like to think about (or explain) conditional logic, events, and subevents in C3 please let me know.

    Thanks!

    Aaron

  • You can't have else in a subevent because for the subevent to trigger the top event must return true, which would be a contradiction of the else statement.

  • I agree the way Else logic is handled (as distinct Events) is a little unintuitive. I would prefer that they visually appear and act as attached to the events they are depending on above, both for visual understanding and also for when I'm dragging to reorder events. When reordering events, it's easy to forget to drag the Else event along with the event above it upon which it is logically dependent; or when dragging another event, it's easy to mistakenly insert it above an Else, separating it from the event it's meant to be directly underneath.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • "The Else event runs if the previous event did not run." As moon said, a subevent only runs if the parent event DID run, which is a direct contradiction.

    It would work however, if there was an additional subevent on the same level above it.

    The way it works and looks is unique to Construct's event sheet, so I would try not to get too hung up on what "correct" should look like using other programming languages as a standard (especially if you're teaching Construct).

    If I were to teach someone how to use the else event, it would be directly related to toggles as you have done, and I would explain it exactly like how it is explained in this article scirra.com/tutorials/292/guide-to-construct-2s-advanced-event-features.

  • It's like "1" in many programming languages.

    c/c++/java/javascript/...

    if(condition)
    {
    	//actions
    }
    else
    {
    	// actions
    }

    Even in indented languages like python it's like that. I guess it would be more like "2" in languages in lisp or something.

    Anyways, events are a lot like typed languages. For example some normal events would look like this in javascript or something like that:

    // trigger events
    function startOfLayout()
    {
    	action();
    }
    
    // game loop
    while(1)
    {
    	//all non triggered events
    	// normal two condition event
    	if( condition1 and condition2)
    	{
    		action();
    	}
    	else
    	{
    		//repeat 10 times
    		for(var loopindex=0; loopindex<10; ++loopindex)
    		{
    			action();
    		}
    	}
    
    	draw_frame();
    }

    The only added complexity is when you're doing stuff with picking.

  • Thank you everyone for your thoughtful replies to my message. This is very helpful!

  • As noted putting "Else" in a sub-event is more-or-less equivalent to putting the "Else" inside the "if" block in a programming language, e.g.:

    if (condition)
    {
     // ... actions ...
     
     else {
     // ... else actions ...
     }
    }
    

    This is not only invalid syntax, it's also logically nonsensical, because it implies the "else" only runs if the condition is true, in which case it does not run, which means it can never run. Construct helps you put "else" in a place where it makes sense by showing the condition red if it's not in a valid place.

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