Ignoring specific solids using the platform behavior...

Discussion and feedback on Construct 2

Post » Tue Jan 19, 2016 7:48 pm

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.
Last edited by ruskul on Thu Jan 21, 2016 6:37 pm, edited 2 times in total.
Image
B
32
S
11
G
2
Posts: 563
Reputation: 5,096

Post » Tue Jan 19, 2016 7:54 pm

@Solomon
@Waltuo
@Zebbi


I think you 3 mentioned that you'd be interested in this solution when I posted it. Sorry it took me so long to get around to it. Hopefully it is still relevant. (: Also @Tokinsom , you might find this interesting as well. @Colludium - I think we talked about this like a year ago lol.
Image
B
32
S
11
G
2
Posts: 563
Reputation: 5,096

Post » Tue Jan 19, 2016 10:33 pm

ruskul wrote:@Solomon
@Waltuo
@Zebbi


I think you 3 mentioned that you'd be interested in this solution when I posted it. Sorry it took me so long to get around to it. Hopefully it is still relevant. (: Also @Tokinsom , you might find this interesting as well. @Colludium - I think we talked about this like a year ago lol.

Wow, this is incredible! I'm looking forward to hacking away and trying this, I really appreciate the effort you've gone to to figure out how to give us this pretty important feature. How does it fare with C2 updates, btw? Not that I'm complaining! :D
B
39
S
12
G
14
Posts: 1,110
Reputation: 11,184

Post » Wed Jan 20, 2016 3:08 am

Sweet work @ruskul - thanks for posting this! This is perfect timing - I'm just in the beginnings of creating a platform behavior game (using platform+), and I imagine that this will work as a hack for that too. (rubs hands with glee)

:)
B
68
S
17
G
65
Posts: 2,182
Reputation: 41,304

Post » Wed Jan 20, 2016 11:43 am

@ruskul This is AWESOME! Thank you so much for coming through and taking the time to put this together, this was beyond helpful. Briefly tested it last night and everything so far seems to work perfectly! Initially I got an error message and had to change a few more lines to reflect the new behavior name, but after that it was smooth sailing. Thanks again, this is very very much appreciated!

PS. How did you know the kind of game/mechanic I'm aiming to use this for;)

PPS. I have the same intentions as @Colludium , mixing this with the Platform+. Have yet to try it but hopefully it's added just as easily as with the standard Platform behavior..
B
31
S
9
G
4
Posts: 131
Reputation: 4,321

Post » Wed Jan 20, 2016 12:59 pm

Sorry, does this means I could technically switch the solids for lets say walls on and off to reduce the collision count?.

And then re-activated only when the character its at lets say 4 pixels from a wall.

Thanks!
B
39
S
10
G
14
Posts: 488
Reputation: 10,270

Post » Wed Jan 20, 2016 3:19 pm

@FractalZero - you could achieve that effect already by enabling and disabling collisions. With collision cells working as they do I'm not sure how much of a performance gain doing such a thing would give you though. Your code to check the nearness of all objects to the player and then turn collisions or Solid on/off might prove to be more demanding than just leaving the engine to run normally. It would be interesting to see if you find a difference....
B
68
S
17
G
65
Posts: 2,182
Reputation: 41,304

Post » Wed Jan 20, 2016 6:09 pm

@Colludium, Thanks for the info.
B
39
S
10
G
14
Posts: 488
Reputation: 10,270

Post » Thu Jan 21, 2016 2:26 pm

Thanks for this! It works well!

I finished your tutorial and I got 5 errors when I tested it, but I fixed them.

The errors were that I had to change all the

cr.behaviors.Platform.prototype

to

cr.behaviors.MyPlatformerModified.prototype

Here is the completed behavior for anyone that doesn't want to change the code. I also changed the author and help url in the edittime.js file.

Link: http://www.mediafire.com/download/q5y7l ... dified.zip

And here's an example cap of the behavior working

Link: http://www.mediafire.com/download/r9c2l ... tform.capx
B
10
S
4
G
6
Posts: 80
Reputation: 3,623

Post » Thu Jan 21, 2016 6:23 pm

@Colludium and @FraktalZero - I had a talk with Ashley about this a year ago and he said that you would most likely hurt your performance rather than help it. The collision system is very well optimized at the moment. Also, the collision detection algorithm runs much better than checks you can make in the event sheet. In general simply asking if something is overlapping is a very fast check depending on circumstances. Now I can see managing objects so there are no collision checks off screen for some games... I have done this and gained performance because of that, but I had thousands of objects that needed to be checked. Disabling them once they were off screen was a good way to cut down the volume of checks needing to be performed. But I wouldn't worry about enabling and disabling collisions a few pixels from where it matters. Actually, if the bounding boxes aren't overlapping the collision check is super fast. You would basically be doing the same thing it already does, but more slowly because it is via events instead of jscript.
Last edited by ruskul on Thu Jan 21, 2016 6:42 pm, edited 1 time in total.
Image
B
32
S
11
G
2
Posts: 563
Reputation: 5,096

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 7 guests