S&box Wiki

Revision Difference

Addons/GettingStarted#551432

<cat>Code.Misc</cat> <title>Creating a runtime addon</title> # Creating an addon project Start s&box in "Content Mode". <upload src="b4bc4/8db9a1f9d9d8b43.png" size="12387" name="contentmode.png" /> Once in game, select "New Project..." under "File" on the menu bar. <upload src="b4bc4/8db9a20127ec8df.png" size="9083" name="newproject.png" /> Select the "Addon" project type and give it a name. <upload src="b4bc4/8db9a20822c03f6.png" size="16584" name="image.png" /> # Coding your runtime addon ⤶ Once created, right-click your addon under the "Addon" list in the "Projects" dock Once created, right-click your addon under the "Addon" list in the "Projects" dock and select "Open in Visual Studio". See [Setting up Visual Studio](https://wiki.facepunch.com/sbox/Setting_up_Visual_Studio) for a guide on setting up VS. <upload src="b4bc4/8db9a20f111a4a5.png" size="14081" name="image.png" /> Right click your project and create a new class with Add->New Item... (or press CTRL+SHIFT+A), and then give your new class a name. Right click your project and create a new class with Add->New Item... (or press **CTRL+SHIFT+A**), and then give your new class a name. <upload src="b4bc4/8db9a21646b8a36.png" size="23789" name="image.png" /> The file you just created is where you'll be creating the "Primary Type" class for your runtime addon. In the case of the Sandbox gamemode, this is the entity that users of your addon will spawn when they select it in the entities list. You'll probably want to make your class inherit from some kind of [Entity](https://asset.party/api/Sandbox.Entity). You'll most likely use [ModelEntity](https://asset.party/api/Sandbox.ModelEntity). In the case of the Sandbox gamemode, this is the entity that users of your addon will spawn when they select it in the entities list. You'll need a class which inherits from some kind of [Entity](https://asset.party/api/Sandbox.Entity). You'll most likely want to use [ModelEntity](https://asset.party/api/Sandbox.ModelEntity). The code for the runtime addon featured in this article is very basic: ```csharp namespace Sandbox; public partial class MyAddonThing : ModelEntity { private TimeSince _lastJump; public override void Spawn() { base.Spawn(); Model = Cloud.Model( "facepunch.soda_can" ); SetupPhysicsFromModel( PhysicsMotionType.Dynamic ); } [GameEvent.Tick.Server] private void OnTickServer() { if ( _lastJump >= 3 ) { PhysicsGroup?.AddVelocity( Vector3.Up * Game.Random.Next( 80, 120 ) + Rotation.Forward * 60 ); _lastJump = 0; } } } ``` This code defines a soda can entity that'll hop forwards every 3 seconds. # Preparing to upload your addon to asset.party Once you are ready to upload your addon to [asset party](https://asset.party/) click the cog on your addon in the "Projects" dock. (Or right-click it and select "Project Settings...".) <upload src="b4bc4/8db9a23f7a29995.png" size="5002" name="image.png" /> <upload src="b4bc4/8db9a2447553b2d.png" size="46800" name="image.png" /> ## Assigning your addon to your organisation and tagging⤶ ## Assigning your addon to your organisation and tags⤶ Once here, make sure to set "Organisation Ident" to that of your asset.party organisation. Make sure to add "runtime" to the "Tags" of your addon. <upload src="b4bc4/8db9a25c2cc2c0a.png" size="2759" name="tags.png" /> Make sure to add the tags **"entity"** and **"runtime"** to the "Tags" of your addon. <upload src="b4bc5/8db9bc18e9d3f8f.png" size="1808" name="tags.png" /> <warning>⤶ These tags are required for your entity to show up in the entity list in the Sandbox gamemode and be eligible to spawn. These requirements may change in the future, especially if/when the Sandbox gamemode is re-written.⤶ </warning>⤶ ⤶ If you're creating an NPC, include the tag **"npc"**.⤶ ## Target Game This guide assumes you are making addons for the "Sandbox" gamemode. In this case you set "Target Game" to "facepunch.sandbox". For other gamemodes, it's as simple as: `orgident.gamemodeident`. As this guide assumes you are making addons for the "Sandbox" gamemode, you'll need to set "Target Game" to "facepunch.sandbox". For other gamemodes, it's as simple as: `orgident.gamemodeident`. <upload src="b4bc4/8db9a2483c3c470.png" size="1949" name="image.png" /> ## Primary Type Find "Runtime Addon" under "Addon". <upload src="b4bc4/8db9a249afc7739.png" size="1720" name="image.png" /> Set "Primary Type" to the class you made earlier. The way you do this is by typing the full class path, including namespaces. In our case, it's `Sandbox.MyAddonThing`. <upload src="b4bc5/8db9a31274397e4.png" size="12868" name="type.png" /> <upload src="b4bc4/8db9a25437118ff.png" size="1979" name="image.png" /> If your class were under a different namespace, such as `Sandbox.Entities.Fun`, then you would include it in full: `Sandbox.Entities.Fun.MyAddonThing`. The reason you need to set Primary Type is so that the target gamemode knows which class to instantiate when you want to create/spawn your addon in-game. For example, the Sandbox gamemode does something like this: ```csharp var entityname = package.GetMeta( "PrimaryAsset", "" ); await package.MountAsync( true ); var type = TypeLibrary.GetType( entityname ); var ent = type.Create<Entity>(); // Do stuff with "ent" ``` See [Mounting assets at runtime](https://wiki.facepunch.com/sbox/Downloading_Assets_For_Your_Server) for more information on mounting packages at runtime. See [Mounting assets at runtime](https://wiki.facepunch.com/sbox/Downloading_Assets_For_Your_Server) for more information about mounting packages at runtime. # Uploading to asset.party Under "Publishing", click "Upload To Asset Party". <upload src="b4bc4/8db9a2655cabf40.png" size="2598" name="image.png" /> Select your org like before, click "Next" and follow the upload wizard steps. ## Tagging and Publish State Select your org like before, click "Next" and follow the upload wizard's steps. ## Addon Publish State After your addon is done uploading, click the "View on asset.party" button on your addon in the "Projects" dock. <upload src="b4bc4/8db9a27937a33f3.png" size="2028" name="image.png" /> ⤶ Be sure to add "runtime" to the addon's "Tags" if it is not present, and set the "Publish State" to Released, otherwise your addon will not appear in-game. After that's done, click "Save Changes". <upload src="b4bc4/8db9a26cead73cc.png" size="26680" name="image.png" />⤶ ⤶ Set the "Publish State" to Released, otherwise your addon will not appear in-game. After that's done, click "Save Changes". Your addon is now ready to be used in the Sandbox gamemode. <warning>⤶ It may occur that the asset.party version of your addon no longer has the tags you applied earlier. You may have to re-apply them on the website.⤶ </warning>⤶ ⤶ <upload src="b4bc5/8db9bc188f0f802.png" size="27445" name="tags2.png" />