1. Hey! Guest! The 31st GMC Jam will take place between Nov 16th, 12:00 UTC (Friday noon) and Nov 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 [SOLVED]with statement issue

Discussion in 'Programming' started by Kezarus, Nov 8, 2018 at 10:06 PM.

  1. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Hi! I'm having major problems with the statement "with". I ussualy call it like:

    Code:
    with(objPeople){
        if( OwnerName != "" ){
            //do stuff
        }
    }
    Then, after 2 hours of gameplay or more I get erros like:
    [​IMG]

    [​IMG]

    Those errors happen inside the objects that have no relation with to objPeople, like Game Maker is calling objCamera inside it or objArcaneShot.

    The sample code from error 1 is:
    Code:
    ///TownPeopleConceal( Alarm Index )
    if( !instance_exists(objWorldMapController) ){
        alarm[argument0] = -1;
        image_alpha = 1;
    }
    
    if( Conceal ){
        //IS SOMEONE CLOSE?
        var IsSomeoneClose = false;
        var MyOwnerName = "";
        var MyRace = -1;
        var MyType = "";
       
        if( object_index == objTown ){
            MyOwnerName = Name;
            MyRace = TownRace
        }else
        if( object_index == objBuilding ){
            MyOwnerName = OwnerName;
            if( instance_exists(TownOwner) ){
                MyRace = TownOwner.TownRace;
            }
        }else{
            MyOwnerName = OwnerName;
            if( instance_exists(Owner) ){
                MyRace = Owner.TownRace;
            }
            MyType = Type;
        }
       
        with(objPeople){
            if( id == other.id ){
                //SKIP MYSELF
            }else
    //************** ERROR HAPPENS HERE
    //IT CALLS OBJCAMERA FOR NO REASON
            if( OwnerName != MyOwnerName || MyOwnerName == "" || OwnerName == "" ){
                if( point_distance(other.x, other.y, x, y) <= 5*TileSize ){
                    var LocalRace = -1;
                    if( instance_exists(Owner) && object_index != objPeoplePlayer ){
                        LocalRace = Owner.TownRace
                    }
               
                    if( object_index == objPeoplePlayer ){
                        IsSomeoneClose = true;
                    }else
                    if( MyType == Type && MyType != "" ){
                        //DON'T BREAK CONCEAL
                    }else
                    if( MyRace == Races.WoodElf && Type == "Beast" ){
                        //DON'T BREAK CONCEAL
                    }else
                    if( LocalRace == Races.WoodElf && MyType == "Beast" ){
                        //DON'T BREAK CONCEAL
                    }else{
                        IsSomeoneClose = true;
                    }
                }
            }
        }
       
        if( IsSomeoneClose ){
            image_alpha += 0.05;
        }else{
            image_alpha -= 0.05;
        }
    
        image_alpha = clamp(image_alpha, 0, 1);
        if( image_alpha <= 0 || image_alpha >= 1 ){
            alarm[argument0] = 15;
        }else{
            alarm[argument0] = 1;
        }
    }else{
        alarm[argument0] = -1;
        image_alpha = 1;
    }
    The sample code from error 2 is:
    Code:
    ///RADAR
    alarm[3] = 30*2; //RADAR
    
    if( !global.Paused ){
        ClosestEnemy = noone;
        ClosestEnemyBuilding = noone;
       
        var MyRace = -1;
        if( instance_exists(Owner) ){
            MyRace = Owner.TownRace;
        }
       
        with(objPeople){
            if( id != other.id ){ //NOT MYSELF
                //NOT FROM MY TOWN OR NO TOWN
    //************** ERROR HAPPENS HERE
    //IT CALLS OBJATTACK FOR NO REASON
                if( OwnerName != other.OwnerName || OwnerName == "" ){
                    //5 SQUARES RANGE AND VISIBLE
                    if( point_distance(x,y,other.x,other.y) < 5*TileSize && image_alpha > 0.5 ){
                       
                        var closest = false; //SEARCH FOR THE CLOSEST
                        if( other.ClosestEnemy != noone && instance_exists(other.ClosestEnemy) ){
                            if(
                                point_distance(x,y,other.x,other.y) <
                                point_distance(other.ClosestEnemy.x,other.ClosestEnemy.y,other.x,other.y)
                            ){
                                closest = true;
                            }
                        }else{
                            closest = true;
                        }
                   
                        if( closest ){
                            var MarkEnemy = false;
                            if( other.EvilFlag == false ){
                                if( MyRace == Races.WoodElf && Type == "Beast" ){//IF WOOD ELF X BEAST
                                    MarkEnemy = false;
                                }else
                                if( EvilFlag == true ){ //EVIL IS HATED BY ALL
                                    MarkEnemy = true;
                                }else
                                if( ReputationFunc(other.OwnerName, OwnerName) <= 30 ){ //REP < 30
                                    MarkEnemy = true;
                                }
                            }else{
                                //EVIL HATES ALL BUT SELF
                                if( EvilFlag == true ){ //IF ME AND OTHER ARE EVIL
                                    if( other.Type != Type ){ //AND DIFF TYPES
                                        MarkEnemy = true;
                                    }
                                   
                                    if( OwnerName != other.OwnerName ){
                                        MarkEnemy = true;
                                    }
                                }else{
                                    MarkEnemy = true;
                                }
                            }
                           
                            if(MarkEnemy){
                                other.ClosestEnemy = id;
                                other.alarm[1] = 1;//BEHAVIOR ALARM
                                other.State = "ENEMY ALERT";
                            }
                        }
                       
                       
    
                    }
                }
            }
        }
       
        //LONERS DON'T ATTACK BUILDINGS
        if( object_is_ancestor(object_index, objPeopleLoner) ){
            exit;
        }
       
        //NO ENEMIES ON SIGHT
        if(ClosestEnemy == noone){
            with( objMapDestructible ){
                //5 SQUARES RANGE AND VISIBLE
                if( point_distance(x,y,other.x,other.y) < 5*TileSize && image_alpha > 0.5 ){
                   
                    var closest = false; //SEARCH FOR THE CLOSEST
                    if( other.ClosestEnemyBuilding != noone && instance_exists(other.ClosestEnemyBuilding) ){
                        if(
                            point_distance(x,y,other.x,other.y) <
                            point_distance(other.ClosestEnemyBuilding.x,other.ClosestEnemyBuilding.y,other.x,other.y)
                        ){
                            closest = true;
                        }
                    }else{
                        closest = true;
                    }
                   
                    if( closest ){
                        var wRepName = "";
                        if( object_index == objTown ){ wRepName = Name; }
                        else if( object_is_ancestor(object_index, objTown) ){ wRepName = Name; } //TO GET EVIL CENTERS
                        else if( object_index == objBuilding ){ wRepName = OwnerName; }
                   
                        var MarkEnemy = false;
    
                        if( EvilFlag ){ //IF DESTRUCTIBLE IS EVIL
                            if( other.OwnerName != wRepName ){ //NOT FROM THE SAME OWNER
                                MarkEnemy = true;
                            }
                        }else
                        if( other.EvilFlag ){ //IF TROOP IS EVIL
                            if( other.OwnerName != wRepName ){ //NOT FROM THE SAME OWNER
                                MarkEnemy = true;
                            }
                        }else
                        if( ReputationFunc(other.OwnerName, wRepName) <= 30 && closest ){ //REP < 30
                            MarkEnemy = true;
                        }
                       
                        if( MarkEnemy ){
                            var wTarget = id;
                            var wMyself = other;
                            var wTargetIt = true;
                            with(objBuildingFire){
                                if( TargetID == wTarget ){ //ALREADY BEING ATTACKED
                                    if( AttackerID != wMyself ){ //AND NOT BY ME
                                        wTargetIt = false;
                                    }
                                }
                            }
                       
                            if( wTargetIt ){
                                other.ClosestEnemyBuilding = id;
                            }
                        }
                    }
                }
            }
        }
    }
    If anyone knows something about the with statment and can help me I would appreciate it! =]
     
    Last edited: Nov 8, 2018 at 10:42 PM
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,262
    i would agree that on the face of it I don't see why this would happen, but GM obviously thinks they are related. You said "after two hours or more of gameplay" but I assume you get the error the first time the script is called? In other words, its not like those scripts/events run fine for two hours and then mess up? If you haven't tested this, you should.

    Otherwise, how is your inheritance set up? Which objects do objCamera and objAttack inherit from, and so on up the line?
     
    Kezarus likes this.
  3. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Hi @samspade! Thanks for helping!

    So... yep, the script runs since the start and both run on alarms set to about 30 or 60 steps and there are about 20 to 60 instances running them at a time.

    About the inheritance... well, neither objCamera nor objArcaneShot have ANY parents whatsoever... and I don't change them by code, nor any other object for that matter. They are both upload_2018-11-8_20-55-39.png

    I'm REALLY puzzled by this! Now I even put a code like below to prevent any further errors. But that is never a good thing:
    Code:
    if( !variable_instance_exists(id, "OwnerName") ){ //ULTRA SAFE GUARD
                    //DO NOTHING
                    show_debug_message("objPeopleAI->RADAR->OwnerName not found");
                }else
    If you can drop any other idea that I can look through, please do. And thanks a lot for your time! =]
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,262
    No other ideas really.
     
    Kezarus likes this.
  5. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    395
    Although unlikely, you should consider the possibility of the error message to show the wrong object reference, while in fact the variable is missing fron the objPeoe instance. You can verify this by outputting the object index name to the console right before the if condition, and check the last value in the console before the error.

    When exactly are you setting that variable in objPeople instances? Are you getting the error right at room start or randomly?
     
    Kezarus likes this.
  6. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Hi @Catan !

    Good idea! I improved the Safe Guard code like so:
    Code:
    show_debug_message(
                        "objPeopleAI->RADAR->OwnerName not found on " +
                        object_get_name(object_index)
                    );
    I'm setting the objPeople variables on the Create event and I do call the inherited event on further instances (when they have a Create event).

    This bug is of the worst kind. Can't quite reproduce it, sometimes it doesn't happen at all. And when it does, it takes a LOT of time to happen. And only after it already did run more than a 30k times (not exagerating, 20 instances running it every 2 seconds for an hour). Other objects, like objCamera, are already at the room when it starts.

    Thanks a lot for your help. Any idea is welcome! =]
     
  7. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Well, there is something really wrong and I can't figure out where. I thought maybe GM gets crazy when it stays on the same room where lot of instances are created and destroyed and the ID just gets messed up. If that's the case (I hope it's not!), then I'm stuck... =/
     
  8. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    39
    Sometimes I get errors that seem to originate from the incorrect location. For example, I have a shader that I add to a list and then read it out of the list to apply to a tile layer with layer_script_begin; but when I forgot to add the shader to the list, it was giving me an undefined variable error on an object from my asset tree that wasn't even used in my game! This kind of thing could have something to do with the way GM uses ID numbers for everything, but I really have no idea.
     
    Kezarus likes this.
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    5,412
    When all else fails, click the dustpan icon, save your project, close gamemaker completely, reboot your computer, then try again. Works 5% of the time LOL.

    So objPeople is an actual object, right? Not some variable that you yappened to name like it was a resource?

    I don't think it's possible, but make sure you didn't put "var objPeople" anywhere. lol


    ...... You know what? In both errors the instance ID is the same. It's always instance 100046. I'm curious if that's the case for all of the errors, even the ones that you didn't take screenshots of.
     
    Online Handle and Kezarus like this.
  10. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,120
    Search for all occurrences of OwnerName. What do they look like?
     
    Kezarus likes this.
  11. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Thanks @Danei, I actually use lists by the dozens for a lot of things. Maybe a did something wrong in one of them. Well, let's start mission double check. =]

    @TheouAegis what a keen eye! I will look into 100046 and see what that bloody fluffer is doing, it's probably the culprit! I did all your other suggestions before posting on the forum. =]

    I will search for all occurrences of OwnerName, @FrostyCat. What should I expect or search for?

    To all, thanks a lot for your support! I really appreciate it. =]
     
  12. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,120
    I would suggest starting with OwnerName =, which would give you all the assignments to this variable. You should make sure that:
    • Neither var nor globalvar lay their hands on one
    • It isn't behind an if statement that may skip
    • Every descendent with an overriding Create event carries either an event_inherited() call, its own assignment to OwnerName or is always created using routines that give OwnerName a starting value, and none of those initializations are behind if statement.
    • If it is part of a "with created instance" pattern, that you do not have a Create event dependent on values set by the with block.
    At this point, any calls to instance_change() should also be considered suspect, especially those changing to objects under the objPeople lineage.

    Finally, since there appears to be a constant pattern of the source instance ID (always 100046 as TheouAegis pointed out), you may want to temporarily hard-code a check for that and use a breakpoint to stop the game state for your inspection.
     
    Kezarus likes this.
  13. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Well, I look for 100046 and it was nowhere to be found.

    Then I proceed to clen up all instance_change that I had. The game is running for about...4 hours now. Well! I'm going to let it run at max speed over night and see what happens.

    Thanks a lot for the help guys! =D
     
  14. Online Handle

    Online Handle Member

    Joined:
    Oct 11, 2016
    Posts:
    237
    Would suggest running in debug mode.
    This way if the project crashes, it will breakpoint in the compiler at the location of the issue.
    Then you can still view the instance variables and maybe get some insight into what's going on in the memory.

    Edit: I just saw this is in GMS 1.4, I don't recall if debug mode operates the way I described in that version
     
  15. Kezarus

    Kezarus Member

    Joined:
    Jan 14, 2018
    Posts:
    61
    Yey! We are operating bug free for 42 years and 88 days in game terms. About 12 hours real world on x16 speed.
    upload_2018-11-10_10-22-36.png

    The culprit was instance_change, thanks @FrostyCat! And thanks everyone for the support! =D
     

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