Passing Variables between actors in GameSalad

So I have a situation where I want a hungry actor to pick up food when he’s *not* eating, but not when he’s already eating. From the actor’s perspective, it’s pretty easy in GameSalad. You simply detect the collision from that actor, have an internal boolean variable about whether you’re hungry or not, and take action accordingly. However, what about the food? It can detect that it’s overlapped with an actor, but without using Crazy Global Variables or something, there’s *no* way it can know if that actor is hungry or not. The solution?

Two identical-looking actors. One hungry actor, one non-hungry actor. When the non-hungry actor gets hungry, he spawns a hungry copy of himself and Destroys himself. The same going the other way.

Ok so far as it goes. The food knows whether to disappear or not because it knows it’s collided with either a hungry actor or a non-hungry actor. However, there is *one* tiny problem:

There are other variables that must be passed from the non-hungry actor to the hungry one.

Shit.

So I *could* use some global variables to pass this info and *hope* that two different actors don’t step on each other. However, that’s messy, and likely to fail spectacularly at some critical time during my Nobel Acceptance Speech, so I wanted to try something else. The solution? Take your medicine!

I have two variables, one for Health, and one for Hunger. Both must be passed to the next incarnation. I had the idea that I could create objects from the Destroying actor that the Spawned actor would detect upon his creation, then set the variable accordingly. Originally I simply had a Health Pill and a Hunger Pill, and I generated the correct number from our noble, dieing actor for the next actor to Collide with. However, GameSalad doesn’t seem to handle multiple simultaneous collisions correctly, so instead of ending up with a health of 4 when I dropped 4 Health Pills, he ended up with a health of 1, but all of the pills still (correctly) destroyed themselves when they detected the collision.

The solution? Pills for each value. So now I have a One Health Pill, a Two Health Pill, etc. And this works! When I put pills of various values for the next actor to pick up, they are correctly detected by the actor, who sets his Health and Hunger accordingly, and the pills destroy themselves upon collision!

Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

3 Responses to Passing Variables between actors in GameSalad

  1. Andrew says:

    It may work reasonably acceptable with your scenario but beneath this still lies a problem of actor-to-actor communication without global variables.
    For example, I would like to dynamically determine how many actors I want in a screen. And I would like for each actor to be aware of everyone else’s position.
    That means that each actor should post his data in game’s global variables, which, unfortunately need to be defined upfront.
    If there is no direct actor-to-actor communication, global variables should at least support array variables. This way every actor could access the global array and put his own position inside and access everyone else’s…

    • Scott Mauer says:

      That’s true. I haven’t re-visited this problem in a while. However, haven’t writeable tables been implemented since I posted this? If they had, they might be usable as a “global array” for this kind of thing. Each object could have a globally assigned index number, and that number could be used to index into a global table with a hard-coded name. I haven’t tried it in practice as I’m using Corona more now, but it might be worth a try.

  2. Andrew says:

    Again, maybe tables would prove good here, haven’t even tested them yet 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s