Advanced Ship Editing

Before looking at this thread, it is recommended that you have first looked at the original Ship Editor Tutorial and are well familiarised with the Ship Editor.

Introduction
Before we delve into the power of CoffeeScript, we need to make a slight change to how we call our model first (not needed for few limited power like array extensions).

Instead of  at the beginning of the ship code, we will simply change that to   and at the very end of the ship code we will put. This won't affect how we normally use the ship editor and simply allows for extra code to work after the ship code.

As a reminder, I'll put the properties for bodies and wings as a reference:

Reset Points
Reset points allow us to start a new object from the same body (well, technically not but at least appear to).

A reset point is when 2 joints next to each other in the width & height arrays are 0. The location of these 2 joints however will be in different places, giving the illusion of starting another object elsewhere from the same body. (In reality, the object draws an invisible 0 width and height line between the two joints.)

For example: Here's a more extreme example of using reset points in this construction pylon. Yes, this is all one body.



Functions
Functions are prepared blocks of code that sit before our ship code, which can then be called later on. For our purposes, we will be using them to make bodies.

To create a function, all we need is the name of the function followed by brackets, and then the magic  and there's your function.

It should look something like this: But right now it has no use.

So lets start by making it create a body for us. For this example I will be using a simple ring generator function, but before we do, I will post useful array extensions in which the function partly relies on.

Credit for these array extensions go out to 123 Notus. The top block of code are the functions themselves, but due to an error in Ship Editor, the mod export will not work. This is where the fix comes in the second block.

We use them after arrays as follows: More details can be found there: Array extending in Starblast Ship Editor.

Back to our function: As we can see, the function now has internal variables assigned to it which we have mapped to the corresponding properties of our body. We call it under bodies in our ship code as follows: And it should look something like this:



Functions like this are useful for creating complex bodies. It means we can just take a pre-existing base and call it from 1 line rather than manually create the whole thing many times to where the code takes 500 lines worth of bodies and all their properties. It should be noted that functions don't need to be this complex. It's entirely possible to simply use it for mass duplication of bodies with the necessary control using the function parameters. It's also useful if we decide that we want to change something that all of these bodies have in common. We can do so by changing the body properties in the function itself and all of the bodies called from the function will update, rather than having to change every one individually.

So as useful as this is, we can go further. I want my functions to be able to create not 1, but several bodies. For this, we're going to have to do something slightly different.

Instead of using  which allows us to call the function alongside the bodies of our ship code, we will be using what is called   which will let us pre-specify our body names using strings. Note that I've also included  for this case, it's important that we avoid duplicate names because the code cannot differentiate 2 different objects with the same name, even with different properties. So the best solution for this is to number these bodies by adding n in the function parameters. This will let us manually number them, but we still need to be careful that we don't end up with duplicate numbers. This becomes especially important when we get to loop functions.

I want to put turrets on my ships. So lets do that using a 3 body function. To call this function, rather than call it under bodies in our ship code, we instead have to call it after our ship code.

We can take this to extremes as we see fit, however I cannot stress enough the importance of keeping code like this optimised as much as possible. Highly detailed, 1 pixel in game is absolutely unnecessary and only wastes server resources. In other words, be careful when calling a lot of functions like this. The poly-count adds up faster than you think.

for-loops
loops allow you to iterate over bits of code. Essentially, it generates an array.

You can use it like this for specific numbers But the most useful way is to use it like this 2 dots will generate the array between the 2 values, including both values. 3 dots will generate the array but without the last number.

You can also use it like this for multiples of 2 For our case, we will be using for loops to iterate over bodies, again using model.bodies. The previous image example showing our turrets from our function was actually using a for loop. Notice how  is used and multiplied to spread their distances. And yes, it's entirely possible to use loops to iterate over laser properties. Here's another example where we generate an array of cubes

Oh and did I mention we can nest loops to generate a grid?

Oh, no. We're not done yet.

Alright that's enough madness. But seriously though, be careful when trying this, it will most likely lag.

Conclusion
There's just one final note I would like to put out there. The ship editor was never expected/meant to be used to such an advanced level, so to those programmers out there who are feeling creative, don't expect much as the ship editor comes as it is. Everything here uses the same fundamentals as the original tutorial, just in a different way that takes off a good amount of manual work. The most important thing to remember in ship building is to remain open and creative. Highly detailed ships are good and all, but don't go overboard with bodies. Make good use of the available textures, as limited as they may be, because they compensate for large, flat areas that lack detail. It's completely fine to use several bodies to construct a detailed part of a ship. Just make sure they're not just a pixel big in game, otherwise they become unnoticed and obsolete.

But most importantly of all, experiment, have fun and create complete garbage ships. Because that's how you will learn and improve. The ship editor is more than just capable of building ships, which the most talented ship builders Finalizer & Goldman & Uranus have proven.