Guide

Swift

Write Wii app logic in Swift. It's translated to C and built with devkitPro.

How it works

devkitPro's compiler can't build Swift for the Wii's PowerPC processor, so Revolution Editor translates a subset of Swift to C each time you build, then devkitPPC compiles that. You write real Swift syntax; the translator checks types and reports errors in the usual file:line:column: error: form in the Console.

This is a deliberately small language. It covers the logic of menus, tools and simple games, not the whole Swift language or its standard library.

Start a Swift app

  1. Choose File > New > Wii Homebrew > Project… and pick Swift: UI app (designer + Swift).
  2. Design the screen in ui/main.wiiui with the UI designer.
  3. Write the logic in swift/main.swift. Every .swift file in the swift/ folder is translated together.
  4. Press Build & Run.

The Swift code is edited in the built-in code editor, with syntax highlighting.

Example

var count = 0

func appOnButton(_ id: Int) {
    if id == UI.go {
        count += 1
        setText(UI.status, "Pressed \(count)")
    }
}

UI.go is the widget named go in your design. The editor reports a clear error if no such widget exists.

Entry points

Define any of these and the app calls them:

  • func appOnStart(): once, at launch.
  • func appOnFrame(): every frame.
  • func appOnButton(_ id: Int): when a button widget is clicked.
  • func appOnWpad(_ pressed: UInt32): when Wii Remote buttons are pressed. Test them with Button.a, .b, .one, .two, .plus, .minus, .up, .down, .left, .right, for example if pressed & Button.a != 0.
A hook can come from Swift or the node graph, not both. If both handle the same one, the build stops and says so.

What works

  • Types: Int, UInt32, UInt8, Double, Float, Bool, String, your own struct and enum, and fixed-size arrays.
  • Statements: let and var, functions (with argument labels), if/else, while, repeat-while, for over ranges (0..<n, 0...n) and arrays, switch (must be exhaustive), break, continue, return.
  • Expressions: arithmetic, comparison, logic and bit operators, the ternary ?:, and string interpolation like "Score \(score)". Strings join with + and compare with ==.
  • Structs have stored properties (with defaults), methods, mutating methods and the usual memberwise initialiser, Point(x: 1, y: 2). Enums are plain cases.
  • Arrays are fixed size: [Int](repeating: 0, count: 8) or [1, 2, 3], with .count and subscripting. An out-of-range index stops the app instead of corrupting memory.
  • Numbers don't convert on their own, as in Swift: write Double(n) or Int(x).

Built-in functions

  • setText(_ widget: Int, _ text: String), getText(_ widget: Int) -> String
  • setVisible(_ widget: Int, _ visible: Bool), setColor(_ widget: Int, _ r: Int, _ g: Int, _ b: Int) (0 to 255; the text colour for labels, the fill for everything else)
  • frameCount() -> Int, randomInt(_ lo: Int, _ hi: Int) -> Int, exitApp()
  • abs, min, max (Int or Double), sqrt, sin, cos, tan, floor, ceil, pow

What doesn't

These produce a clear error saying so: optionals, closures, classes, protocols, generics, dictionaries, tuples, guard, try/throw, overloaded functions, custom initialisers, and the Swift standard library and Foundation. Arrays can't be passed to functions or copied (keep them in a global), and strings are limited to 127 characters.

For anything else, write a C or C++ file in source/. It lives happily alongside the Swift.