@TheRetroRoomRoo There are a couple of ways you could slim down your collision code.
Method 1: Combine NPCs that share the same code but just have them use different Sprites
This is ideal if you have a lot of NPCs that say the same things and behave the same way but differ only by sprite art. You basically set the desired amount of NPC to be created in a room, and upon them being created you have them switch to a specific sprite or sprite frame that you want.
An example of this can be seen with the Bouncing Balls Example that's located in the DSGM 5.12 Examples folder on your Virtual Machine.
Every time a Ball is created it randomly selects a random sprite or sprite fame before being appearing in the game's room.
Method 2: Define your own Collision Function to reuse code but with different values
With a collision function we define, we can take the following code:
Code: Select all
if Pad.Held.Right
if(PA_EasyBgGetPixelCol(Top_Screen, 3,[X] + [Width] +2, [Y] + [Height]/2)!= PA_RGB(0,0, 0)){
If !Object_Under_Point(Top_Screen, [X]+[Width]+2, [Y]+[Height]/2, NPC1)
If !Object_Under_Point(Top_Screen, [X]+[Width]+2, [Y]+[Height]/2, NPC2)
[X] = [X]+2;
End if
End if
End if
If Pad.Held.Left
if(PA_EasyBgGetPixelCol(Top_Screen, 3,[X], [Y] + [Height]/2)!= PA_RGB(0,0, 0)){
If !Object_Under_Point(Top_Screen, [X]+[Width]+2, [Y]+[Height]/2, NPC1)
If !Object_Under_Point(Top_Screen, [X]+[Width]+2, [Y]+[Height]/2, NPC2)
[X] -= 2;
End if
End if
End if
}
And simplify it to this:
Code: Select all
if Pad.Held.Right
if CollisionCheck(([X] + [Width] +2), ([Y] + [Height]/2)) == false
[X] = [X]+2;
End if
If Pad.Held.Left
if CollisionCheck(([X] - 2), ([Y] + [Height]/2)) == false
[X] -= 2;
End if
End if
End if
}
We then define our CollisionCheck Function somewhere globally like in an external .h file that we add to the game settings' "Include Menu":
Code: Select all
'Here we are declaring a function called "CollisionCheck" which takes in any X and Y values and then returns/gives us a bool truth or false value based on what the function calculates.
bool CollisionCheck(int ValueX, int ValueY){
if(PA_EasyBgGetPixelCol(Top_Screen, 3,ValueX, ValueY)!= PA_RGB(0,0, 0)){
If !Object_Under_Point(Top_Screen, ValueX, ValueY, NPC1){
If !Object_Under_Point(Top_Screen, ValueX, ValueY, NPC2){
return false;
}
}
}
return true;
NOTE: This is just for educational purposes, I have not tested this code so you will likely run into errors if you just copy and paste this code into your project.
Unfortunately, I have not successfully declared functions directly in DSGM yet which is why I declare them in an external file, so you might not be able to use this method without a lot of trial and error should you decide to pursue it without an external file. I'm also not sure if external files are aware of object names declared in DSGM so it might not recognize NPC Object's names.
There is another way of simplifying your code without declaring a function but it's not as elegant. You basically create a bunch of variables that change values depending on which direction the player is facing and those variables are then used in place of the X and Y values used in the Collision if Statements. You then use a Blocked bool variable which changes value depending on if the collision if statements return true or false.