• Nothing
  • About
Menu

Asher Einhorn

  • Nothing
  • About
messenger.png

Message sending vs function calling

June 18, 2015

The following is part of an ongoing series of basic tutorials for gameplay scripters.

Programming languages and scripting systems alike, use one, or sometimes both of two paradigms. These are

  1. method or function calling based systems.
  2. message sending based systems.

So what do the two mean? It's actually very simple, but important to know nonetheless.

In a method or function calling system, you have to have to know that an object contains a function in order to use it.

Say you've programmed a dog and that dog has a method that causes it to bark when called. In a calling system you would access the dog, and then call its bark method.

In a message based system you send a message to an object that asks if it knows how to bark, and if it does, then it would execute that code.

The benefit of a message based system is that it allows you to to treat many different objects the same. If two different classes both contain the bark function, even if they're completely different they will respond to that same message.

This is of course possible with function calling through inheritance. If both objects inherit from a base class that defines the bark function, then you can again call the same method on both, you just have to be sure the method exists or you will have an error on your hands.

This is called polymorphism.

There is another advantage to message sending, which is that in many systems you don't actually have to even have a reference to an object you’re trying to send a message to. You can often issue a message globally, and any objects listening for it will pick it up and execute code if they have a corresponding message handler.

So to summarise: Message sending is often more flexible from the offset, but function calling can be slightly more efficient. Realistically these differences are negligible, it's just important to understand the concepts as you might come up against systems that use either one of these in any games you work on.

UE4 for example, implements both in its Blueprints system.

In Scripting Handbook
← Event driven systems vs PollingScripting Handbook: Vectors →
Twitter
  • RT @CKapmeh: Sunday Capilla San Bernardo / Nicolás Campodonico https://t.co/V56q7uftPf
    May 28, 2017, 10:42 AM
  • Crunch dadaism. Come to @Naughty_Dog and experience the highs of creative culture. https://t.co/9yMPcTdiYA
    May 27, 2017, 9:33 AM
  • Crunch garden #gamedev Avocado, Lemon and Peppers. From right to left. https://t.co/jtZXeWDEgh
    May 27, 2017, 9:31 AM
  • Working with a migraine two days in a row is quite possibly the most unpleasant thing in recent memory.
    May 26, 2017, 3:36 PM
  • Sure is fun to have a night off! #PS4share https://t.co/WMcEMY5yWE
    May 21, 2017, 7:59 PM
  • The EU/US PSN account conflict is an ongoing nightmare. Looks like I'm not playing anything tonight after all.... pain!
    May 21, 2017, 7:49 PM