How do I acces an instance variable from family

Just started using Construct 2? Post your questions here

Post » Wed Oct 08, 2014 1:25 am

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…

3c4768f586547c505a95702000e0d5df.png


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
You do not have the required permissions to view the files attached to this post.
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Wed Oct 08, 2014 9:54 am

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.

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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Wed Oct 08, 2014 10:55 am

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
You do not have the required permissions to view the files attached to this post.
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Wed Oct 08, 2014 2:23 pm

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?
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Wed Oct 08, 2014 4:40 pm

j0schi wrote:Hey nimos100, Thank you so much for helping me out here :)

Np :D

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.

"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?

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.

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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Wed Oct 08, 2014 5:52 pm

Hey nimos,

thank you so much for your time! can i buy you a beer? (donation button)?
actually this was the question:
but variables added to specific objects are not available to family object

the description and description1 variables where only for testing purpose.

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...

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...


0f95eed7dc79a4536081f48df499a966.png


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
You do not have the required permissions to view the files attached to this post.
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Wed Oct 08, 2014 6:59 pm

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?

..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:
Image
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:
Image
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:
Image
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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Wed Oct 08, 2014 7:50 pm

well, this is way better than my aproach, which was:
86ac4aea7f3f9edd2f837675d42662ce.png

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 :D

i am shure this wont be my last question i have to ask here :) tho the offer with the beer is still here :)
You do not have the required permissions to view the files attached to this post.
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Wed Oct 08, 2014 8:19 pm

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
B
19
S
7
G
1
Posts: 222
Reputation: 2,546

Post » Wed Oct 08, 2014 8:31 pm

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 :D

That's good :D

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

i am shure this wont be my last question i have to ask here :) tho the offer with the beer is still here :)

I might cash in on that beer some day :P
Last edited by nimos100 on Wed Oct 08, 2014 8:36 pm, edited 1 time in total.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: vslk and 2 guests