Hey all,
Have you ever wanted to have some characters using the platform behavior to collide with some solids but have others pass right through? I have a no nonsense solution to the problem. It involves modifying behaviors, so be prepared for that. I would love to put this all in a tutorial but for some reason it seems the new tutorial link is broken at the moment...
If you are fluent in the sdk and c2 scroll down for the TLDR.
Lastly, JackieChan has kindly provided a link to the modified behavior if you really don't want to touch the code. If you are a veteran coder and know your way around, this is fine, but I would strongly encourage the rest to try and learn why things were changed. The more fluent you can became at working in the sdk the more powerful c2 is. Plus you gain levels as a game dev! Skillz unlocked. The link is a few post down.
Okay, moving on...
There is a folder called construct 2 somewhere on your computer. Probably. It should be under program files or where ever you installed it. Follow this path -> Construct2 -> Exporters -> html5 -> behaviors. Inside the beahviors folder is one called platformer. Make a copy of this folder and name it something else ("MyPlatformerModified" for example).
You technically don't have to make a copy, but any changes you make to a behavior can break existing projects if you are not careful. Thus we will make it a new behavior.
In this folder there is a runtime file and a edittime file. Open both.
If you were just modifying the existing behavior you wouldn't make the following changes, but it is super important to make them if you copied the behavior. In the edittime file at the top you should see the following:
return {
"name": "Platform",
"id": "Platform",
"version": "1.0",
"description": "Jump and run between platforms (solid objects).",
"author": "Scirra",
"help url": "http://www.scirra.com/manual/100/platform",
"category": "Movements",
"flags": 0
};
You need to change "Platform" to whatever you named the folder when we copied it. Make it exact and make sure you keep the same syntax (spacing, quotes, commas' etc) Syntax in coding is like grammar. If you don't use it correctly the computer won't have a clue what you are trying to tell it. Its like a grammar nazi it won't do anything unless you speak correctly. You can also edit the author to indicate you have tampered with it.
Next, go to the runtime file and look near the top. You should see this:
cr.behaviors.Platform = function(runtime)
{
this.runtime = runtime;
};
(function ()
{
var behaviorProto = cr.behaviors.Platform.prototype;
Change Platform in both places to the same name you chose earlier. Again, make it exact. Spelling and caps matter. Anywhere you find the following code must be changed:
cr.behaviors.Platform.prototype
to
cr.behaviors.TheNameYouChose.prototype
Make sure of course that the "TheNameYouChose" is reflects the behavior ID that you replaced earlier.
Now that you have change everything to make this behavior indeed a different behavior all that's left is to change it.
In the Runtime file search for the following line:
behinstProto.posttick = function ()
and change it with:
Acts.prototype.platformupdate = function ()
Basically posttick is function that is called automatically by construct every tick in the game. By changing this to platformupdate we make it our own function. Now it will only be called when we tell it to be called. This is important because we now can have control over when the behavior updates. I am sure you see the significance in this
Now because we turned this into our own function, we need a way to call it. You notice that below the function line is an opening squiggly bracket {. There is a corresponding closing squiggly bracket for every opening bracket. We need to cut this entire function out from where it is and paste it in a new location. This will be lines 383 through 943. Its quite a large function, but it makes everything in the platform behavior "work" the way it does.
Paste it under the following line of code:
//////////////////////////////////////
// Actions
function Acts() {};
Make sure you copied the entire function! from its name to the final closing bracket. You also don't want it left where it originally came from. so make sure you cut and pasted it and not just copied it. We added it to the section of code that is responsible for containing functions that can be called from within construct as actions. All that is left now is to add this action in the edittime file.
Sear for the following lines of code:
AddNumberParam("Jump sustain", "The new jump sustain, in milliseconds to sustain jump velocity for.");
AddAction(14, 0, "Set jump sustain", "", "Set {my} jump sustain to <b>{0}</b> ms", "Set the jump sustain property.", "SetJumpSustain");
Under it add the following:
AddAction(15, 0, "Update", "", "{my} Update Platform behavior" , "Manually call the platform update function.", "platformupdate ");
This is where our script interfaces with construct 2 events. Save everything and open a new construct 2 project to test this out!
One last thing! If you are new to coding for c2, if you make a mistake, when you run your construct 2 project you will get an error. The error probably won't mean much to you, but it will give you a line number. Go to the code and look up that line number and you will find the mistake. With time and practice, fixing errors will become easy and fast. Also, you need to restart construct 2 every time you make a change to your script, other wise construct 2 won't load the newer version of the behavior. Check out the SDK in the manual for more good info!
---------------------------------------Construct 2 portion------------------------------------
Inside construct 2 create a quick scene with some solids. Make sure you have a least two different objects that have the solid behavior. A red one a blue one. Add a red character and give him the new platform behavior. Add a blue character and give him the new platform behavior. Add one more character but make this one purple, give it the new platform behavior as well.
At this point, if you run the game nothing happens. The behavior is identical in how it functions to the original platform behavior, but remember we need to call its update function manually now.
Add the following events:
EveryTick ->
PurpleCharacter set action Update.
BlueSolid Disable Solids.
RedCharacter Update
BlueSolids EnableSolids.
RedSolids DisableSolids.
BlueCharacter Update.
RedSolids EnableSolids.
There are much better ways of managing the turning on and off off the solids as well as updating the characters, but this should give you a general idea of whats happening. I keep my characters in families with a collision id variable. I also keeps my Solids in a family that has a collision id. I then enable or disable solids based on the ID and update all characters with the appropriate Id. Basically you can handle them all in groups and when you add a new character into the game you just have to give it the correct Id. You can change the character ID at anytime to allow it to pass some objects but not others. I use 0-7 for my Id numbers and use the id sort of like 3 bits. It is really neat what you can do with collision filtering and its disappointing that construct 2 doesn't handle this itself. I consider it necessary. You can use it to create Character that you can go through some walls while others can't. You can use it to make it so some objects only interact in the background layer and some the foreground layer. Ever played mutant mudds or xenodrifter? You can make the same effect in construct now. Yay. I'll move this to a tutorial once that works.
Let me know if you have any questions!
TLDT _____________________________________________________________________
1.) copy the platform behavior folder and change it to be a new behavior.
2.) Move the contents of the posttick function into a new function as an action. Create a corresponding action in the edittime file. I named it "platformUpdate" in mine.
3.) In c2 you can control when a platform object gets updated by calling the action you just created. Use enable/disable solids on objects with the solid behavior to control which ones the platform object interacts with.
4.) Use families and collision ids to update platform objects and enable/disable solids in groups and batches for the best performance, project scalablity, and pretty code.
5.) Yay. Or perhaps not. If the ladder then give holler. Usually I holler back. If the former feel free to add me to the credits of your autobiography when you are famous. I'm sure I'm the reason why.