Container type functionality for families?

For questions about using Classic.

Post » Sat Dec 10, 2011 10:27 am

So I have a family that contains a variety of sprites (like hundreds). I would like to store some values for each sprite in an array instead of using PVs. I was wondering if I could do something like when you use containers to create objects (in this case arrays) every time an instance of one of the sprites in the family is created or when a sprite is added to the family. The reason for this is because using PVs would be difficult since there will be a lot of values that need storing and would be tough to access one at a time through Family.Value('Var') statements and I'd rather use looping and such or checking specific indices. Juryiel2011-12-10 10:35:02
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sat Dec 10, 2011 11:55 am

R0j0hound was using arrays in a container there ->

http://www.scirra.com/forum/record-the-player-and-create-a-clone-who-repeat-action_topic42771.html

Maybe it helps you?
B
24
S
9
G
2
Posts: 294
Reputation: 3,160

Post » Sat Dec 10, 2011 8:57 pm

Right, that's what I want in terms of functionality but instead of doing it for objects (e.g. a container for two objects) I wanted to do something where any object that belongs to a particular family has an array associated with it.

The reason is that there will be, say 200 objects in that family (not instances but different objects, which will be sprites), that each need to be in a container with an array. Then throughout the game I will spawn instances of these sprite objects which would create an array to go with them.

I can brute-force it so that every time I create one of the family objects I also create an array with the name of the object, and put them in a container, but I foresee issues in forgetting to do that and getting burried under a ton of arrays, but also more importantly, when I do the code, since each object will have a different array (as opposed to instances of the same array), I'll have to have a separate block of code to process each object's array.

E.g.
OBJ1 and OBJ2 are in a family called OBJFAM. ArrOBJ1 and ArrOBJ2 are their respective arrays in which they're in a container with

When I spawn instances of OBJ1, an ArrOBJ1 would be created, and for OBJ2 an ArrOBJ2 would be created. But when I do the code, how could I then reference the correct array such that:

Create some number of OBJ1 and OBJ2 instances
Pick: OBJFAM instance by Some method
Read and write to the array for the instance of OBJFAM picked.


If I picked an instance of OBJ1 instead of an instance of OBJFAM, I would know that the code would always reference ArrOBJ1 and the container would pick the correct instance of ArrOBJ1 that went along with the correct instance of OBJ1 picked. But not sure how I would do that with picking the objects through their family association.
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sat Dec 10, 2011 9:36 pm

For custom classes, there are much more elegant solutions. One of them is using Python, or, if you want to avoid the overhead, the fantastic (but complex) plugin 's' by lucid.

Here is the link to the thread, but please make sure you download the newest version (somewhere on the last pages) AND read the complete first 3 to 4 pages to get an overview on how 's' works and what you have to take care of:
http://www.scirra.com/forum/s-update-as-of-4-12-11_topic38456_page1.html?KW=plugin+lucid
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat Dec 10, 2011 11:38 pm

Hmm I looked at the plugin and read the descriptions and a good part of the thread but I'm not sure how to do what I want with it. So I create the various sprite objects in a layout for storing objects, and put them in the correct family. Then in the game I spawn copies of them via Create Object.

So at creation time I would also create an entry in S? Is that the basic idea? If it is, what does it buy me over just using a 3D array such that one index is the Object.UID, the second dimension is the variable name, and the third is the value?
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sun Dec 11, 2011 11:03 am

[QUOTE=Juryiel] ...what does it buy me over just using a 3D array such that one index is the Object.UID, the second dimension is the variable name, and the third is the value? [/QUOTE]
That's not the way, arrays work. An array is just a line of cells, and you access those cells by using coordinates. Whatever count of coordinates you have, it leads to exactly one cell.




So, to access the data you need to do something like this: var = array(2, 1, 4)
To store a value it is: array(2, 1, 4) = "one cell"


(Just pseudo code in the following text)

The basic idea of object oriented programming is to get rid of these limitations. With s, for example, you create a prototype of your very own data structure, a so called template:

"lifeform"
- 'visual' as objecttype, default mySprite
- 'hp' as number, default 100
- 'speed' as number, default 150
- 'name' as string, default "Lion"
- 'strength' as number, default 200

"animals"
- 'animal' as super, default "lifeform"

Adding as many 'animal' to "animals" as you like, you can access them and their properties in various ways, e.g. Textbox.Text = "animals".'animal'(1).'name'

You can go as deep in this tree, as you want to. For example, you could have another super "world", where you store "animals" among others. But still, you access everything as easy as Textbox.Text = "world"."animals"(2).'animal'(1).'name'

You would also do the complete managing with s, it is not just an array of data. You can create objects, load and save every bit of data, including spatial data from your objects, and so on.

There is a lot more, but as I said, 's' is quite complex, and you would need to be familiar with object oriented programming, but that is true for everything, which should work the way you want to have it working.

You can try to make it work without Python (an object oriented programming language) or s, but it will be a pain, with a lot of workarounds involved.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sun Dec 11, 2011 7:47 pm

Thanks for taking time to explain. I do understand how arrays and superstructures work in general. Sorry, my description is not quite clear for the array I had in mind. By 'variable' I meant a 'slot' for something like an effect, and by 'value' I meant different properties associated with that effect. What I meant was this example, where z is the UID, y is the 'slot' number, and x is the various properties of that slot like name and value (minus UID since I'm also storing it in the 'slot' since UID numbers assigned to objects are not under my control so I can't convert them to an index directly without using a Hash table)

3D array that stores values in this form, with each of the matrices below being 2D slices of said array:

________________________________
|UID1     Name1     Value1     |
|UID1     Name2     Value1     |
|UID1     Name3     Value1     |
________________________________
________________________________
|UID2     Name1     Value1     |
|UID2     Name2     Value1     |
|UID2     Name3     Value1     |
________________________________


Then I can just search the array by UID for the correct slice or use a hash table of UIDs and Array indices. Not as elegant as using a structure, clearly.

The main issue is that every time an instance of an object is created at runtime, I have to manually do code to insert it into this array, and in addition, for objects created in the editor, I have to run a function that loops through those at the start of a layout and creates their array slices. Every time it is destroyed, I have to do code to remove it from the array. Oh the other hand, with a container as mentioned in my first post, since each object would have its own array that would be created and destroyed, that would be automatic.

So I guess instead of asking "How" to do it with S (I'll figure that out) let me just ask, is it possible to take a family of objects, and every time I create an instance of one of the objects in that family either in the layout editor or at runtime, the appropriate data storage structure is created with S? (Note that just replacing families with S for everything is not ideal since I already have a ton of events that use family functionality that I'd rather not replace. However, putting all the objects in said family also in an S structure and doing it would be just fine). Juryiel2011-12-11 20:02:16
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Mon Dec 12, 2011 12:45 am

[QUOTE=Juryiel] So I guess instead of asking "How" to do it with S (I'll figure that out) let me just ask, is it possible to take a family of objects, and every time I create an instance of one of the objects in that family either in the layout editor or at runtime, the appropriate data storage structure is created with S? (Note that just replacing families with S for everything is not ideal since I already have a ton of events that use family functionality that I'd rather not replace. However, putting all the objects in said family also in an S structure and doing it would be just fine). [/QUOTE]
Well, without going too deep: You can.

If you created your template of the data structure and a super that stores arrays of them, it is very easy:

+ some trigger
-> create object 'someobject'
-> super, add array from template 'somestructure'
-> super, lastarray, id = someobject.oid

(or just store the object itself, which is more comfortable, as you can then pick it later from within s, without any trigger or condition needed)

+ some trigger
-> destroy object 'someobject'
-> super, loop through arrays, until id = someobject.oid
-> super, delete array

You may also want to have a look at Python. It is also very easy to realize it. You'd create your data structure as a class or a dictionary on startup and just store copies of it in a list or other container, together with the object reference, whenever it is created.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Mon Dec 12, 2011 1:16 am

Oh I see, thanks for explaining that. So it basically makes teh UID indexing and referencing the correct structure easier, but I still have to remember to create the data structure at runtime myself. Seems like it will do half of the functionality I want, which is better than none. I'll give it a shot. Juryiel2011-12-12 01:16:41
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Mon Dec 12, 2011 1:30 am

If you are afraid that you could forget to create it, just make a function and call it instead of creating object and data structure in the actual event.

+on function "create"
-> create object
-> add array

+ some trigger
-> "create"

tulamide2011-12-12 01:31:35
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 9 guests