Scrap Mechanic

Scrap Mechanic

Not enough ratings
Using String Logic In Impact
By AK006
Describes how you can use String Logic which is shipped with Impact.
   
Award
Favorite
Favorited
Unfavorite
Introduction


Table Of Contents
  • Overview
  • The Different Logic Types
  • About Strings And Characters
  • Using String Inputs
  • Using String Gates
  • Using String Outputs
  • Examples

About This Guide
As shown above, this guide should help you navigate safely through the wild waters of String Logic. Therefore this guide has been specialized on the Impact Mod by AK006, other implementations can differ.

Additionally, this guide was made for the Impact version of February 5th, so there might be changes when a new update is released.

Please excuse any spelling mistakes, grammatical errors or something in that sense as I'm not an English native speaker. Correct me if I'm wrong.

Prerequisites
To get a better understanding of the following contents it is usefull that you are familiar with a few things:
  • Digital and Number Logic in Scrap Mechanic
  • Strings (especially in Lua)
  • Standard String Manipulation Functions (Concatenate, Repeat...)

Lets Get Started
If you are fimiliar with strings in Lua, you can skip the section "About Strings and Characters". If not I'll see you there in a minute.

The Different Logic Types
Currently there are three logic types available for Scrap Mechanic:
  • Digital Logic (the default logic)
  • Number Logic (shipped with The Modpack)
  • String Logic (shipped with Impact)

The Base Principle
All logic types in Scrap Mechanic are based on the same concept: there are inputs, gates and outputs.

In any of these logic types the corresponding logic parts can be chained, that means they can be put in series like in electronics.

Normally a chain looks something like: input, gate [gate, gate...], output.

For starting to understand String Logic, it is crucial that you understand the base principle of logic. For every logic it stays the same.

Thats why it is easy to create a table that compares these logic types:
Logic Type
Conditions
Input Examples
Gate Examples
Output Examples
Digital
(0 or 1) / (false or true) / (off or on)
Button, Switch...
Logic Gate
Light, Horn...
Number
Numbers (0, 1, 2, 3...)
Key Pad, Counter, Logic Configurator...
Math Block...
Number Screen, Number Block...
String
Characters (a, b, c, 1, 2, 3...)
String Input
String Gate
String Output

String Logic
String Logic is a much more complex logic than digital or number logic. It is meant to be a fast interface between logic and the user. At its core it is faster than both of the logic types in similar cases in performance and creation time.

Unlike digital or number logic it is not natively integrated (yet?) in Scrap Mechanic. That means that mods that support a similar feature do not have to be compatible with each other.

If you are a mod maker and want to implement Impacts String Logic system you have the permission to do so. In the future there will be a section how do this, but for now you have to find it out on your own.
About Strings And Characters
What Is A String?
Lets start simple: lets just say a string is just something you would normally call a text. This page you are reading can be a string, the next line you read can be a string, the word you last read can be a string, the number 300426 could also be packed into a string.

But unfornately we have to go deeper, because computers don't think the way we do. They like numbers, collections of bits to be precise, not plain text. And, as we want to use text on a computer, we somehow have to pack the text we want into numbers.

So lets think how we can approach this issue.

From Text To Numbers
As we already discussed, we have to convert text to a collection of numbers.

Therefore we have to think of the principles of text: each text is made out of letters. So one text is just a collection of letters. You may not recognized it, but we already solved a part of our issue: converting text to a collection of something.

What Are Characters?
But before we progress further I have to tell you that from now on we don't refer to text components as letters. Because 1, 2, 3 and so on can be contained in strings we can't say that a string is a collection of letters: it is a collection of characters.

For the last step we just have to map these text components (characters) to numbers.

That can be done really easily, because there are standards like ASCII or UTF-8 that do this for us.

About Indices
Now that we have a collection of characters defined as our term "string", we can give each element of the collection an adress or, to make it clear: an index.

In Lua, respectively Impact, the first character in the string has the index 1 and the last one has the length of the string as the index.

So for example when you index the string "Hello World" with 5, the character at that index would be 'o'.

String Literals
You may seen it above or elsewhere: when we are defining strings we usually put the quotation mark in front and at the end. This goes back to defining strings in programming languages.

For now you just have to know that whenever you see a quotation mark in String Logic, it usually indicates that we are using a string value.

Another important literal is the character literal ('). Whenever you see this symbol in front of a single text component, the following text is a character value.

Nil - A Special Value
There is one exception you have to know when using strings: they can be nil.
Nil is a special value in Lua that indicates that a variable has not been set. It is the same as "null" in other high level programming languages.

A nil value can occur in String Logic when there are errors. For example when a String Gate has too little inputs, it outputs nil.

Normally, this should not lead to any problems, but try to avoid this value.

You Are Ready To Progress Further
That's all you need to know for now, you can now go to the next section.
Using String Inputs
Great, you made it here, let's start with the actual logic.

The Item
In Impact a String Input has the following icon:


As the name suggests it is used to input strings. Therefore it can only be connected to inputs, not to outputs.

What It Does
String Inputs are used to define a string value via the GUI. After inserting a string value via the GUI, String Inputs then output this value to connected String Logic inputs (for example String Gates or String Outputs).

That's it
You now know all there is about String Inputs. Let's keep going.
Using String Gates
Motivation
I know String Gates are hard to understand at first, even for experienced logic creators. But once you got into String Gates, you quickly recognize how much they can or could do. So don't give up and read the following text carefully.

The Item
String Gates in Impact have the following icon:


They can be connected to every type of logic that's currently in the game. They support unlimited inputs as well as unlimited outputs.

At the top, a String Gate shows the functions name it is set to. The default is: Concatenate.

The Basics Of String Gates

String Gates output values according to their inputs and their function they are set to. Normally, they are in the middle of a String Logic chain.

String Gates are similar to regular Logic Gates. They have an output delay of 1 tick and can be connected and chained to create complex setups. They also can be colored. This sometimes is a relevant feature like you know it from Number Logic from The Modpack.

The GUI

Category
String Gate functions are grouped by their inputs. A group of functions is called a category. Currently String Gates support three categories.

Category
Description
String Logic
This category includes functions which are mainly used with string inputs. These functions don't need to be connected to digital or number logic.
Number Logic
This category includes functions which require the String Gate to be connected to a number logic signal. Functions in this category need to be connected to number logic and output an according value.
Digital Logic
This category includes functions which require the String Gate to be connected to a digital logic signal (standard Scrap Mechnic Logic).



You can select a category by using the dropdown. Once you the select a new category the functions are refiltered. By default all categories are selected.

Functions


Under the category dropdown you can find a grid that consists of 4 function names. These functions are ordered alphabetically and filtered by the category above.

You can click any of these functions to show a description what it does.

By clicking these functions and closing the GUI the selected function is automatically set to the String Gates function.

Pages
As discussed earlier, there are 4 function names shown. This collection of 4 functions is called a page. You can switch pages by clicking the buttons below the function names.



Output


This section just describes the function, nothing to mention here.

String Gate Functions

Concatenate
This function combines all input strings in a specific order. It can be thought as adding strings like numbers. But unlike adding numbers the order in which they are added is crucial.

This function combines all input strings descendingly ordered by their red channel of the color of their String Outputter (String Input, String Gate...).

If you don't know what that means, here is an explanation. Colors in Scrap Mechanic are defined by 4 channels R(ed), G(reen), B(lue) and A(lpha). This is known as RGBA. Each of these channels can have a value from 0 to 255. The alpha channel is just transparency, a value of 255 means the color is opaque, a value of 0 means the color is fully transparent. To quickly define those channels, we use the hexadecimal number system (search online to get an explanation of that) and for convenience we ignore the alpha channel and say it is 255 by default.
Considering the paragraph above, a color of (R = 255, G = 255, B = 255) or for short #FFFFFF would be white. You can use a color picker (online) to experiment with that or you can find the colors of the Paint Tool here[scrapmechanic.fandom.com].

It is recommended to use black, gray or white at first to keep it simple.

Repeat
This function repeats the connected String Logic signal by the value of the connected number logic signal and outputs the repeated string. This functions can be thought of as multiplying a number.

Length
This function outputs the length of the input string as number logic.

Other Functions
I've not listed other functions here, because all of them are described in game. If you however want to get a better explanation, request the function (s) or category in the comments.

Moving on
You almost made it, just the next section.
Using String Outputs
The Item
String Outputs in Impact have the following icon:


They can be connected to every type of logic that's currently in the game.

Using String Outputs
String Outputs are the end points of a String Logic chain. They write the value to the chat or to the HUD when they are triggered by a digital logic signal (standard Scrap Mechanic logic).

You can select the output mode in the String Output GUI.

You can connect a string outputter (String Input, String Gate...) in any color except for the white of the Paint Tool to the String Output to print the value.

If the connected string outputter (String Input, String Gate...) has the color white, the String Output thinks the string outputter stores a name of a player. The player with that name then receives no messages from the String Output. There can be more than one ignored players.

If the String Output is in HUD (Heads Up Display) mode, you can also connect a number signal. This number signal then indicates the time to display the text on the HUD in seconds.

Formatting Output
As Scrap Mechanic uses ImGUI for GUIs, String Outputs accept the text format supported by ImGUI.

That means you can color your text like the following:
"#FF0000Red #00FF00Green #0000FFBlue #FFFFFFWhite" will result in a colored output according to the color names.

For custom colors I recommend using an color picker (just type "color picker" into your browser).

You've Made It!
Congratulations, you are ready to get your own String Logic going.

Please give feedback of this guide, to help me improve it.
Examples
Example 1
Build the following creation:

Connect them like connected here:

Activate the switch and see what happens.

Example 2
This example is available on the Steam Workshop.
6 Comments
ngandrud 6 Dec, 2024 @ 12:10pm 
just wondering if string logic can read chat and output a number from the mod pack
pie 7 Feb, 2023 @ 8:55pm 
thanks and goodluck
AK006  [author] 7 Feb, 2023 @ 10:42am 
I'm sorry, I'll try my best to make it clearer. There's just so much to cover. I'll try to make more illustrations and simplify a little bit.
But anyways: Thank you for your feedback and for the award, helps a lot.
pie 7 Feb, 2023 @ 9:20am 
heres an award, hope you make this more understandable for me and others, or im the only one that is such a dum dum
pie 7 Feb, 2023 @ 9:17am 
but god damn, after reading the entire thing i didnt understand anything cuz of how complicated it is but atleast its something
pie 7 Feb, 2023 @ 9:00am 
thanks mate