"Game Behavior"

Dreames

  • Posts: 18
I'm having trouble creating a common class with functionality that can be shared between the other behaviors.

For instance I have a need for numbers to be displayed using a certain format.
I have a custom block that I pass the number into, the event does a lot of custom work and math then returns a string withe the formatted number result.

I want to be able to use this anywhere in the entire game, whether a scene behavior is calling it or an actor behavior. I don't want to have to create a scene behavior or actor behavior and attach it everywhere to call this functionality.

Therefore "Game behaviors".
It can hold common events that would be called from different actor and scene behaviors as necessary. It would also not be necessary to attach it to anything within a scene for it to be used.

I believe code wise it would look something like:
The Game behavior could be called GameCommon
Code: [Select]
[assembly: AssemblyTitle("GameCommon")]
which would create a GameCommon.dll
Then events under it would be coded:
Code: [Select]
namespace GameCommon.GameEvents
{
     public class EventName
     {
     }

//or for my instance

     public class Numbers{
        public static string displayNumber( int inputNumber){
        inputNumber++;
        return inputNumber.toString();
     }
}

I would then have
using GameCommon.;
at the top of every behavior created.
I could then have my number I want displayed and have

Code: [Select]
Using GameCommon;
...other code and behavior creation...
String displayText = GameEvents.Numbers.displayNumber( 10 );
Text.Draw(displayText);
and it would draw "11" as a string.

I could draw up with the blocks would look like.
I believe the GameBehavior should have its own color code. Technically it is a behavior, however unique, so maybe a different shade of red.

« Last Edit: March 20, 2015, 02:03:27 pm by Dreames »

Justin

  • *
  • Posts: 4583
You can use global custom blocks to call code that isn't attached in any way to an available scene or actor. You still need to create a behavior to store all your global custom blocks in though.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Dreames

  • Posts: 18
So I tried that actually. It was the first thing I noticed.

But when trying to call that global custom block from another behavior I kept getting errors.

GlobalBehavior : has global custom block

Actor Behavior:  calls global custom block:
Unable to access attributes of GlobalBehavior

I can post the exact errors if you need me to, but it will be a few hours.

EDIT:
Ohhh... I think I just realized the problem.

I need the attributes localized to the global block, not to the behavior.

« Last Edit: March 20, 2015, 02:21:41 pm by Dreames »

Justin

  • *
  • Posts: 4583
No, that's okay. I see what the problem is. (edit: I guess you saw the problem too )

Because attributes are not static, and global custom blocks are, you can't access your attributes from the global custom block.

We don't have a way to define attributes as static in Stencyl, so all I can give you right now is workarounds.

// ===

- You could use game attributes instead of behavior attributes.

// ===

- You could define static variables for the behavior with the "Custom Code" event, and then use the code blocks to refer to them. ( Flow > Advanced )

Custom Code event:
[ code : [ static var i:Int = 0; ] ]

Your function:
[ print [ code : [ i++ ] ] ]

// ===

- If the attributes don't need to be persistent, you can use code to sort of "gain access" to the attribute blocks.

[ code: [ var _MyNumberAttribute:Float = 0; ] ]

( then you can use the number attribute "My Number Attribute" as a block like you normally would )

[ print [ My Number Attribute ] ]
[ increment [ My Number Attribute ] by [ 10 ] ]
[ print [ My Number Attribute ] ]
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Dreames

  • Posts: 18
If I use a custom code inside of an Event to create an attribute, will that attribute be displayed under the attributes in the design mode?
( I would try this, but I don't have access to stencyl right now.)

If yes,
Since the attribute was created inside of the event, it would then be technically localized and accessible right?

if not,
 I create an Event variable, do I need to use code to access it?

Basically,
I would rather do really complicated math using variables that are only going to be used to do the math, then disposed.

Justin

  • *
  • Posts: 4583
Attributes displayed in design mode are only the ones that you created using the new attribute dialog. If the attribute is created with code blocks, Stencyl and design mode don't really know that it exists. It just goes straight into the generated code.

Sounds like you want to use global custom blocks with code blocks to initialize and work with temporary variables.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Dreames

  • Posts: 18
The third option almost works.

Except most of the math functions create "asNumber" which causes the code to fail.

Justin

  • *
  • Posts: 4583
Global Custom Blocks are handled much better in Stencyl 3.3.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Dreames

  • Posts: 18
So Bringing the subject of Game Behavior back up...

In our game we needed a clock to persist across the entire game.

I created a Scene behavior and attached it to every single scene.

Is there a way to do this without having to attach a behavior to all the scenes? Such as an "Add to all, now and forevermore."

Justin

  • *
  • Posts: 4583
Not yet, unfortunately.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)