Ignoring specific solids using the platform behavior...

0 favourites
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • 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.

  • 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.

  • 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!

  • 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)

  • 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..

  • 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!

  • - 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....

  • Colludium, Thanks for the info.

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Colludium and - 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.

  • JackieChan - Excellent, thanks for posting that. I figured I would overlook something. Changing the op to reflect this. I am also adding your link to the op for the edited behavior file if that is okay.

  • Yeah you can use my link. I don't mind.

  • This thread should be famous, can't thank you enough again Ruskul for actually figuring this thing out!

  • Forgot to ask, or I'm being forgetful, does this work per instance or per object?

    edit: what I mean is Ruskul can you test for an instance of an object as usual, then have the update apply only to that specific instance rather than all of them?

  • This looks very promising

    Ashley What do you think about this workaround ? Can you implement it in the official plugins ? Would it have negative effects (bugs/glitches) ?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)