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

From Medieval Engineers Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{:Official Content/OCH}}
Hello engineers,
Hello engineers,



Revision as of 14:57, 30 May 2018

Official Content/OCH


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!