1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Child objects, event_inherited and variables. Ordering issues.

Discussion in 'Programming' started by Dr.Spankenstein, Feb 10, 2019.

  1. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    5
    I have a parent object which has a create event that has a variable and calls a script which requires the correct type to be set:

    Code:
    type = Types.MyDefaultType
    scr_add_to_level() // Does things based on the type
    
    I have various child objects that will set their type in an overridden create event that calls event_inherited to make sure that the scr_add_to_level is called (this is useful as it means that I don't need to remember to call scr_add_to_level, or potentially other common functions, each time an object of this type is created).

    Code:
    type = Types.ThisObjectType
    event_inherited()
    
    The problem is that I need to make sure all of the child objects have the variable 'type' so it is currently set in the parent create event. However, setting this value overwrites the correct value which has been set in the child object's create event.

    I thought I could create a script for each one of my child objects just to be able to obtain the correct value in the parent class create event without overwriting it to the default value but that seems nuts. I'm probably missing out on a better approach which I'd like to hear about.
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,559
  3. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    5
    Is there no way to do this programmatically?
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,559
    You can add the scr_add_to_level() line to a user event and then run it with event_user(), but what's the point of that? Is it really that far beneath your dignity to use IDE features? The Object Variables mechanism works perfectly with inheritance and takes effect before the Create event, exactly what your use case needs.

    Go into your parent object, delete the line setting type, and add this to its Object Variables interface:
    objectvars1.png
    Now go into your children objects and look at their Object Variables interface. You should see type being automatically inherited:
    objectvars2.png
    This automatic inheritance will ensure that all children have a type variable before the Create event even starts. If you need to override it, click the pen icon and change the value. Furthermore, if the part setting type is the only difference between the children's Create event and the parent's, then using Object Variables would remove the need to specify the children's Create event altogether. You could just let it inherit naturally from the parent (which at this point would contain only scr_add_to_level()).

    There's this movement around the GMC that suggests being a "pro" user means using only code and lots of it, and building everything from the ground up. It's a dumb counter-culture with no practical purpose, and it severely impedes the development and use of convenience features for the GM engine. A good coder uses whatever features possible to ignore as much code as possible at a time, only rookies trying to look big tries to overstuff their plate with code and swear by it.
     
    Dr.Spankenstein likes this.
  5. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    5
    That's the kind of response I was expecting :) Will use the UI for this case.
     

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