How do I implement a function with families

Get help using Construct 2

Post » Tue Sep 30, 2014 8:38 pm

I found a repetition in my code and would like to replace it with a function but haven't had any success yet. As you may have guessed it's a picking issue. I guess I still don't get it. Reading the families page hasn't helped me thus far and I'm hoping it's not a larger organizational issue. The newly created object is a different sprite for each "floor type" but they are all part of the same family - Rooms. Here's a snapshot of the problem area, I left the previous working code as a reference on 47


Image

At the end of a timer, a previously placed sprite is replaced by a different, new sprite, and I'd like the old sprite to pass it's instance variables over to the new one. I'd greatly appreciate your advice
B
10
S
2
Posts: 76
Reputation: 987

Post » Tue Sep 30, 2014 9:22 pm

Unless I misunderstood what you are after, there are not really anything you can do about it in this case. Whether you do it with a function or in the Timer trigger itself you will have to do it for each object, even though these might be part of the same family they ain't the same object meaning that Hotel and Casino are different objects, which you have to set individually. But you can do as you have done with calling a function to set the values as long as they are the same, if you don't want to look at so many actions. However to make your function work, you have change some things:

First you need to pick the ConstructionFloor object as well as the Rooms object.

And you have to change the way you pick to this:
Room Pick instance with UID Function.Param(0)
Since that is holding the UID that you pass to it in the call (Call "SetupRoom" (ConstructionFloor.UID <- This is Function.Param(0) since its the first parameter, the next parameter would be Function.Param(1) that's just the way it is, as C2 doesn't allow you to rename them.)

So in the Function SetupRoom you add these two lines instead of the one you have:
Room Pick instance with UID Function.Param(0)
ConstructionFloor Pick instance with UID Function.Param(0)


Then the function knows which elements you want to work with, and that should solve it for you.

The way you call the function itself is fine, you don't need to change that.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Tue Sep 30, 2014 10:11 pm

Thank you for the detailed answer. I attempted your suggestion to no affect. I don't expect a solution but I am curious if I followed your direction correctly. I tried a few variations that yielded no results but they are basically the same. It's not a big deal to go with my original code, I just thought I could refine it a bit
Image
B
10
S
2
Posts: 76
Reputation: 987

Post » Tue Sep 30, 2014 11:16 pm

Without the full code its hard to judge as I don't know exactly what your objects are, so its ofc a bit of a guessing :D

However it seems that you don't need "Rooms Roomtype>= 3" as you don't use it for anything, unless something requires it to be more or equal to 3?
There are some things you need to check:
1. You have to make sure that you don't have several ConstructionFloors that are equal to 3, otherwise you cant be sure what you pass to the function.

2. Is ConstructionFloor also part of Rooms family? if not then you cant use Param(0) to select it? In that case if Casino is part of the family, you can pass its UID as Param(1) and then select the Rooms using that instead.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Tue Sep 30, 2014 11:45 pm

To further complicate things, C2 doesn't let you pick a newly created object until the next top level event, so that's getting in your way too. One trick is to put a "Wait 0" before your function call. This will suspend the call until the end of the event sheet and call it then, allowing the pick.
ImageImageImage
B
71
S
21
G
220
Posts: 3,646
Reputation: 123,721

Post » Wed Oct 01, 2014 12:02 am

blackhornet wrote:To further complicate things, C2 doesn't let you pick a newly created object until the next top level event, so that's getting in your way too. One trick is to put a "Wait 0" before your function call. This will suspend the call until the end of the event sheet and call it then, allowing the pick.

There shouldn't be any problems in this case, as its within the same scoop so passing the Casino object and picking the family object with it will work fine here.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Wed Oct 01, 2014 12:10 am

Furthermore, he's picking his instances with "Pick by UID", which does not suffer from the infamous "newly created object" issue.
B
67
S
27
G
24
Posts: 969
Reputation: 18,573

Post » Sat Oct 04, 2014 12:33 am

For anyone who may stumble upon this later and has a similar problem. I think a much simpler solution would be to use one sprite object for every room by distinguishing the different rooms via different animations within that same sprite object. For example rooms.sprite contains animations for Casino, Hotel, etc. This way you can detect room type via the animation and use a function easier since you are only working with one object. Haven't implemented it yet but hope that makes sense.
B
10
S
2
Posts: 76
Reputation: 987


Return to How do I....?

Who is online

Users browsing this forum: jaytrott86 and 8 guests