Username or E-mail
Password (Forgot?)
New? Sign Up
Join or Sign In
Forums Stencylpedia Stencyl TV Translations Go Pro - Plans & Pricing Download Stencyl


Switch to Book Layout

1: Getting Started

  • Getting Started
  • Crash Course
  • Crash Course 2
  • StencylForge

2: Building Logic

  • What is a Behavior?
  • Creating a Behavior
  • Attributes
  • Game Attributes
  • Attribute Types
  • Events
  • Our Behaviors

3: Actors

  • What are Actors?
  • Animations
  • Motion & Forces
  • Physics
  • Controls
  • Collisions & Groups
  • Killing vs. Recycling
  • Tweening
  • Effects
  • Chapter 3 Challenge

4: Scenes

  • Scene Basics
  • The Camera
  • Tilesets
  • Regions
  • Drawing Text & HUDs
  • Changing Scenes
  • Music, Sounds & Channels
  • Backgrounds
  • Customizing Actors
  • Chapter 4 Challenge

5: Game Mechanics

  • Saving
  • Pausing
  • 3rd Party Services
  • Web Requests

6: Advanced Topics

  • Get/Set Attributes
  • Custom Events
  • Blending Modes
  • Lists
  • Custom Blocks
  • Continuous Collisions

7: Testing & Tuning

  • Testing Games
  • Optimizing Performance 1
  • Optimizing Performance 2

8: The Last 10%

  • Flash Publishing
  • Standalone Apps
  • iOS App Store
  • Chrome Store
  • Making Money

M1: Mobile - Intro

  • Getting Started
  • Testing on your Device
  • Flash -> iOS Guide

M2: Mobile - Basics

  • Atlases
  • Drawing Text
  • Retina Display
  • Accelerometer
  • Joystick
  • Universal Games

M3: Mobile - Services

  • iAds
  • Game Center
  • In-App Purchases

M4: Mobile - Publishing

  • Debugging
  • Publishing to the App Store
  • Optimizing Performance
  • Promoting your Game

A: Troubleshooting

  • Showstoppers
  • General FAQ
  • iOS FAQ
  • The 90% Memory Warning
  • Recovering Broken Games
  • Can't Export to SWF
  • Reloading Documents
  • Generating Logs
  • Flash Security Settings
  • How to Report Bugs

B: How-To Guides

  • Importing Assets
  • Scene Designer
  • Code Mode
  • Font Editor
  • Pencyl (Image Editor)
  • Tile Editor (Shapes)
  • Game Cleaner

C: Reference

  • Glossary
  • Block Reference
  • Useful Shortcuts
  • Stencyl API

D: Resources

  • Stencyl TV
  • Abigayl's Guides
  • Giving Critiques
  • Creating Extensions
  • Translating Stencyl
  • Credits

3.0 Drafts (In Progress)

  • What's New in Stencyl 3.0?
  • Setup (Android)
  • Setup (Desktop)
  • Setup (iOS) - Concepts
  • Setup (iOS) - Mac
  • Setup (iOS) - Windows
  • Testing iOS on Windows
  • iOS Troubleshooter

  • Mobile App Scaling
  • Full Screen Mode
  • Simple Physics
  • Backgrounding an App

  • iOS App Store
  • Mac App Store
  • Windows Store
  • Google Play
  • HTML5

  • Android Ads
  • Android Purchases (WIP)
  • 4" Form Factor (iPhone 5)
  • Mobile Input
  • Mobile Features

  • Extending the Engine
  • iOS / Android Extensions
  • Developing the Official Extensions
  • Developing the Engine

  • iAds (Revised)
  • Game Center (Revised)
  • iOS Purchases (Revised)
  • Atlases (Revised)
  • Drawing Text (Revised)
  • Joystick (Revised)
  • Accelerometer (Revised)
  • Sounds (Revised, WIP)
  • Debugging (Revised, WIP)
  • iOS Performance (Revised)
Level: Expert

Custom Blocks

Note: This article is for advanced users. It's a holdover from the older documentation and may not be quite up to the new standard, but we thought it was useful enough to keep.


Contents

  • What are Custom Blocks?
  • How to Make Custom Blocks
  • Using Custom Blocks
  • Global Custom Blocks

 

What are Custom Blocks? (For Programmers)

Custom Blocks = Functions (or Methods)

 

What are Custom Blocks? (For the rest of us...)

A Custom Block is a wrapper (like the "Always" or "When Created" wrappers) that can hold virtually any blocks that you need it to. You can then use this block anywhere else in the behavior.

So what? What is the benefit of that?

  • Code Reuse - You can reuse the same bunch of code across the behavior. Bugs frequently happen when you copy the same code everywhere, make a change and forget to edit it all the places.
  • Data In/Out - You can pass data (parameters) to a Custom Block and take back a return value.

 

How to Make Custom Blocks

Let's make a custom block that returns the distance between 2 actors.

 

Step 1: Getting Started

Pick out the "Event" for Custom Blocks.

You'll now see this.

Custom Blocks Opener

 

Step 2: Give it a Name

Self explanatory.

 

Step 3: Give it Fields (Parameters)

Just click the + button in the block fields and you can select what parameters will be added to the list expected by the Custom Block.

 

Step 4: Spec

This one's tricky to put into words. Basically, this is how the block will appear to the user. The % fields are the blanks in the field. You can see in the Fields table what the % fields are.

Custom Blocks Wrapper

 

Final Step: Return Type

At the bottom, you can also select a Return Type, which is what will be given back to the behavior whenever this block is used.

Note that you can select None, in which case the block will just perform its actions but not give you anything back.

For Programmers: This is equivalent to "void"

In this example, we want the block to calculate the distance between actors and give us back a positive number, so I've selected the Return Type of Number. As a result, the custom block can now be used anywhere a Number attribute would go in the behavior!

 

Using Custom Blocks

Now that it's been created, you can find the new Custom Block wrapper (shown above) in your behavior at the very bottom below the other wrappers, and the custom block that you use to invoke it is in the palette as shown below.

 

Custom Blocks reside inside the "Custom" category of the Palette

Notice that there are other custom blocks that I made before, they're listed by the name of the behavior they're created inside of.

You may even notice that one is shaped like the True/False blocks, which is because it returns a Boolean value (which is either true or false, of course).

Custom Blocks Palette

 

Using a Custom Block

Like any ordinary block, just draw a Custom Block in to use it.

 

Note: The rest of this section completes the "distance between actors example that the original author started. Feel free to follow along or skip down to "Global Custom Blocks" to continue with the rest.

Now, we still haven't actually done anything yet to tell the custom block to actually DO anything. Below, you can see an image where I've grabbed all the necessary blocks for our needs. Notice there are two arrows that show you how to use the parameters.

You can grab the "Actor1" and "Actor2" blocks from the Custom Block's wrapper and use them inside of it as necessary anywhere an Actor attribute would be used. If you had used different parameter types (such as Number, etc) then you could use those in their appropriate fields instead.

Custom Blocks Detail

Below you can see it in it's final assembled state. Notice that the calculations have been put into a Return block (found under Messaging > Custom Blocks > Return Values) that will return the result of those calculations.

Custom Blocks Assembled

Now you can drag your custom block from the palette anywhere you want to use it! Using this block, I can now measure the X distance between any two actors.

Custom Blocks Final

In this case I set a Number Attribute called "Distance" to the result of the custom block (the returned value), but you could just as easily use it elsewhere where numbers are needed.

Examples would be things like the following:

  1. Inside an IF conditional check that compares the distance between two actors to another number value, such as AI trigger (have the enemy start chasing the player when he gets too close!).
  2. To add the distance values to a list for all enemies in the scene (using the "For each actor of type" wrapper and the "The Actor" block as the second parameter).
  3. As the width of a line or box being drawn between two actors.
  4. Converted to text and displayed on the screen as a measurement.
  5. and many many more!

 

Global Custom Blocks

Global Custom Blocks are custom blocks that can be "called" from any behavior. You may have noticed this as an option when first creating the custom block.

The catch? You can't refer to any of a behavior's attributes from within the custom block's implementation. Why do you think this is?

For Programmers: These are "static" functions

 

Note: Global Custom Blocks are not available for iOS. They will be supported in Stencyl 2.5.

 



Last Updated: 2012-03-30 by Jon

13048 have read this article
Disclaimer: The Stencyl Team does not actively monitor comments on articles. If you're seeking help for your game, please ask a question on the forums. Thanks!
5 Comments
pallavsharma91
@farah you have to make a "custom block" and not a custom event.
0 6 months, 1 day ago
farah
whenever i make a custom event i do not have the option of "number" as a return type or even in block field.and i could not test my game.what to do plz......... help
0 8 months, 3 weeks ago
MDuru80
rob1221 your crazy!
-3 9 months, 3 days ago
KelvinZhao
I think a little more explanation or perhaps an example can be given for global custom blocks? Seems a little hastily explained in just 1 line "You can't refer to any of a behavior's attributes from within the custom block's implementation. Why do you think this is?". Not too sure what that means...
1 10 months, 2 weeks ago
rob1221
Code blocks can also be used inside custom blocks. To refer to the inputs, use the name with two underscores. In the above example, "Actor1" would be referred to as "__Actor1" in a code block. This can make it a bit easier to use code not supported by blocks, especially when sharing on StencylForge to those who may not understand the code at all.
2 1 year, 1 month ago



Commenting Guidelines

Sign In to Comment

Make Games

  • What is Stencyl?
  • Roadmap
  • Pricing

Play

  • Arcade
  • Showcase

Community

  • Forums
  • Chat
  • Translations

Help

  • Stencylpedia
  • Stencyl TV

About Us

  • Blog
  • Contact Us
  • Press
  • Privacy
Follow Stencyl on Twitter



© 2013 Stencyl, LLC.