Revision Difference
Addons/GettingStarted#551425
<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
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.
<upload src="b4bc4/8db9a21646b8a36.png" size="23789" name="image.png" />
The file you just created is where you'll create the class for your runtime addon. You'll probably want one that inherits from some kind of [Entity](https://asset.party/api/Sandbox.Entity), such as [ModelEntity](https://asset.party/api/Sandbox.ModelEntity).
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). ⤶
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
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" />
## Target Game
This guide assumes you are making addons for the "sandbox" gamemode. In this case we set "Target Game" to "facepunch.sandbox". For other gamemodes, it's as simple as: `orgident.gamemodeident`⤶
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`.⤶
<upload src="b4bc4/8db9a2483c3c470.png" size="1949" name="image.png" />
## Primary Type
Find "Runtime Addon" under "Addon"⤶
Find "Runtime Addon" under "Addon".⤶
<upload src="b4bc4/8db9a249afc7739.png" size="1720" name="image.png" />
Set "Primary Type" to the class we made earlier. The way you do this is by typing the entire namespace the class resides in, and then the class name.
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.⤶
# Uploading to asset.party
Under "Publishing", click "Upload 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
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" />