Movement and Attacks

Holy crap, the units move now! And attack each other!

Man oh man am I excited! I know my last post was titled "Okay, This Is Starting to Look Like a Game", but, ok you guys, this is really starting to look like a game!

I got the whole turn loop systematized and consolidated in the code so that all the button signals are connected, all the HUD elements update correctly, and and movement confirmation now ACTUALLY MOVES THE MECH ACROSS THE BOARD. And has it facing the correct way when it gets there! Holy crap this is exciting.

I owe it all to my little friend

private enum SelectionState {
    case idle
    case movement(MovementPreview?)
    case attack(target: UnitID?)
}

private var selectionState: SelectionState = .idle {
    didSet {
        updateForSelectionState()
    }
}

This handy little enum makes it easy to track which action state we're in, and then refresh everything, every time the property is updated. This allows for exhaustive switch statements based on the state, for clean and easy-to-maintain control flows.

Anyway, here's movement and attacking in action. No animations or visuals yet--you have to look at the inpector window to see the stats change--but I'm making rapid progress!

Trying out another editor

You may notice that the game looks different and probably a little snappier. When I'd run the battle scene, I had been noticing some serious lag between mouse clicks and things being updated. We're talking like a half second. I thought it was a little early in development, and with such a simple-looking game, to already be encountering performance issues, so gippity and I started doing various debug paths to try to track down the lag.

It was hard to find! Nothing in the Godot instruments told us anything. We engaged in heavy caveman debugging, peppering the code with print statemetns in various key states of the round-trip. Everything was sub-100 ms. The problem was that we couldn't get a timestap for the actual initial click, so none of our measurements were meaningful.

Thank goodness for Codex, because this next bit wasn't something I had any interest in learning how to do myself, hahaha. So, gippity suggested we measure outside of Godot to get the initial click. It wrote a small Swift/CoreGraphics monitor that polled macOS’s left-mouse-down counter every 2 ms. When the counter changed, it read the event’s age and subtracted that from the current Unix time to estimate when macOS first recorded it. That gave us the earliest software-visible click timestamp rather than the moment that the Godot engine registered it. That's when we discovered the issue: depending on the selected renderer, there was anywhere between 254 and 577 ms of added latency.

I had to know if anything in my project (my project structure, Swift code, or maybe even Swift-Godot itself) was causing it. It didn't seem likely, but I built a Godot project that had a single button in it and nothing else, and it recorded the same exact latency issue.

I had gippity write up all my findings and posted them on the Godot subreddit. This morning I woke up to a single comment, which was asking if I had tried Xogot. I hadn't heard of it. It turns out that it's a version of Godot built with a native MacOS UI. (I guess vanilla Godot is written using a cross-platform framework or something. 🤷‍♂️) Anyway, I hurriedly downloaded the beta and...

The latency was gone! And on top of that Xogot is super slick! In addition to the native UI, there are a whole raft of neat features that make it very inticing. It has some bugs and crashes a little bit, but my overall experience is already better. I dug around and discovered that Xogot is made by Miguel de Icaza, who is the dev responsible for creating the Swift-Godot extension that my project uses for its domain layer! As I was writing this, I even saw this page but haven't had time to dig into it yet.

The name is funny because it clearly is trying to make users of Xcode feel at home (and doing a FANTASTIC job at it, I must say) so: xcode + godot == xogot, I guess.

Oh and one other interest aspect: You can run your scenes/game directly inside the editor in Xogot! Very cool.

Anyway, that's all for today. See you soon!

← Back to writing