Swift Substring Slicing

I was toying around with the ‘Natural Language’ Framework for iOS when I got a warning about a deprecated line.

https://developer.apple.com/documentation/foundation/nslinguistictagger/tokenizing_natural_language_text

fromString.substring(with: tokenRange)

Code screenshot showing warning

substring(with:) is deprecated: Please use String slicing subscript.

This produced the warning: substring(with:) is deprecated: Please use String slicing subscript.

I looked up the new docs on String slicing and it looked as though I could just put the range in brackets and get what I wanted.

https://developer.apple.com/documentation/swift/substring

I thought, hey this is easy.

fromString[tokenRange]

But that produced a hard error: Subscript ‘subscript(_:)’ requires the types ‘String.Index’ and ‘Int’ be equivalent.

I broke out the range into its lower and upper bounds. It gave the same error.

tokenRange.lowerBound..<tokenRange.upperBound]

I printed out the type and it showed Subscript. Of course, it was the wrong type. These substring methods are very interesting and are basically just a reference to the original String. There is even a warning on the Apple docs for Substring.

Important
Don’t store substrings longer than you need them to perform a specific operation. A substring holds a reference to the entire storage of the string it comes from, not just to the portion it presents, even when there is no other reference to the original string. Storing substrings may, therefore, prolong the lifetime of string data that is no longer otherwise accessible, which can appear to be memory leakage.

So I actually just needed to cast it into a String from that Substring:

String(fromString[tokenRange])

So, for my purposes:

wholePoem.substring(with: tokenRange)
Just needed to be rewritten to this:
String(wholePoem[tokenRange])

Here is a playground showing this.

Data Model Speed Run

This is my first foray into making videos of live-programming. I am learning more and more about OBS and close-captioning every day so I know that as I continue to develop videos they’ll improve immensely.

This is a speed-run of me creating a Data model for a board that I found to be really interesting. Sadly I do not get together with my gaming group so I’ve not played it yet. I hope this gives a good idea about how to manage data and state for a game.

Check out this game on Board Game Geek: Nidavellir
https://boardgamegeek.com/boardgame/293014/nidavellir

Gaming for All

I am trying to find the nytimes link for this article. Until I find it. This is the text as transcribed by me. I thought it was great to see accessibility for games covered in the nytimes. Please help me find the digital link!

Gaming For All by Melissa Hart Dec 27, 2020 NYTimes

Flashing lights. Small text. No subtitles. These are a few of the features that can make video games really hard for some kids to play. That’s why the gamer Courtney Craven started Can I Play That?, a website that reviews video games for people with disabilities and teaches developers how to create more accessible Games.

Craven, who is nonbinary and uses they/them pronouns, lost their hearing in both ears several years ago, which made it harder to play video games that relied on sounds as cues. It also made the games less fun to play: “If controls aren’t accessible, if subtitles are poorly done,” Craven says, “disabled kids won’t get to experience any of the benefits.”

                On Can I play That?, video games are reviewed by gamers with disabilities who give scores between 1 and 10, based on how easy and fun the games are to play. Craven started the website two years ago, and there are hundreds of game reviews, including for Fortnite (10/10 for deaf accessibility) and Animal Crossing (6.4/10 for blind and low vision accessibility). Here are several audiences Craven and their team consider when reviewing a game, plus recommendations from the website.

1. Deaf or Hard-of-Hearing

Players with hearing loss need captions at the bottom of the screen in plain black type, so they can read what characters are saying. A good game will have descriptions of every sound, from an enemy rustling in the grass to footsteps racing down an alley. If a game’s dramatic moments – like explosions – are paired with a controller’s vibration, that’s even better. 

Recommended: “Immortals Fenyx Rising” (“Will have Deaf and hard-of-hearing players living their best Greek hero lives.”)

2. Blind or Visually Impaired

Lighting is really important for players who are visually impaired (this means their eyesight can’t be fixed with glasses). The best games let players turn the brightness up fo they can tell different shapes apart; large text and bold outlines around objects can help, too. Blind players need an option to add a screen reader, which lets them follow along in Braille.

Recommended: “Timecrest” (“The most accessible interactive-fiction game to date.”)

3. Motor or Physical Disabilities

Not everyone can grip or easily use a game controller. Players with motor challenges have a hard time using their hands or fingers, so they need games that provide alternate controls like using a mouse or a keyboard. If a player can’t hold down a button to allow a character to run, they should be able to do the same thing by pressing multiple keys instead.

Recommended: “Fall Guys: Ultimate Knockout” (“Unlike traditional battle royal, there are no ridiculous kill ceilings.”)

4. Cognitive Disabilities

Players with cognitive challenges like autism and dyslexia process words and sounds differently. They need games that let them control everything they hear and see onscreen, from music and sound effects to what the characters are saying. Players also need easy-to-read text and the ability to turn off flashing images, which can cause migraines or even seizures.

Recommended: “Moving Out” (“You really can play the game however you need to and not be shamed or penalized.”)

SpriteKit Starting Project

SpriteKit hasn’t gotten much love from Apple after it was created, but it was pretty complete already. I’m not a fan of the sks files or storyboards so I’ve created a starting project where you can just start coding on a 2d game.

https://github.com/mevdev/SimpleSpriteKit

Features:

  • No sample code
  • Programmatic invocation of starting view controller, which eliminates the need for a storyboard.
  • GameScene is programmatically invoked, which eliminated the (useless to me) sks file

Game Data Design

This was a series of tweets.
https://twitter.com/mevdev/status/1318779823951507456

Game Data Design is something I learned best by doing. I’ve released 4 games on the app store (only one is still available). I’ve gotten to a playable game with 4 others. I am by no means an expert but I feel like I’ve learned a lot and now know how it works best for me.

Have a model that holds your data and state. Make it separated from your UI so it changes its own state. Build for serialization (save/open). Write and rewrite in an evolutionary manner. Make the UI an engine that reacts to and drives your model’s data and state.

A good model will ease all scenarios making bugs easier to fix but also the ability to save/resume a game in any state. Practice good naming. Make variable names verbose if that describes them well. Do not reuse terms for unrelated items. Be a bit silly at times.

I start by roughing out a model and to understand state. Understanding data transformations and calculations will suss out player objects vs the model. What holds what, which direction should communication flow. How is a turn kept track of.

This is my current game’s state enumeration:

enum State { 
     case startingGame, 
     rolling, 
     rolledAwaitingSelection, 
     selectedRoll, 
     awaitingStopOrContinue,
     awaitingLocalPlayerRoll,
     busted,
     win, // network game only 
     waitingOnNetworkPlayers 
}

// My main model variables:

struct GameModel { 
     var players: [Player]
     var state: State 
     //MARK: Potential and Selection Calculated vars 
     var currentSelections: [DiePairing] 
     var gameBoardPotentialKeys: [Int] 
     var availableNumbers: [Int]

Model manipulation and UI interaction

     func isAvailableNumber(_ num: Int) -> Bool 
// MARK: - Mutation for States mutating 
     func rolling() 
     mutating func selectedRoll(_ pairing: [Int]) 
     mutating func stopSelected() 
     mutating func currentPlayerRoll()

// Player turn management
// MARK: Turn Management

     func nextPlayerIndex() -> Int 
     func nextPlayer() -> Player 
     mutating func continueToNextPlayer() 
     mutating func continueSelected() -> Bool 
     mutating func didSomeoneJustWin() -> Player?

// MARK: State Observation

     func isWinningState() -> Bool 
     func isBusted() -> Bool 
}

I know when the model is changed & how it changes state. It holds an array of Players which encapsulate their own data (If I were on a team more would be private).

// The Player manages its board, potential board & win state. I allow the UI to introspect in to draw.

struct Player { 
     var gameBoard: [Int: Int] 
     var gameBoardPotential: [Int: Int] 
     var hasWon: Bool 
     func winningRowsCount() -> Int 
     func winningRows() -> [Int]

// Then this last bit changes the state of Player

     mutating func addPotentialDie(_ die: Die) { 
     mutating func convertPotential() 
     mutating func convertPotentialToGameBoard() 
}

Maybe this isn’t the best way but it currently works for me.

Thanks for playing!

I realized after writing that ‘func convertPotential()’ was an exact duplicate and not called. It was more elegant though so I tested it then used that. I should be making a TestFlight build next week for testing. Also all of my structs and enums conform to Codable btw.

Sprite Tour iOS

I’ve updated this somewhat dated Apple example app that really doesn’t seem to be that available anymore (which makes sense since it is from 2013, https://developer.apple.com/download/more/?name=WWDC%202013).

It now compiles on Xcode 11. I will probably make a Swift version of this sooner or later.

https://github.com/mevdev/SpriteTouriOS

  • Each scene demonstrates one major concept. You can learn one technique from each sample, then decide which techniques are useful and how to combine them together.
  • APLBasicSprites Shows how to create a default textured or untextured sprite.
  • APLColorizedSprites Shows how to apply a colorization effect to a textured sprite.
  • ColorizedAnimatingSprite Shows animating colorization effect. Originally part of APLColorizedSprites on mac os version.
  • APLResizingSprites Shows how a sprite can be resized, with different scaling effects applied to the texture.
  • APLSpriteAnchors Shows how a sprite’s anchor position can be changed so that the texture image is not centered on the sprite’s position.
  • APLSpriteBlending Shows how an additive blending mode can be used to simulate lights.
  • APLAnimatedSprites Shows how a sprite’s texture image can be animated.

Is Your Mission Real? Community and Communication for Business

https://medium.com/@mevdev/is-your-mission-real-community-and-communication-for-business-3f37daaefe4c

Good communication is key to any successful business. It can inspire employees, make them your best evangelists and it can make others want to work with you, however bad communication can result in silos, us-vs-them mentality and even harassment claims. There are many facets to business communication whether internal or external. I will focus on internal communication for and with satellite offices and single remote workers. With global presence comes global communication.

A blurred image of a computer desktop shows the author's name in a slack chat with the title of the article.



What is really happening inside an organization can help employees feel more comfortable and give them confidence for longevity and personal growth. …Read more on Medium…

All aboard: Designing an onboarding experience

https://medium.com/@mevdev/all-aboard-designing-an-onboarding-experience-5d8aab50fb8e

Joining a new group is difficult, whether it is a company, a benevolent group or any team. You are the new person, and there are names to learn, people to meet, on-going politics, specific policies and your own role you need to learn. I’ve found that there are various ways to ease this process. I will focus on onboarding in the tech industry because that is what I know most about.

I got these headphones from Metal Toad as an onboarding gift to combat the open office.

All companies have a culture, whether they like it or not. It can sometimes take days or months for it to be established. I believe…Read more on Medium…