stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 283

Day 283 of 365 · Expert · Month 10: Bits and data

Hashes at run time

Every device reveals its PrefabHash and NameHash: recognising devices on pins at run time, comparing names and building a chip that adapts to its wiring.

Lesson 18 min Week 41

What you learn today

  • Read PrefabHash and NameHash
  • Recognise devices
  • Compare hashes

So far your code always knew which device hangs where – you connected it yourself. A chip that other players take from the library does not know that. But it can ask: every logic-capable device reports what it is and what it is called.

Three identifiers

LogicTypereturns
PrefabHashthe hash of the prefab name, i.e. the device type
NameHashthe hash of the name you gave it with the labeller
ReferenceIdthe unique number of this one device

PrefabHash is the same number that HASH("StructureGasSensor") returns. NameHash the same as HASH("Raum"). So you simply compare with a define:

l kind d0 PrefabHash
seq tmp kind SENSOR          # 1 = a gas sensor is on d0

Recognising devices on pins

With the indirect pin dr0 (day 102) a loop walks through all six pins. bdns skips empty pins before l raises an error:

move pin 0
loop:
bdns dr0 next             # empty pin: skip
l kind dr0 PrefabHash
next:
add pin pin 1
blt pin PINS loop

That way the chip counts how many sensors and batteries it has, and finds the device with a certain name – no matter which pin it sits on.

What it is good for

  • Self-check: a chip checks at start whether there really is a gas sensor on d0, and reports an error otherwise instead of controlling nonsense.
  • Universal display: depending on the device type the chip reads Temperature or Ratio.
  • Names instead of pins: the chip looks for “Haupt”, whether somebody put the battery on d1 or d3.
Warning

An empty pin is not a device. l kind dr0 PrefabHash on an empty pin stops the chip with an error. First bdns, then read.

Tip

On the network you need this less often, there lbn already searches by type and name. But NameHash helps there too: a chip can check that way whether a device carries the name it expects.

Diagram

Reading with l, writing with s Gas sensor (d0) Temperature 293.15 IC10 r0 = 293.15 Wall light (d1) On 1 l r0 d0 Temperature device → register s d1 On 1 value → device
Through dr0 the loop reads pin by pin, PrefabHash tells the type, NameHash the name.

Exercise

Task

Gas sensors, batteries or nothing hang on pins d0–d5. Write a loop over all pins:

  • display “Sensoren”: number of gas sensors
  • display “Akkus”: number of station batteries
  • display “Haupt”: pin number of the device named “Haupt”, −1 if it is missing

The pin number is in pin (r0) so that dr0 works.

Hints

Hint 1

bdns dr0 next skips empty pins.

Hint 2

l kind dr0 PrefabHash, then seq with SENSOR and BATT and add up.

Hint 3

Name: l tmp dr0 NameHash, seq tmp tmp MAIN, select main tmp pin main.

Quiz

3 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere