Why do we have Global Custom Blocks?

Epic428

  • Posts: 1118
[Rant,Help,Whatever]

OK, so I haven't played much with custom blocks. I thought that for the menu screen I'm putting together in a game, they would work great. I figured I could basically put the custom block in a single behavior to run some back end code in another behavior, eventually displaying text on the actor for which the Custom block was attached to.

In simpler words, when I hover over any button on the screen it displays text in the message bar placed in the menu. Unfortunately for me, that didn't work. After previewing the code and checking the help files, I quickly realized that regular custom blocks will only work for the actor it directly attached to and not for what I needed. Fine.

Sure there is another way I could go about this, but it would require me to either make the behavior a scene behavior, or create a reference to the message bar in every actor on the scene. I'll have to go about this method currently but, I'd like to refrain from creating unnecessary references. It is just easier to make this a one stop shop.

Moving on. I thought that Global Custom Blocks would be the better choice, however, upon previewing the code, I realized it would not work because the function becomes a Static function, which explained the hundred errors I received about my attributes not being recognized.

So my question: Why do we have Global Custom Blocks?

In their current state, they are useless. All that can be done with a Global custom block, is a simple math function and a return. Any local variables for the function would have to be done using code blocks which is fine. However, here is the biggest hurdle: Static functions can only use static variables on a class-wide level. which is why I said in my earlier statement, they can only do a simple math function and a return.

Code mode would defeat the purpose of a Global Custom Block.

Also, I noticed that when a Global Custom Block that has already been created is used in another behavior, it is referenced as
Code: [Select]
actor.say("Display Message", "Display_Message", ""); - example from my current game.

I thought that Global Custom Blocks didn't have to be accessed via the actor for which it directly belonged to? So, instead, shouldn't it be referenced, in preview code, as
Code: [Select]
Design_64_64_DisplayMessage.DisplayMessage("This is my message I wish to display!"); ?

Also, going back to the attributes not being recognized, is it not possible to add a check box next to hidden, in the create attribute menu, that says "Global"? What I mean by this is that instead of declaring the variable like this:
Code: [Select]
public var MessageText:String; it is declared like this:
Code: [Select]
public static var MessageText:String;
Then, under the attributes section in the palette, there could be a section called Global Attributes. These would be the Global Variables people were asking for a while back. They could be accessed from any class. or if "Hidden" is also marked, only from within the current class, but capable of being used with Global Custom Blocks.

Unfortunately, I am either completely misunderstanding how Custom Blocks really work, or their functionality is far more limited than led to believe. I also wasn't sure if this should go under suggestions as I've made suggestions in this thread or where it is currently placed.

Any insight on this, thoughts, comments, discussion would be greatly appreciated as I'd like to better understand the intended functionality of these blocks.

p.s. I wasn't too thrilled with the help content about Custom Blocks, though, I'm not sure what would make them any better.

[/Rant,Help,Whatever]
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Legendary Hoamaru

  • *
  • Posts: 1088
Global Blocks are extremely important, and have plenty of outside uses rather than just math functions.  Global Methods can do basically anything except store a variable (which you can workaround, as shown below), you just aren't exactly using them correctly :P.  There are not many reasons to use global blocks with snippets that attach to actors or scenes, they are usually used for utility snippets not attached to a scene/actor.

The only thing it lacks is static variables (and maybe method level variables), which you can actually work around doing something like the attached image (with any type of messaging block).


« Last Edit: March 29, 2011, 12:35:41 am by Legendary Hoamaru »

Epic428

  • Posts: 1118
So to understand what you are saying, I can declare variables, import classes, and create new functions by using a Code block within a Global Custom Block? Would that mean that if I wanted to I could also declare Static variables as well?

Edit: And how exactly would that work, declaring these things inside a function? Would Java rearrange the code into their proper places? I've just never seen Class imports and Functions inside of other functions.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Justin

  • *
  • Posts: 4584
It's not actually inside the other function. The trick that LH uses is actually closing off the DM function, importing the Dictionary class, and then creating a dummy function that will be closed off naturally by the DM block.

I've actually just thought of a nice block for DM. A standalone code block. Can't be attached to other blocks, like the event wrappers that behaviors start with. This would put code straight into the behavior, without having to resort to a clever workaround like LH posted.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Epic428

  • Posts: 1118
Ah! OK after replicating it and viewing it in preview code I see what is being done.

So basically for what my needs are: I can use the global custom block to run a function that I custom coded within the blocks itself, giving me a way to, move the data from function to function.

Here is my question then: Why is the Dictionary Class being imported? Or is that just as an example?

Also, I can't compile this:
Code: [Select]
public static function sendMessage(__message:String):void
{
    displayMessage(__message);
}
private function displayMessage(__message:String):void
{
    _TexttoDisplay = __message;
    print("" + _TexttoDisplay);
    _XOffset = ((actor.getWidth() - getFont(91).font.getTextWidth(__message)) / 2);
    _YOffset = ((actor.getHeight() - getFont(91).font.height) / 2);
}

because of the fact that I'm calling a non static function. So how would the example shown work in my case? Am I still misunderstanding it? I do realize the injection of code, that wouldn't otherwise be able to be injected, unless the behavior was designed in code mode.

Oh and I like that suggestion Justin. I think that could be very useful. Perhaps would it be possible to make that type of block optional? For example normal users, could use standard DM, where as slightly more advanced users could use a CM/DM hybrid that carries the benefits of both?

« Last Edit: March 29, 2011, 12:45:38 pm by Epic428 »
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Legendary Hoamaru

  • *
  • Posts: 1088
I'm kind of confused on what you're attempting to do.  Why do you want to use a static method to call a non static method from the same class?  Are you trying to have it send the message to all instances of that snippet?  From the way your code looks you could completely remove the static block.  Does your custom block have a field to select which actor to perform the action on?  (Something like "For {whatever actor} display message {whatever message}")

The Dictionary class was just an example.  The trick also allows you to import other classes and use other objects that aren't declared/supported in DM.  You can also declare static variables, which you can return/set through global custom blocks.

Epic428

  • Posts: 1118
The Dictionary class was just an example.  The trick also allows you to import other classes and use other objects that aren't declared/supported in DM.  You can also declare static variables, which you can return/set through global custom blocks.

Basically, I have about 15 actors on my menu screen. Every one of them when hovered on is to display a message in the same location. I figured if I customized the behavior and attached it to the Message Bar, I could then use a single block in the several different behaviors to send specific text to the message bar that it would display.

Ultimately, what I want to happen is I want to use a static function that the instance of the message bar would access from within it's own class. However, the above quote just gave me a good Idea on how I can do this with minimal code, while still using DM blocks.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Epic428

  • Posts: 1118
And it worked. Thanks for providing that example and clarifying the Global Blocks.

I really liked Justin's idea so I hope to see it at some point.

And, I can see a lot more functionality in DM based off the example alone.
James Moore - Official Support & Documentation.
We cannot and will not respond to PM's asking questions. Please make a new thread in the forums if you have any questions, Thank you.
For better support and faster response times, please post your logs regarding any Stencyl related issues. Debug > Logs > Generate Logs

Justin

  • *
  • Posts: 4584
It's actually pretty easy to do. You don't even need a custom block. Just grab a message receiver, name it something random, put in a code block, and do the same thing as you would with the custom block.

I have to say, I'm really enjoying the ability to inject code into DM. This is a pretty neat trick.Thanks, LH.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)