Keen:Code Example - Smart Shovel (Part 2): Difference between revisions

From Medieval Engineers Wiki
Jump to navigation Jump to search
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{SEO|image_url=http://www.medievalengineerswiki.com/images/e/e6/KEEN_COMPASS_ONLY.png|description=Hello engineers, this tutorial continues on the Smart Shovel (Part 1) tutorial}}
{{SEO|image_url=http://www.medievalengineerswiki.com/images/b/b7/KeenLogoBig.png|description=Hello engineers, this tutorial continues on the Smart Shovel (Part 1) tutorial}}
{{:Official Content/OCH}}
{{Keen:OCH}}




Line 14: Line 14:
Let’s start with adding the data to the definitions. Open MyObjectBuilder_SmartDiggerToolBehaviourDefinition.cs and add the following two variables to the definition:
Let’s start with adding the data to the definitions. Open MyObjectBuilder_SmartDiggerToolBehaviourDefinition.cs and add the following two variables to the definition:


<source lang="csharp" collapse="false">
<syntaxhighlight lang="csharp" collapse="false" line>
// Name of the control as specified in Controls.sbc
// Name of the control as specified in Controls.sbc
public string ControlName;
public string ControlName;
Line 20: Line 20:
// Format of the message displayed to the player.
// Format of the message displayed to the player.
public string AltitudeFormat;
public string AltitudeFormat;
</source>
</syntaxhighlight>




Line 26: Line 26:
Now that we updated the Object Builder to contain these two values, we can start to use it in the code. First, we have to load it into the definition. So open MySmartDiggerToolBehaviorDefinition.cs and add the following two variables:
Now that we updated the Object Builder to contain these two values, we can start to use it in the code. First, we have to load it into the definition. So open MySmartDiggerToolBehaviorDefinition.cs and add the following two variables:


<source lang="csharp" collapse="false">
<syntaxhighlight lang="csharp" collapse="false" line>
// A hashed value of the control name, this is better for performance.
// A hashed value of the control name, this is better for performance.
// Use string hashes for strings that are used as identifiers.
// Use string hashes for strings that are used as identifiers.
Line 34: Line 34:
// Use MyStringId for texts that are displayed to players.
// Use MyStringId for texts that are displayed to players.
public MyStringId AltitudeFormat { get; private set; }
public MyStringId AltitudeFormat { get; private set; }
</source>
</syntaxhighlight>




And make the Init function look like this:
And make the Init function look like this:


<source lang="csharp" collapse="false">
<syntaxhighlight lang="csharp" collapse="false" line>
         // Called when the game loads up and creates definitions out of all the SBC files.
         // Called when the game loads up and creates definitions out of all the SBC files.
         protected override void Init(MyObjectBuilder_DefinitionBase builder)
         protected override void Init(MyObjectBuilder_DefinitionBase builder)
Line 53: Line 53:
             AltitudeFormat = MyStringId.GetOrCompute(ob.AltitudeFormat);
             AltitudeFormat = MyStringId.GetOrCompute(ob.AltitudeFormat);
         }
         }
</source>
</syntaxhighlight>




Line 59: Line 59:
Now that the definition is updated, we’re half-way there! All that’s left is integrating these values into the code in MySmartDiggerToolBehavior.cs and adding the data to Shovel.sbc. Let’s begin by integrating it into the code. Open MySmartDiggerToolBehavior.cs and find the Init method. Then, replace the MyStringHash.GetOrCompute(“ToggleShovel”) with m_definition.ControlName.
Now that the definition is updated, we’re half-way there! All that’s left is integrating these values into the code in MySmartDiggerToolBehavior.cs and adding the data to Shovel.sbc. Let’s begin by integrating it into the code. Open MySmartDiggerToolBehavior.cs and find the Init method. Then, replace the MyStringHash.GetOrCompute(“ToggleShovel”) with m_definition.ControlName.


<source lang="csharp" collapse="false">
<syntaxhighlight lang="csharp" collapse="false" line>
// So this...
// So this...
m_inputContext.Actions.Add(MyStringHash.GetOrCompute("ToggleShovel"), new MyInputContext.ActionBinding
m_inputContext.Actions.Add(MyStringHash.GetOrCompute("ToggleShovel"), new MyInputContext.ActionBinding
Line 65: Line 65:
// becomes this...
// becomes this...
m_inputContext.Actions.Add(m_definition.ControlName, new MyInputContext.ActionBinding
m_inputContext.Actions.Add(m_definition.ControlName, new MyInputContext.ActionBinding
</source>
</syntaxhighlight>




Then, in ScanAltitude, replace the hardcoded “Altitude: {0:0.0}m” string with MyTexts.GetString(m_definition.AltitudeFormat):
Then, in ScanAltitude, replace the hardcoded “Altitude: {0:0.0}m” string with MyTexts.GetString(m_definition.AltitudeFormat):


<source lang="csharp" collapse="false">
<syntaxhighlight lang="csharp" collapse="false" line>
// So this...
// So this...
string message = string.Format("Altitude: {0:0.0}m", altitude);
string message = string.Format("Altitude: {0:0.0}m", altitude);
Line 76: Line 76:
// becomes this...
// becomes this...
string message = string.Format(MyTexts.GetString(m_definition.AltitudeFormat), altitude);
string message = string.Format(MyTexts.GetString(m_definition.AltitudeFormat), altitude);
</source>
</syntaxhighlight>




Line 82: Line 82:
All that’s left is to open up Shovel.sbc and add some fields! Which is what we’re going to be doing next. In the WoodenShovel and IronShovel behavior definitions, the ones we had to change the object builder and type for (lines 73/74 and lines 103/104) add two fields for each of them at the end of the definition:
All that’s left is to open up Shovel.sbc and add some fields! Which is what we’re going to be doing next. In the WoodenShovel and IronShovel behavior definitions, the ones we had to change the object builder and type for (lines 73/74 and lines 103/104) add two fields for each of them at the end of the definition:


<source lang="xml" collapse="false">
<syntaxhighlight lang="xml" collapse="false" line>
<ControlName>ToggleShovel</ControlName>
<ControlName>ToggleShovel</ControlName>
<AltitudeFormat>Altitude: {0:0.0}m</AltitudeFormat>
<AltitudeFormat>Altitude: {0:0.0}m</AltitudeFormat>
</source>
</syntaxhighlight>





Latest revision as of 20:08, 18 July 2022



Hello engineers,

This tutorial continues on the Smart Shovel (Part 1) tutorial, we expect you to have a fully functional version of the mod. If it’s not functioning right, please make sure you go back and make it work correctly first. After completing the first part, there are more things we can do to make the mod nicer. Let's put the message format and the input keybinding into the definition, instead of leaving it hard-coded.


Version: 0.5

New Definition Variables

Let’s start with adding the data to the definitions. Open MyObjectBuilder_SmartDiggerToolBehaviourDefinition.cs and add the following two variables to the definition:

		// Name of the control as specified in Controls.sbc
		public string ControlName;
		
		// Format of the message displayed to the player.
		public string AltitudeFormat;


Load The New Variables

Now that we updated the Object Builder to contain these two values, we can start to use it in the code. First, we have to load it into the definition. So open MySmartDiggerToolBehaviorDefinition.cs and add the following two variables:

		// A hashed value of the control name, this is better for performance.
		// Use string hashes for strings that are used as identifiers.
		public MyStringHash ControlName { get; private set; }
		
		// A string ID of the altitude string, this is better for performance.
		// Use MyStringId for texts that are displayed to players.
		public MyStringId AltitudeFormat { get; private set; }


And make the Init function look like this:

        // Called when the game loads up and creates definitions out of all the SBC files.
        protected override void Init(MyObjectBuilder_DefinitionBase builder)
        {
            base.Init(builder);

            var ob = (MyObjectBuilder_SmartDiggerToolBehaviorDefinition) builder;
			
            // Get a string hash of the control name.
            ControlName = MyStringHash.GetOrCompute(ob.ControlName);
			
            // Get a string id for the altitude format.
            AltitudeFormat = MyStringId.GetOrCompute(ob.AltitudeFormat);
        }


Use The New Variables

Now that the definition is updated, we’re half-way there! All that’s left is integrating these values into the code in MySmartDiggerToolBehavior.cs and adding the data to Shovel.sbc. Let’s begin by integrating it into the code. Open MySmartDiggerToolBehavior.cs and find the Init method. Then, replace the MyStringHash.GetOrCompute(“ToggleShovel”) with m_definition.ControlName.

// So this...
m_inputContext.Actions.Add(MyStringHash.GetOrCompute("ToggleShovel"), new MyInputContext.ActionBinding

// becomes this...
m_inputContext.Actions.Add(m_definition.ControlName, new MyInputContext.ActionBinding


Then, in ScanAltitude, replace the hardcoded “Altitude: {0:0.0}m” string with MyTexts.GetString(m_definition.AltitudeFormat):

// So this...
string message = string.Format("Altitude: {0:0.0}m", altitude);

// becomes this...
string message = string.Format(MyTexts.GetString(m_definition.AltitudeFormat), altitude);


Provide Some Data For The Variables

All that’s left is to open up Shovel.sbc and add some fields! Which is what we’re going to be doing next. In the WoodenShovel and IronShovel behavior definitions, the ones we had to change the object builder and type for (lines 73/74 and lines 103/104) add two fields for each of them at the end of the definition:

	<ControlName>ToggleShovel</ControlName>
	<AltitudeFormat>Altitude: {0:0.0}m</AltitudeFormat>


This is kind of tricky! Did you place it right? If you did, you can now launch the game, and everything should work! If you didn’t, you can look at the example files we attached to the guide for reference to compare where we put it. By setting it up like this, it makes it easily possible to adjust and tweak settings on your mod without having to go into the code or rely on hard-coded values.

Example And Sources

We are going to end the tutorial here. You can find the result on the Steam Workshop

http://steamcommunity.com/sharedfiles/filedetails/?id=936042947


We’ve also attached a copy of the mod’s source code

http://www.medievalengineerswiki.com/uploaded_files/SmartShovel_PartTwo.zip


For a fun exercise in understanding, try to add another parameter to the Altitude message like the Game’s elapsed time. (You can get it from MyAPIGateway.Session.ElapsedPlayTime)

Good luck, have fun modding it!