Dynamic Dispatch
Lesson 28 of 48 in Coddy's GDScript for C# Developers course.
C# interfaces give named contracts checked by the compiler. GDScript can dispatch dynamically with Object.call, while typed script references express inheritance relationships; a C# interface declaration cannot simply be pasted into GDScript. A dynamic call still requires an actual compatible method. Use has_method when optional capability needs checking, or document and enforce the required contract. Consumers must use the supplied collaborator rather than secretly constructing their preferred implementation. The example asks a bell to speak.
Complete C# comparison. Compile all listed C# files together; Program.Main is the entry point.
C# file ISpeaker.cs:
public interface ISpeaker { string Speak(); }C# file DemoBell.cs:
public class DemoBell : ISpeaker { public string Speak(){return "ding";} }C# file Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
public static class Program {
public static void Main() {
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
ISpeaker bell=new DemoBell();Console.WriteLine(bell.Speak());
}
}C# output:
dingGDScript example file demo_bell.gd:
class_name DemoBell
extends RefCounted
func speak() -> String:
return "ding"GDScript example file main.gd:
extends Node
func _ready() -> void:
var bell: DemoBell = DemoBell.new()
print(bell.call("speak"))GDScript output:
dingChallenge
EasyFeeReader.total(policy:RefCounted,units:int)->int must invoke the supplied policy cost method. FlatPolicy.cost returns 5 regardless of units. Solve uses FlatPolicy. The driver also supplies a different compatible policy.
Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.
Your code goes in fee_reader.gd, flat_policy.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.
Try it yourself
extends Node
class ProbePolicy extends RefCounted:
func cost(units: int) -> int:
return units * 4 + 1
func contract_ok(data: Array) -> bool:
if FlatPolicy.new().cost(9) != 5 or FeeReader.total(ProbePolicy.new(),4) != 17: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in GDScript for C# Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command Count3Collection Ownership
Array OperationsTyped ArraysShared and Copied ArraysDictionary PresenceDictionary TraversalRecap: Word Frequencies6Object Relationships
Inheritance and SuperDynamic DispatchComposed ObjectsRecap: Tiered FeesSignals and PayloadsPractice on your own: Online GDScript compiler