Abstract

Discussion in 'Programming' started by MattCast, Jan 16, 2020.

  1. MattCast

    MattCast Member

    Joined:
    Apr 2, 2017
    Posts:
    9
    Is there a way to declare abstract variables for strategy pattern use in superclass objects? I'm working on a version of pac man, and I want to have a strategy pattern for ghost AI, but I need to be able to declare an abstract variable to later be assigned a specific targeting method in the inherited create event, so that in the inherited step event I can just call that variable without worrying about the specifics of which script it calls.
     
  2. Rui Rosário

    Rui Rosário Member

    Joined:
    Oct 10, 2019
    Posts:
    45
    Take a look at event_inherited and script_execute. This way you can do something along the lines of:

    In the parent object's Create Event:
    Code:
    action = undefined;
    // other initialization code
    
    In the parent object's Step Event:
    Code:
    script_execute(action);
    // other step event code
    
    In the first child's Create Event:
    Code:
    event_inherited();
    action = scr_action_1;
    
    In the second child's Create Event:
    Code:
    event_inherited();
    action = scr_action_2;
    
    And when you create instances of the first and second child objects they will execute the correct script. Of course that this is just an example, you can instead of script indexes assign enum values or any other value with which you decide what to do.
     
    DukeSoft likes this.
  3. DukeSoft

    DukeSoft Member

    Joined:
    Jun 20, 2016
    Posts:
    800
    No, there is no real difference in "visibility" or extendability in objects in GM. Everything is public, and everything is not abstract - just there. You can overwrite events, but thats about it. You'd use `event_inherited();` and overwrite your variable under it.

    EDIT: Ninja'd with an actual practical implementation :+)
     
  4. MattCast

    MattCast Member

    Joined:
    Apr 2, 2017
    Posts:
    9
    Thanks so much! This is exactly what I was looking for, and it also answers my question about passing scripts into functions and assigning them to pointers without executing them!
     
  5. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,192
    As a, different not necessarily better, alternative you can also use user events. I tend to prefer these and often structure my code as follows.

    In parent create event:

    Code:
    
    ///variables for all children
    
    //trigger specific child setup
    event_user(0);
    
    
    In child's user event 0:

    Code:
    
    //whatever is relevant to child
    
    
    The reason I prefer this method is that it separates the child specific setup from the parents create event meaning that you can use event 0 elsewhere in code without triggering the parents create event.

    I also just like being able to look at the object events tab and have the ability to go straight to the parents create event or the child's create event.

    One important note is that you can call a non-existing event without an error so putting event_user(0) in the parent event does not require you to create an event for a child if you don't want to.
     
  6. DukeSoft

    DukeSoft Member

    Joined:
    Jun 20, 2016
    Posts:
    800
    You should probably wait for 2.3 to come along by the way, since there's going to be a lot of awesome changes that fix issues like these :+)
     

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