Game Maker programming question

edited in General
I'm pretty new to game maker (well since I've actually taken up programming anyway). Since then I used to do my own things from scratch, or work with unity.
I often use method overriding. It's just the most useful thing I got from OOP. Though I'm not sure what I can do in game maker which would be similar. Is there an alternative? I know about scripting, but how can I change a script to be customised for each child object?

Comments

  • Well, arguments arent strongly needed, you can easily test the arguments taken in by a script to see what you want to do? But Im unsure what you mean by "making a script customised for each child"?
  • edited
    Uhm let me try and explain what I mean.

    I may have two objects. Each has an AttackOpponent function. Inside each has a Shoot function, which I want to "customise" so to speak, for each child object I make (say, one will shoot bullets, the other one rockets - so the shoot function for each will be unique, but yet they are both still considered a shoot function)

    AttackOpponent
    {
    //similar code for all children here
    shoot()
    }

    In other languages, I can put the AttackOpponent method in a base class, and have a Shoot() method which is overridable in some way. Then I can make the child classes, and extend this base class, and simply override the Shoot method, each implementing it in its own unique way. So then I can just generically call my AttackOpponent function for any child and know it will do what it should both the common code as well as the customised code as it should.

    Does that make any sense whatsoever?

    **************
    ooh, I think the key is user-defined events
  • well why didn't you construct your AttackOpponent so that you pass a argument to it that tells it whether to skip shoot() or not, and have the if statement that tests that argument?

    I'm not even sure whta you're talking about with user-defined events...
  • edited
    @Tuism Well I never want to skip the shooting. It's just for my two ships the shooting means something different. Although I can still take your method of passing an argument to tell it which type of shooting to do. I better look into this, because I don't actually know how this works in game maker yet. Thanks

    * And game maker has these user defined event things. If you go to an object, and add an event, one option is a user-defined event. So in my script I can call user-defined event 0, for instance, and then it will do the custom code for each object that uses the script
  • Well you can pass through and argument that is a number for the attack type? Or you can do what I am doing (which is largely hacky because of using a hex grid) and on the script pass it the object's "id" as an argument and check from that.

    e.g.
    Object Step:
    scr_shoot_bullet(id);

    Script "scr_shoot_bullet":
    with (argument0)
    {
      var var1,....; //keep unecessary variables temporary till out of scope just in case.
    
      if (id = collision_point(x,y,o_checking_for_type,true,false))
      {
        //if there is a collision at x,y with an object type, however
        //this does assume that objects of the same type wouldn't overlap
      }
    }


    Otherwise I would suggest each shooting type you just dedicate and argument to the choice, so argument0 would be an integer for what shooting type to do.
  • Cool thanks. I will look into this tonight. I might consider moving over to unity though if I decide to continue with this prototype. The only thing I like about game maker at the moment is how easy it is to add new sprites, object and sounds. Also the sprite editing is pretty cool for someone as useless at me. But it feels a little limited to me in some sense, and the hackiness of getting around things scares me a bit :) Thanks anyway, I will consider your solution
  • edited
    or create objects, shoot_bullet and shoot_rocket put the code in their user_defined0 event
    I think you also need to make sure that one of each object exists in the game, then
    pass the shoot_bullet or shoot_rocket to the script and in the script call event_perform_object(argument0,ev_other,ev_user0)

    edit:
    ok so as long as you reference the object id in the event_perform_object you don't need an instance of it in game
    using the user0 event also lets you code the bullet_obj and rocket_obj as normal but be able to pass them to the script to change the way stuff shoots
    Thanked by 1Denzil
  • I use GM's user defined events for this kind of method overriding all the time.

    I'll build a basic enemy class that has all the logic for cooldown and target detection and all that jazz, but when it fires, it calls event_perform(ev_other, ev_user0). Then in each different enemy type, I populate their user0 event with what I want their firing behavior to be like.

    It's a little annoying and sloppy sometimes, but I've used this for all sorts of behaviors before - everything from collision response to network-synced events.
    Thanked by 1Denzil
Sign In or Register to comment.