7. Read workshop state

Now turn from practice numbers to your own workshop. Read how much ore is available. Reading stock alone does not start mining or crafting.

Read a long name in parts

Workshop.Resources[ResourceKind.Ore].Available gives the amount of ore you can use now.

PartMeaning
WorkshopYour workshop
.ResourcesIts resource list
[ResourceKind.Ore]Ore in that list
.AvailableThe amount not reserved for other jobs

A dot selects a name inside an object. Square brackets select an item from a list. ResourceKind.Ore is the game's name for ore, so copy it as written.

The rules a game provides for reading state and requesting actions are its API. You can look up names in the API reference when you need them.

Use the amount in a condition

ArcaneScript
public class Program : ArcaneProgram
{
    public void OnStart()
    {
        var ore = Workshop.Resources[ResourceKind.Ore].Available;
        if (ore >= 10)
        {
            Workshop.Log.Write("Enough ore.");
        }
        else
        {
            Workshop.Log.Write("Need more ore.");
        }
    }
}

var ore = ...; creates a variable whose type comes from the value on the right. Here the type is Amount, which the game uses for resource quantities. You can compare these quantities as you compared int values in the previous chapter.

The autonomous spell displays Enough ore. when at least 10 ore is available, or Need more ore. otherwise. Reading stock does not consume it.

ore stores the amount at the moment you read it. It does not update automatically when stock changes. Read Available again when you need the latest amount.

Total and Available

Total includes input materials reserved for jobs. Available excludes those reservations. If you own 20 ore and 10 is reserved for crafting, Total is 20 and Available is 10. Neither includes unfinished output.

Use Available to check whether you have enough materials for a job.

Exercise

If ore Total is 20 and Available is 8, which message does the example display?

Show example solutionHide example solution

It displays Need more ore. The spell reads Available, so the condition is 8 >= 10, which is false.