objects as functions parameter

Get help using Construct 2

Post » Thu Nov 28, 2013 7:05 am

Hello,
is any way to use objects or families within function parameters?

the idea is to create a function that sets up the angle of motion and speed attack with different families objects as enemies (aliens, planes, dragons, etc...).

I know i can create the object inside the function body, but that limits the function to the specific object or family i created within. Of course, one way is to create the objects outside the function and use the function to only set up the attack movement values.

Just wondering :)
B
3
Posts: 6
Reputation: 187

Post » Thu Nov 28, 2013 7:17 am

I'm not sure exactly what you mean, but I'm quite sue it can be done ;)

Normally you would do something like:

On enemy destroyed: call function "enemyDestroyed" with the first parameter as the enemy.UID.

Then in the function, pick enemy by UID and you can do whatever you like from there. Hope it helps! Feel free to ask if you have trouble.
B
28
S
8
G
5
Posts: 429
Reputation: 6,997

Post » Thu Nov 28, 2013 8:26 am

Hello Tobey,

Thanks for such a quick answer :). I was thinking more on being able to use the create object event with a function parameter. Something like this:

Let's say we have 2 families of sprite objects. FlyDragons and GroundDragons, and we want to use a function to create every 3 seconds an enemy of any of those families in the screen.

Every 3 seconds: call function "newEnemy"(family, screen.X, screen.Y)

:)Perhaps (just come to my mind) the approach is using a random variable value for the first parameter and in the body of the function create the object depending on that value.
borparo2013-11-28 08:37:39
B
3
Posts: 6
Reputation: 187

Post » Thu Nov 28, 2013 8:38 am

The question is really unclear.
If you want the function to work on a specific instance it should be picked.
If the object is created within the function it can be referenced by using subevents.
If after the creation you want to do things to each object in the family, you can add a for each.

Point is, you want to sent the family or object as a parameter. To do this you'd have to know which object or family to use as parameter, right? So actually you have to do the picking before being able to set as parameter. Which is outside of the function, which makes your question questionable.

You could just choose to send a global variable as a parameter and within the function have a subevent that says if global variable is ... perform these actions. these actions could be family or object specific, ofcourse.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Thu Nov 28, 2013 8:41 am

Just saw your reply. I would probably go for the easy approach.

You could use choose(1,2)
and create family 1 if choose is 1
and create family 2 if choose is 2
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Thu Nov 28, 2013 9:05 am

Thanks LittleStain. Definetively going with the variable method :)
B
3
Posts: 6
Reputation: 187

Post » Thu Nov 28, 2013 9:19 am

You could discard the global variable actually:

call function"enemycreate"(choose(1,2),screen.x,screen.y)

on function"enemycreate"
-compare two values : param(0)=1 - family1 actions
-compare two values : param(0)=2 - family2 actions
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Thu Nov 28, 2013 3:46 pm

Thanks again. Now i need more enemies! :)
B
3
Posts: 6
Reputation: 187


Return to How do I....?

Who is online

Users browsing this forum: ivanski1983, SturmIV and 6 guests