How to exit a code but not make the object exit the whole Step Event

Discussion in 'Programming' started by Makkeonmies, Jun 28, 2019.

  1. Makkeonmies

    Makkeonmies Member

    Joined:
    Oct 25, 2017
    Posts:
    104
    In an object's Step Event theres a piece of code.
    If certain conditions don't meet in this code, i want the object to quit the code, however not the entire Step event - the rest of the Step event outside the code i want to go on as usual.
    I tried with exit; but that didnt work. Or is it supposed to be done with 'return?'

    The code's smth like:

    Step event:
    if enemypar > 0
    {
    if distance_to_object(enemypar) < sight
    {
    If these conditions meet then here I want the code to end, and proceed with the Step event outside of it normally
    }
    }

    else //if the conditions do not meet
    {
    code goes on...
    }
     
  2. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    821
    Sounds like a perfect situation just to use an "if" statement or a "switch" statement.

    No need for for "exit" which quits the whole event or script. That's pretty heavy handed, although I guess you could use exit if you offloaded that section into a script...

    "Return" is generally used in scripts to hand back a value as a result of the code that has ran in the script. An example of this might be if you wrote a script that searched for a the closest enemy and you wanted the script to return the id of that enemy back to the object instance that ran the script.

    I'm a little confused by what you're asking because the psuedo code you provide looks fine.
     
    Toque and Makkeonmies like this.
  3. Makkeonmies

    Makkeonmies Member

    Joined:
    Oct 25, 2017
    Posts:
    104
    Basically there's a lot of Actions outside this piece of code in the same object's Step event, after the code. I wish to have the object exit this code if the conditions don't meet, and then proceed into the rest of the Actions in the Step event, outside the code (code=''Execute Code'' -Action).
    Only if the conditions in the code meet, it would proceed with executing the code, and then the rest of the Actions in the Step event.
    I just can't figure out what statement to use to exit the code only, and not exit the entire Step event in the same time... it seems that ''exit'' ''or return false'' statements end the whole Event from proceeding?
    To the best that i can explain it... hope it's not too complicated
     
  4. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    821
    If you want a section of code to only run when certain conditions are met then you can just use an "if" statement (or a series of nested ones).

    Code:
    
    
    
    if(condition == true){
        code;     //this code is only ran if the condition is true
     
        if(differentCondition > targetValue){
              more code;       //this code will only run if both conditions evaluate correctly because its if statement is nested inside the previous one
    
        } // this brace closes the second if statement
    
    } //this brace closes the first if statement
    
    //another way is to link multiple espressions in a single if statement
    
    if(condition = true && differentCondition > targetValue){
        another bit of code;     //this will only run if both the expressions evaluate correctly. Similiar to the nested if statement, however nested ifs allow you to tier code (ie have some run if the first if evaluates correctly and more run if both evaluate correctly.
    }
    
    
    
    some more code;            // this code is outside of all of the previous if statements so will run regardless of how the previous if statements evaluate.
    
    
    //you only need to use else when you want the code to only run when the preceding if doesnt evaluate correctly. for eg:
    
    variable =2;
    
    if(variable == 1){
        //code here runs if variable equals 1 but gets skipped if it doesnt;
    }else{
        //code here runs when ever the variable doesnt equal one, but wont run if the variable does equal one.
    }
    //code here is outside both the initial if and the follow else so runs regardless. 
    
    
     
    Last edited: Jun 28, 2019
    Toque and Makkeonmies like this.
  5. Makkeonmies

    Makkeonmies Member

    Joined:
    Oct 25, 2017
    Posts:
    104
    W
    Thanks for answer
    Would it just be easier for me to write all the Step event of the object into one long piece of code? Maybe this would be easier to implement then.
    I have now a mixture of Actions (lot of them) and some pieces of Code in the Step event, maybe its easier to combine all into a single long script.
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,460
    Code is easier to copy and paste than actions, it's easier both for sharing on the forum and reuse between your own projects. So the sooner you move over to using code more, the easier of a time you'll have in the long run. Also, once you get good at it, it's actually faster to write code than writing D&D...
     
    Makkeonmies likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice