Custom Code/NSMBU-Haxx/Creating a Custom Sprite

This guide will take you through the steps to create a fake checkpoint sprite, and then once you learned the process, you will be able to create your own sprites.

Requirements

 * NSMBU-Haxx
 * At least a basic understanding of C++
 * Text Editor of your choice

Setup
The first thing we will need to do is to create our files. In the  directory create a file called. This is where all of our code will go into. Next, add the .cpp file into  like this: Also, at the beginning of our .cpp file,  the following header files: If you are missing any, just grab them from this repository:

[TODO]

Creating the class
All actors are classes, which inherit base actor classes, and can override their virtual functions to execute code. We create this class like this: The  means that it will inherit the   class. Inside that we add our class members. First is the constructor, which initializes the members of the class, and we pass it the  parameter which has crucial information about the actor like the position, nybbles, layer, etc. Next we declare the build function, which returns an instance of the actor that it will instanciate. Then we have some virtual functions that we override (customize). The  function is called once when the actor is created, the   function is called every frame except when the game is frozen such as when you take damage or entering a pipe, and   is called every frame no matter what. After we declare our own custom function, which we will set up to be called when the player touches the actor. After that we declare the  function which we will be customizing, and then the   function, which we will set up to call. This takes two parameters,  which is the actor itself, and   which is the actor that is touching the actor. Finally, we have the model variable, that we will define to be the model we chose, and then there is the  info that we need for the   class for collision for our actor.

Profile Setup
After we create our class, we will need to set up some profile related stuff. Profiles have all the information about the sprite name, resources, and ID. We set it up like this: The profile constant contains the address of our build function, a Profile ID which determines what sprite ID it will replace and a name. The last two parameters you don't need to worry about. The  defines what ID to replace, and what resource archives to be loaded for the level. If it tries to load an archive that isn't in it's profile resources, and also isn't in another actor's profile resources which is in the same area, the game will most likely crash since the archive it wants is not loaded.

ActiveCollider
Next we will need to set up our  collision information. We define that like this: This info section is actually a struct, which contains data in the following order: The X/Y offset from center and X/Y size are pretty self-explanatory, and for reference:  equals 1 tile.

The shape is an integer where 0 is rectangle, 1 is circle, and other numbers are unknown. The collision mask sets what collisions will be detected, such as Yoshi, and the flags sets allowed interactions such as being pickup-able.

Constructor
After that we will create the constructor for our class. The constructor is pretty simple, just taking an  param, and inheriting the   constructor. Next we create the build function which instanciates and returns an instance of the class. You can view how both of those are written here:

VTable Functions
Here is the fun part, now we get to override the vtable functions and make our sprite behave however we want it to. We need to create 3 functions,,  , and  , with an extra   which just updates the model.

onCreate
The  function will be as follows: Firstly, it defines the model variable to be what model we select. The first string is the archive name and the second string is the model name. The final parameter is the number of skeletal animations in the model that you chose. In our case both of these are  which is the checkpoint model, and it has 4 skeletal animations. After that, we will make it play the wait skeletal animation which is just the idle waving flag animation. After that we will initialize the  with a pointer to itself and a reference to the   data that we defined. After that we will add a rotation modifier to turn the model to the left. Finally, we update the model and  which signifies that everything went well.

onExecute
The  won't really have anything in it, since we are only going to be doing stuff when the player touches it, but you can put whatever you want here and it will be ran every frame. We will just update the model and.

onDraw
Similar situation with the  function, but a bit different. In the  function we will take an instance of the   class and use the   method and pass our model variable to it to render the model for us.

updateModel
The  function doesn't have much in it, but it is still very important. First, we will update the animations for the model, since we are using the wait animation that's included with the checkpoint model. After that, we will create a  (Mtx34) type variable, which contains rotational and positional data for the model. Next we will set the matrix for the model, and then call  again since we will be constantly updating the model each frame.

touch
This is the main custom part of our actor, since this is what will happen when the player touches it. We want to spawn an effect when the player touches it, so we will need to set up a position variable of type Vec3 since it needs 3 pieces of data in it for the position X, Y, and Z. We will set those to the same position as the actor since we want it to spawn directly in front of it. Next we will spawn the effect using the  class, passing in the effect ID, and the address of the   variable. The last two parameters are for rotation and scale, which we do not need to modify. Finally, we will set the bool  to true which will make the game delete our actor.

collisionCallback
Lastly, we will need to make the collision callback function just call the custom touch function that we created. We use a  conversion to tell the compiler to treat this function as the custom   function.

Conclusion
And with that final function, we're done! Compile your code and test it out, it replaces the sprite 312 so place that into your level beforehand.