Author Topic: access another sprite's properties  (Read 4179 times)

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
access another sprite's properties
« on: 2007-11-28, 06:30:55 AM »
i have to ask.
could you please give a few examples of "relative paths"?

my example:
when the player hits Button1, the sprite parameter "Hello" of the sprite ENEMY should be changed to 1.
when i create a rule "Do: OUTPUT = 1" i need a path from my sprite to the enemy sprite.

would you be so kind and give me the path for:
a rule in a layer 1 plan changes the parameter of a sprite which is also on layer 1
the same as above, but the sprite is at layer 2
a sprite rule changes the parameter of sprite 2 in the same layer
a sprite rule changes the parameter of sprite 3 in another layer

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: access another sprite's properties
« Reply #1 on: 2007-11-28, 07:47:47 AM »
A rule on the player sprite cannot easily set a parameter value on another sprite because the player sprite definition does not know what other sprites exist on the current map (it can be used on any map, so it can't assume which sprites are available).  However, you might be able to use plan rules and/or something like SelectTargetSprite and SetTargetParameter to achieve this.

Really your sprites should probably not be interacting so directly, though.  The player sprite should not assume that the enemy sprite is there.  Maybe it should set some global counter value, and then the enemy sprite rules should check the counter value in its own rules and set its own parameter.

Keep in mind that sprite rules cannot access any map-specific data because the same rules must be applied to any map.  So you see you need to be less direct when transferring information between the sprite rules and other elements on its layer.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: access another sprite's properties
« Reply #2 on: 2007-12-02, 01:54:27 PM »
Maybe it should set some global counter value, and then the enemy sprite rules should check the counter value in its own rules and set its own parameter.

that worked great, thank you. i used one counter as communication help between two sprites and a plan. ;D

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: access another sprite's properties
« Reply #3 on: 2008-01-25, 02:58:52 PM »
about two months later i'm in the same dilemma and just now i don't know how to solve it.

this is given:
some sprites at the map. let's call them "moss". two or three dozen more sprites, lets call them "plants". the plants always touch moss. in fact the moss is very large sprite, and several plants shall grow out of it.
when the player throws seed over the moss, the plants shall grow. all works, but i don't know how to make the connection between moss and plants.
if i had only one moss, i could easily use a counter to let all plants grow at a single blow.
but this is impossible, because there are several moss spots, and each of them has to be activated by seeds.

the best solution for me atm were: the plants access the "touchedBySeed" parameter of the moss which they are touching. (don't know how)

then i thought of putting the moss spots in different categories (or simply name them different). the moss asks in what category it is, or what number is behind their name. if it is activated, it simply increment a counter X (where X is the number of the moss). and the plants look which number X the moss has, that they are touching. so the plants can also access the counter x and start to grow.

it is complicated and maybe a stupid concept, but please help me!

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: access another sprite's properties
« Reply #4 on: 2008-01-26, 08:12:36 AM »
Hi,

I've done something like that in my game, maybe it can help you out. 

Possible solution 1: (no custom code)
You could give the plants a parameter like "parentMossTouchedBySeed".  Then in the layer with the plants and the moss, you can access a specific moss parameter "touchedBySeed" and if it is true, then you can set all this moss's plants parameter "parentMossTouchedBySeed" to true.  From that point, when you have to do a specific action in the plant's rules, rather than checking for the moss's "touchedBySeed" parameter, check for the "parentMossTouchedBySeed" parameter of the plants, you should have no problem doing that.  Of course, you need a lot of rules, but it can be done.

Possible solution 2: (with custom code)
With a custom script you could access the moss's parameter without having to create plan rules.  First step, give each moss a property called something like "mossID" and set these properties to an integer value "0, 1, 2, 3, etc" and just make sure that each moss as a different value.  Then create a property on your plant sprite called something like "parentMossID" and give it the same value that the moss's "mossID" that owns this plant.

Then, create a new custom class and create this function:
Code: [Select]
[Description("Returns the parent moss touchedBySeed property")]
      public static int GetParentMossTouchedBySeed(SpriteBase PlantSprite, int parentMossID)
      {
         int i;
         for (i=0; i < PlantSprite.ParentLayer.m_Sprites.Count - 1; i++)
         {
            if(PlantSprite.ParentLayer.m_Sprites[i].mossID == parentMossID )
            {
               //return the actual value
               return PlantSprite.ParentLayer.m_Sprites[i].touchedBySeed
               //to break the loop
               i = PlantSprite.ParentLayer.m_Sprites.Count;
            }
         }
      }

Oh!  I forgot to tell you.  Your parameters "mossID", "parentMossID" and "TouchedBySeed" have to be created in the SpriteBase.cs file.  You need to create a field and then a property with "get/set".  Of course, all of your sprites will have these properties, but you can ignore them on the sprites which doesn't need them.  Or maybe someone knows a way to access the parameters of a sprite created by the wizard.

Anyway, when this is done, in your plant sprite rules you select your custom function like this:
Create a rule, select "CustomObjects.YourCustomClassName.GetParentMossTouchedBySeed" and pass the two parameters "this" and "parentMossID".  And you have it.

The second solution is more complicated to develop, but you have far less rules to manage after that.

Feel free to ask some questions if it's not explained well enough.  I'll try to make it easier.

Good luck!  :)
« Last Edit: 2008-01-26, 08:16:41 AM by Vincent »
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: access another sprite's properties
« Reply #5 on: 2008-01-26, 09:15:27 AM »
first: thanks!

but i understand some things not really.
would i have to insert the custom code into spritebase.cs?
would you please give me example code for setting such a parameter with Set and Get?

if you don't mind, i would prefer to make this with rules.
i think all i would need is to know how to write a rule in the plant sprite which accesses the parameter X of sprite moss.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: access another sprite's properties
« Reply #6 on: 2008-01-26, 11:53:13 AM »
Hi Morgengrauen,

I don't see any easy way to access sprite "b" property from the rules of a sprite "a".
I have created 2 sample sgdk2 project files (each using one of the solutions I've told you about).
With your permission, I would send these files to you be email. Two .rar files of about 1.5 meg each.

It's your call! ;)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: access another sprite's properties
« Reply #7 on: 2008-01-26, 12:01:25 PM »
yes!!! i send you a pm  ;D

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: access another sprite's properties
« Reply #8 on: 2008-01-26, 08:25:31 PM »
If you have less than 32 moss areas per map, you could maybe just use SetMapFlag and IsMapFlagOn to control up to 31 groups of plants. (You could have a parameter "GroupNum" on the plant sprites and the seed sprites to control which group of plants they are associated with... then it's easy to set it in the map editor.)