count no of instance of object

SANDEEP410SANDEEP410 Member Posts: 46
edited May 2012 in Working with GS (Mac)
How to to get the number of instance of an object
Also in my scene i want my game to progress to next level if all enemys are dead.So where should i put my conditions,in which actor

Best Answer

  • MotherHooseMotherHoose Posts: 2,456
    Accepted Answer
    in any scene: the actors you place in the scene and their rules/behaviors only run in that scene
    … each time a scene is loaded …it starts exactly as you programmed it

    of course: the current state/condition of gameAttributes
    … if used in actor's rules to trigger events/behaviors … are in effect

    ==
    in your scenario … create a small controlActor
    … drag in to scene
    … then drag offScreen out of the camera area (the large white-lines rectangle)
    … unlock it
    … Create a rule
    Rule: when (All)
    Attribute: expression-select Current scene Name is (the name of that scene)
    Attribute: game.enemyCount ≤ type in a number
    --changeScene

    image MH

Answers

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    How are your enemies placed in the scene? Are you prepacing them or spawning them?
  • SANDEEP410SANDEEP410 Member Posts: 46
    They are all placed by me
  • AppTekAppTek Member Posts: 152
    Put a change attribute in the enemy saying to add 1 to the number of enemies attribute. Then in the code where you destroy or renew the enemy, put a change attribute saying to subtract one from the number of enemies attribute.
  • SANDEEP410SANDEEP410 Member Posts: 46
    Well thats not the poper way to do it.
    what if i wana find out, if instance of a particular enemy currently exist in the screen.
  • AppTekAppTek Member Posts: 152
    edited May 2012
    Here, I made you a demo.
    www.apptekgames.com/enemy.zip
  • AppTekAppTek Member Posts: 152
    If you want to find the instances of a lot of objects, then make the attribute for each one. Then put a change behavior in the object, doing the same thing before, but instead for the new attribute.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    I'm not really sure how you would keep track of specific enemies without writeable tables, but I have two ideas:

    1. If you only need to know whether an enemy has "died," give each enemy a unique self.ID (index) and have a rule that says When [enemy dies] Change Attribute game.deadList (text) to game.deadList..self.ID. Then have another actor offscreen that has a rule When attribute game.deadList contains [actor ID you want to check] then [that actor is dead].

    2. If you have a small number of enemies, you can give each one a boolean attribute self.dead and then unlock a separate offscreen actor and set up rules for When attribute Current Scene-->layers-->[layer name]-->[instance name] is true then [that actor is dead]. You can even rename your instances so that you can tell which one is which.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • MotherHooseMotherHoose Member Posts: 2,456
    Great Demo @AppTek … left a comment on your site.

    @SANDEEP410 … to monitor an actor's condition in the scene …
    you need to uniquely name each enemy
    … then add gameAttributes … boolean type … for their condition … active/dead

    to show the state of those attributes … use controlActors or displayTextBehaviors

    ==
    or just before the enemy is destroyed change its Alpha to 0
    and have a controlActors check/react to the Alpha of each enemy
    think you are going to have learn/work with GS
    … to get an understanding of the many proper ways to do things

    use the demo from AppTek … to get started with this

    image MH
  • SANDEEP410SANDEEP410 Member Posts: 46
    Above comments all are confusing to me
    Anyway Lets say i have a turret that automatically shoots if a spider appears in the scene.
    Now how can i determine if a instance of spider exists in the scene or not.
  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    @sandeep410 you would use the same technique as described above. please stop rejecting answers just because you don't understand them. You need to have a change attribute adding to the count when the actor loads and a subtracts when it's destroyed.

    Please don't take this tge wrong way but this is a very basic task. If you are having trouble understanding this then you need to take a step back and work on the basics of gamesalad. Watch the cookbook videos and actually create everything the videos create. Your games quality will improve greatly overall if you take the time to Learn the tool really well. Before diving into a project you intend to publish.

    Cheers
  • SANDEEP410SANDEEP410 Member Posts: 46
    Ok i was able to acheive it
    I have one more question I want my game to move to next lvl if specific type of enemys are dead.
    So is there any way such that a rule will only work for a particular scene.Since i have different type of enemys for diff scene.
  • acajicacajic Member Posts: 1
    edited September 2013
    I keep track of number of alive instances of certain actor. I decrement that counter everytime an instance is destroyed. If two or more instances are destroyed very quickly, one right after the other, the counter is decremented only once instead of multiple times.

    I have 50 balloons in a scene. I shoot them one at a time but very quickly so all of them are destroyed within 10 seconds. I log into debugger the number of alive instances every time an instance is destroyed. After I shoot all 50 balloons, I check in debugger and see that the counter went 50->49->48->...->16->15.

    Is there a way to guarantee that a certain behaviour action will be executed but doesn't need to be executed immediately?
Sign In or Register to comment.