How do I acces an instance variable from family

0 favourites
  • 13 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hi all

    I am just starting out with construct 2 and try to understand everything

    So far everything works pretty well, but now I am stuck at some point and just can’t figure it out myself.

    Here is a link to better understand where I am stuck…

    [attachment=0:38hj0w7t][/attachment:38hj0w7t]

    In general what I am missing is some kind of a database or global values for objects which are easy to set…

    I am trying to build a game which has many items. Naturally each item has different values. At the beginning I try to keep it simple, so there are the objects “stations, asteroids, different ships”

    All those objects have some variables likle “Name, Shield, Mass, Max Speed” which I want to display on a HUD. So here are my Questions:

    1) Is there a way to easily insert all those values for all objects in a table style or do I have to create every instance variable for every object manually even if it is from the same type? I tried to copy paste them, but with no luck.

    2) As you can see I have grouped the objects in a family. Now that I want to show the description I must select the instance variable from the object which is selected. But I can only access the instance variables of the FAMILY how can I get the variables from the selected object?

    Am I doing something wrong or is my whole approach to do this wrong? The select and target the selected object works pretty nice… so I would like if I could get the description to work. Also a database style workflow would save me so much work creating all those instance variables.

    maybe i need some kind of "global instance variable", a variable which is added automatically to every object in a famiyl...

    Your help is highly appreciated!

    Regards Patrick

  • [quote:1ivt8s2f]1) Is there a way to easily insert all those values for all objects in a table style or do I have to create every instance variable for every object manually even if it is from the same type? I tried to copy paste them, but with no luck.

    Its a bit hard to see what you want to do on the screenshot, but if I understood you correct.

    If the values you want to set are individual for the objects then you will have to do it for each of them. However you can do it using a "for each" or some other loop to do it faster.

    But can you try to show a screenshot of what exactly you are trying to do, because what im saying might not be what you are after.

    [quote:1ivt8s2f]2) As you can see I have grouped the objects in a family. Now that I want to show the description I must select the instance variable from the object which is selected. But I can only access the instance variables of the FAMILY how can I get the variables from the selected object?

    Am I doing something wrong or is my whole approach to do this wrong? The select and target the selected object works pretty nice… so I would like if I could get the description to work. Also a database style workflow would save me so much work creating all those instance variables.

    The family variable will work fine, it is copied to all the objects in the family. So when you select a specific family object and set its variable to something the object will also get this value.

    In the Select & Info.

    You don't have to store the UID of the RadarObject in a local variable same goes with the description. As you have already selected one with the "On tap" so if you did a "Set description = "This is a description" only the selected object would get this description.

  • Hey nimos100, Thank you so much for helping me out here

    1) yes, the values are different for each object but each object has the same variables... so i thought there is an easy way to just say give all those objects the instance variable "description, shield, firepower" and then enter each value manually without having to create each instance variable for each object manually as they are aleways the same.

    2) got that but actually i will show the description from the object in the hud... but i cant get the variable "description" from the object, just from the family

    you can see the game here: bytecrunch [dot] org/th/

    as you can see when you select an object it shows the description, but i cant set it to the specific objects description variable, just to the familys one...

    as you can see on the screenshot each object (and i am planning on adding a large number) has several values... but somehow i cant access them from the "family"... therefore i dont know how to display the right description

    also i want to add a menu with different menu options for each object, but maybe its better i make a if and determine the object type...

    regards

    joschi

  • Maybe i can answer my own question... it seems to be not possible, i should use some kind of 2D array, as the dictionary only supports 1D... am i right?

  • Hey nimos100, Thank you so much for helping me out here

    Np

    [quote:2q5k0x76]1) yes, the values are different for each object but each object has the same variables... so i thought there is an easy way to just say give all those objects the instance variable "description, shield, firepower" and then enter each value manually without having to create each instance variable for each object manually as they are aleways the same.

    If you create an object lets say "Asteroid" and you give it a variable, such as "Asteroid type" this is added for all asteroid objects and can be set at design time to something default lets say "Iron ore".

    Whenever you create an asteroid it will by default be this type.

    If you add the asteroid object to a family, this variable will not be available if you use the family to select the asteroid object.

    And from your screenshot it seems that this is what you are trying to do.

    [quote:2q5k0x76]"Set text to RadarObjects.description1"

    But looking at the asteroid instance variables there are nothing called description1, but something called description. Which I assume is the one you would like to use?

    Variables that are added to a family is shared by all objects in that family, but variables added to specific objects are not available to family objects, even though they are part of the same family. Which makes sense because if you besides having an asteroid in your family also had a spaceship, it wouldn't make much sense if this had an "Asteroid type".

    So to me it seems like you have mixed up family variables and specific object variables. So to fix It you have to use only the description from the RadarObjects as this is shared amongst all the objects in the family and then remove the description from the specific objects.

    Then you will also be able to give all objects that are the same in the family a default value, so for instant if "Asteroid" and "Spaceship" are in the same family and you have a description variable. You can in design view set Asteroid description = "This is an asteroid" and for the spaceships set it to "This is a spaceship" and this will be the default value for all newly created spaceships or asteroids.

    Hope that makes sense, know it can be a bit confusing?

    [quote:2q5k0x76]2) got that but actually i will show the description from the object in the hud... but i cant get the variable "description" from the object, just from the family ...

    Pretty sure this is due to the above problem.

    [quote:2q5k0x76]Maybe i can answer my own question... it seems to be not possible, i should use some kind of 2D array, as the dictionary only supports 1D... am i right?

    Dictionaries are 1D, but I don't think you should jump into arrays as they will not solve the problem and is not needed in this case anyway.

    As I see it you mix up what different types of objects are and therefore might be confusing yourself a bit

    If you have a "Man object" and a "Women object" these are not the same obvious, if you add both of them to a family called "Humans" they are still different object, however now you added a family object as well, and this object is also a different object than the Man and Women objects as its a Human object in this case. So in theory you have 3 objects.

    The benefit of the family object is that each member of the family inheritance the family variables and can make use of them. So in this case whether you are a man or a women you have an age. So you can add an "Age" variable to the Human object and then both the man and women object can make use of it. Using a family also allows you to work on all members of it. So if you wanted to select all humans that are 20 years old. It will select all humans, men and womens a like that are 20 years old, instead of you first having to select all women that are 20 years old and then all men as well. However a women can be pregnant but a man cant, so it wouldn't make much sense to add a variable to Humans called "Is_pregnant" for instant so this you could add to the Women object instead, and then its only available to Women.

  • Hey nimos,

    thank you so much for your time! can i buy you a beer? (donation button)?

    actually this was the question: [quote:3exp0x9i] but variables added to specific objects are not available to family object

    the description and description1 variables where only for testing purpose.

    [quote:3exp0x9i]So to fix It you have to use only the description from the RadarObjects as this is shared amongst all the objects in the family and then remove the description from the specific objects.

    This wouldnt make sence since every object has its own discription...

    [quote:3exp0x9i]You can in design view set Asteroid description = "This is an asteroid" and for the spaceships set it to "This is a spaceship" and this will be the default value for all newly created spaceships or asteroids.

    i have done this, and this is what i want to archive. the goal was to do that in a single select action (see screenshot) and not make a new action for every object family...

    but maybe that also would make sence... than stations could be green, spaceships red and asteroids gray. so i have to devide it by familys and make an own radar and select action for each family. that would leave me with the problem that later there are differen spaceships, so even if i have a family for spaceships the description is never the same... (i am thinking while writing, sorry) so i have to find a way to display the discription of a specific selected object. i have to rethink my aproach here...

    [attachment=0:3exp0x9i][/attachment:3exp0x9i]

    as you can see on the screnshot i am trying now to get the instance variable "description" by UID of the object. is there a way to select the value of an instance variable by UID?

    seem logic to me as i can select the UID from the clicked object...

    regards and thanks again!

    joschi

  • [quote:31332ceb]This wouldnt make sence since every object has its own discription...

    Yes but you want to be able to set and get the description right?

    [quote:31332ceb]..so even if i have a family for spaceships the description is never the same... (i am thinking while writing, sorry) so i have to find a way to display the discription of a specific selected object. i have to rethink my aproach here...

    Here is an simple example of how to do it.

    Each square illustrate a spaceship of different type, called "Spaceship_1", "Spaceship_2", "Spaceship_3". All of them are also part of a family called "Spaceships".

    This family have 2 Variables:

    "Description" (By default this is empty)

    "Type" (The purple square is Spaceship 1, Green Spaceship 2, Red Spaceship 3)

    To set an individual description for each of the spaceships I can do the following:

    Depending on the type of spaceship I click a description is written. So if I click spaceship 1 it will set the description for that spaceship to "This is spaceship 1" and so on.

    Since I have added the variable to the family I can access it for all the spaceships and set it individually for each of them.

    If I wanted to set it for all of them at once I could do it like this:

    In this case whenever I press a spaceship, I don't care which type it is and just make it so all spaceships get the description "This is a spaceship".

    Again this is because I added all the individual spaceships to the same family. So as you can see there are no problems selecting a specific spaceship or all of them at the same time.

    Also as mentioned earlier because I have added the variables to the family and therefore they are inheritance by the members, I can still access them through the members. So this example will also work without any problems:

    The purple square doesn't have a variable added to it called "Description" besides the one that comes from the family.

    So regardless of how I choose to work with the objects or the family I have access to the variables and can manipulate them whatever way I want.

  • well, this is way better than my aproach, which was:

    [attachment=0:2aetgq5q][/attachment:2aetgq5q]

    still not perfekt, but i can work with this for now, thank you very much

    i am missing something like a simple mysql database... i would like to pull those values from a database as i plan to implement much different objects.

    i have a basic knowledge of mysql and php. so my longterm goal is to make an multiplayer game with an ongoing economy... but that is far from now

    i will look into that in a few weeks

    i am shure this wont be my last question i have to ask here

  • it took me a while to exactly understand what you wrote... and on second thought i have to go with my aproach. i want to use the instance variables, as i need more values than just the description. and it seems to be "cleaner" for me if i dont define all that over the actions but in the instance variable for the objet itself

    let me know if i am wrong here...

    regards

    patrick

  • [quote:11ug9sfx]still not perfekt, but i can work with this for now, thank you very much

    i am missing something like a simple mysql database... i would like to pull those values from a database as i plan to implement much different objects.

    i have a basic knowledge of mysql and php. so my longterm goal is to make an multiplayer game with an ongoing economy... but that is far from now

    i will look into that in a few weeks

    That's good

    However I would strongly suggest that you read this part in the manual about families, before you continue to much. Especially if you plan on making a big game, without understanding this correctly you will eventually loose control of the project and run into some really weird problems, that will be very time consuming and difficult to fix.

    From your screenshot and being satisfied with the solution you got its pretty obvious that you didn't really understood what I tried to explain. As you would need to add one line to the "On tap gesture on RadarObjects" to make it work for all the objects, if your setup was correct.

    To give you an example as of now you have added 3 separate "On touch" which basically does exactly the same thing. Now imagine when you expand your game to maybe 25 different objects, then you would need 25 of these events, but also you would need it for all other event that would involve these objects. So soon you will spend so much time copy/pasting and correcting stuff, that you will hardly make any progress on the actual game. Trust me I did it the same way as you did when I started, so to save you some time I again would strongly suggest you to read and really understand how families works

    Families

    https://www.scirra.com/manual/142/families

    [quote:11ug9sfx]

    i am shure this wont be my last question i have to ask here

    I might cash in on that beer some day

  • it took me a while to exactly understand what you wrote... and on second thought i have to go with my aproach. i want to use the instance variables, as i need more values than just the description. and it seems to be "cleaner" for me if i dont define all that over the actions but in the instance variable for the objet itself

    let me know if i am wrong here...

    regards

    patrick

    Think my last post answered it

  • ive already read this article 2 times, maybe i have to read it again hahah

    ive got your point, but i have to set the instance variables to the ojects or not?

    maybe the next screenshot makes it clearer what i want to archive:

    [attachment=0:ydt3lnlv][/attachment:ydt3lnlv]

    on the other hand i ran into the same problem with the enemy health... each enemy has a certain amount of helth when he spans. all enemys are in the family pirates

    now if the enemy gets a hit i have to subtract one of the health of this enemy... man that really gets complicated, i will look at your example again and hope i get it.

    if or if not i will report back tomorrow... normaly i cant sleep when i have something like this in my mind haha

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • hah, i think i got it know! maybe i am an idiot, but here is the point where ive got confused:

    [quote:n2acthbj]If you add an instance variable to a family, all the object types in the family inherit the instance variable. For example, adding the instance variable health to the family Enemies in the above example will mean BladeEnemy, BugEnemy, CrescentEnemy, FighterEnemy, SaucerEnemy, ScytheEnemy and SlicerEnemy all gain a health instance variable. It will also appear in the editor alongside each object's own instance variables.

    As you can see the object astroid_iron is in the family RadarObjects. But in the editor it has neither of the instance variables from RadarObjects.

    [attachment=0:n2acthbj][/attachment:n2acthbj]

    so i assumed that the instancevariable from the family doest count for every single object, but just one time for the whole family. and if i set it to another value all values get changed for every object. if it count for each object in a single way and is editable for each single object seperatly it does exactly what i want it to do

    Is this an roor in the discription, a bug or am i still missing something?

    found it: on the propertys on the left site of construct it shows up... man thank you so much again

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)