Behaviour: Bad Coding on Scene Change?

Bane

  • Posts: 42
My situation is simple, I have a town with 11 regions that need scene changes, 10 are doors. I currently use the following code for the doors (and a different one for the scene changes)



Now this is possibly too many 'always' items for the scene to handle. Also this needs to be scene specific (if a scene has less 'doors' or the attributes aren't applied there will be epic slowdown).

How can I optimise this code, maybe make it shorter, hopefully make it reusable.

Is this pretty good code for someone who has never coded in his life (and had to make it up on the fly).

Cheers,
Bane

Bane

  • Posts: 42
I was thinking a nice change for this would be to start with 'if doorkey is pressed' then do the later checks - then the only thing the game should be looking for is if the doorkey is being pressed, before executing the code - otherwise the game is trying to look at all that code every frame, yes?

EDIT: At present the game checks 'is scene transitioning?' If yes, stop - If no (most of the time) is there a hero in door 1, door 2, etc etc.

What I typed would be 'is scene transitioning' if yes, stop - if doorkey was pressed, is hero in any doors, if yes, transition to appropriate scene.

Would that fix likely stop the slowdown if certain doors don't exist or don't have scenes attached?

InFecTioN

  • Posts: 21
As annoying as it may seem, a behavior for each door would be the cleanest and best working method. If you really don't want to do that. You could simply give each door an ID, when you hit is, you switch to the certain ID location,

Greg

  • *
  • Posts: 1260
The slowdown is most likely coming from errors being printed to the console.  If you press the ~ you can verify whether or not this is happening.  My guess is yes, since you mentioned that it happens with scenes that have fewer than the number of defined doors.

This is because your logic is checking each door whether or not they exist.  When you reference something that doesn't exist, it throws an error and prints it to the console and this leads to game slowdown.

If you want to continue with the behavior that you have - you'll need to add in another IF check prior to checking each door to see if "Door xxx has a value".  This block is in the Attributes -> Functions section I believe.

Another option is to use a behavior that I have on Forge called "Scene Switcher".  This lets you have an arbitrary number of regions that can control scene switches.  When creating a region, you double click on it - and name it to indicate that it's a Scene Switching region, as well as the name of the Scene to switch to.  This prevents you from having to create attributes for each region you might use.  The behavior should be pretty well documented as far as setting it up, but let me know if you have any questions.

Bane

  • Posts: 42
The slowdown is most likely coming from errors being printed to the console.  If you press the ~ you can verify whether or not this is happening.  My guess is yes, since you mentioned that it happens with scenes that have fewer than the number of defined doors.

This is because your logic is checking each door whether or not they exist.  When you reference something that doesn't exist, it throws an error and prints it to the console and this leads to game slowdown.

If you want to continue with the behavior that you have - you'll need to add in another IF check prior to checking each door to see if "Door xxx has a value".  This block is in the Attributes -> Functions section I believe.

Thanks for this Greg - it was indeed because of that. I have added the line of code to mine and tidied it up a bit (instead of doing if scene is not transitioning calls, it now does if key is pressed calls) - in total identical blocks of code.

I'm trying to make sure I build all my behaviours from scratch so I can get a feel of how to build behaviours and essentially 'psuedo code' - I would prefer to be able to make Stencyl bend to my will, as opposed to relying on the will of others :)