[SOLVED] Sharing an attribute between scene and actor behaviors

Beaumint

  • Posts: 16
This might be a really dumb question.  I tried searching the forums but I'm struggling on how to word my question.


Okay, when I want to obtain the current score of the game I'm running it's very easy.  I can just create an actor behavior and use "Get Current Score" custom block that's created by the 'Score Manager', the pre-loaded behavior that's added to my scene.

When I want to obtain a different number attribute that's also created in my scene in an actor behavior through a custom block I get an error that causes my game to malfunction. 

Why is this?

For a raw example:
Get current score (from a scene behavior created by stencyl) works fine when called by an actor behavior.
Get current level (from a scene behavior that was created by me) does not work when called by an actor behavior.

Is there a way to fix this so I can share attributes between scene and actor behaviors?
Sorry if this is worded poorly, I tried my best, lol.

« Last Edit: July 18, 2013, 04:56:07 am by Beaumint »

Innes

  • *
  • Posts: 1963
Are you familiar with the Attribute | Behavior blocks? I think they might be what you are looking for, e.g:

[for this scene, get ... from behavior ...]
[for Actor, get ... from behavior ...]
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

Beaumint

  • Posts: 16
Thanks for the reply Innes.

I really struggle working with moving information between behaviors though, I always get 'Behavior not found errors' even when I'm doing it through a scene.

I caved in and made a 'game attribute' instead.  I'm just afraid of making too many of these, I'm not sure how much they hurt performance?

Innes

  • *
  • Posts: 1963
I have a feeling that I used to struggle with with getting attributes from other behaviors too (although I couldn't say what, because it's so long ago). I wonder if  there's an issue with Stencyl 2.2 - I use the 3.0 beta exclusively now, and I never have a problem with these blocks.

Perhaps an experienced Stencyl 2.2 user could confirm if they are aware of any issues.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

Photon

  • Posts: 2697
What comes to mind for me is this: if you are trying to use the "get this from other behavior" blocks Innes originally mentioned... if you use them in a "When Created", you can get errors. This is because its possible the other behavior you are trying to access hasn't been "initialized" yet. If this is the case, enclose said blocks in a "Do After .01 Seconds" block.

Also, if you are having an issue with calling custom blocks from a scene behavior, for the love of Stencyl make sure you actually attached the scene behavior to the scene (Stupid as it may sound, it happens). :P
Do NOT PM me your questions, because I likely will not respond. If I have replied to your question on the forum, keep using that topic. Thanks!

Innes

  • *
  • Posts: 1963
Photon has made two very good points there! With regards to forgetting to attach behaviours to scenes or actors, I often used to do this, so my preferred process is now...
  • Create the blank behavior
  • Attach it to the scene or actor
  • Build the code for the behavior
I think I recommend this process in my book.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

Beaumint

  • Posts: 16
Great advice, thanks for the tips. (As I'm creating this game I'm more and more tempted to perhaps pick up your book, haha.  I think it will depend on how this game pans out as it gets closer to completion)

I think I will stick with the game attribute for this one, as its beginning to make more sense in the game to stay that way as it is.  I will definitely not shy away from using 'getting information from other behavior' blocks in the future if it makes sense.

I will go ahead and marked this as solved, I'm sure a very common error for people trying to get their code to work is them initializing it in a 'created block.'  I think that's what I may have been doing.

Thanks again!!  I really appreciate this helpful community!
-Beau

Innes

  • *
  • Posts: 1963

I think I will stick with the game attribute for this one, as its beginning to make more sense in the game to stay that way as it is.  I will definitely not shy away from using 'getting information from other behavior' blocks in the future if it makes sense.


There's nothing wrong with using the odd Game Attribute here and there, but it's not a good idea to rely on them too widely, as it makes the development process more difficult to manage. I think that, for most games, the effect on performance would probably be minimal.

I know that, when I started using Stencyl, I ended up with loads of Game Attributes and really struggled to keep track of what was going on.


I definitely recommend limiting their use when possible.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

aroncruce

  • Posts: 214
Thanks for all the advice guys I'm working on.similar issues, also question is it better for performance to have multiple behaviors and sending and retrieving data with the get _____ from behavior _______ or to just use one or two game attributes?