Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411626 Posts in 69391 Topics- by 58447 Members - Latest Member: sinsofsven

May 11, 2024, 11:11:00 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)How to make a parent object read child variables?
Pages: [1]
Print
Author Topic: How to make a parent object read child variables?  (Read 2695 times)
Ninja Dodo
Level 4
****



View Profile WWW
« on: January 05, 2010, 12:51:24 PM »

I'm somewhat of a beginner with Game Maker and I'm just starting to use parent objects for differentiating types of objects and such, but I'm slightly stuck on the following:

Object ENEMY collides with object PLAYER1 or PLAYER2 triggering a collision event in their shared parent PLAYERS.

How do I access the variables of the object involved in the collision from the collision event in the parent? How do I know if it was PLAYER1 or PLAYER2 that did the colliding? Each have individual properties that should affect what happens after a collision...

I mean, I know I could create individual collision events for both but that would make it less flexible and defeat the purpose of using a parent object...

Help file only seems to mention inheriting children but nothing for going the other way. There is instance_id but since these two are not instances of the same object I guess that's a dead end. Any help would be greatly appreciated.
« Last Edit: January 05, 2010, 01:59:45 PM by Ninja Dodo » Logged

Kadoba
Level 3
***



View Profile
« Reply #1 on: January 05, 2010, 02:09:21 PM »

 - You can use the other keyword to get the other object in the collision event. For example if you wanted to set the x position of the player to the x position of the enemy object for whatever reason you could do this:

Player-Enemy collision event:
x = other.x

 - You can create a common variable in all of the players' creation events that specifies what type of player it is.

PLAYERS creation event:
type = "PLAYERS"

PLAYER1 creation event:
type = "PLAYER1"

PLAYER2 creation event:
type = "PLAYER2"

Then on a collision event you can check type to see what type of object it is.
if(type == "PLAYER1")
 //do stuff
if(type == "PLAYER2")
 //do other stuff

Okay. Now that I've shown you what you wanted to do I have to ask why don't you just create different collision events for PLAYER1 and PLAYER2? I can't think of a need to have a common collision event if they're going to be handled differently.
Logged
Kadoba
Level 3
***



View Profile
« Reply #2 on: January 05, 2010, 02:48:54 PM »

Okay, you added a bit to your first post. You say you don't want to seperate the collision events because you lose flexibility. You won't lose flexibility, in fact you'll retain it by not creating a co-dependency. You can still use the parent's collision event. All you have to do is use the "Call Event" action inside of your child object which will call the parent's collision event. The idea of inheritance (also called polymorphism) is that you put all of the common code shared between all child objects inside of the parent object and all the specialized code inside of your child objects. Your parent objects should never, ever know about their children which was what you're trying to do. Trust me - that will really make your code inflexible.
Logged
Ninja Dodo
Level 4
****



View Profile WWW
« Reply #3 on: January 05, 2010, 03:37:47 PM »

Ah, okay. I think I understand. Will try this. Thanks!

I guess I was thinking it would be more flexible because I could add another player without having to connect it specifically to other objects, only parent it to the PLAYERS object, but my assumptions may be faulty.

The collision example is one case, another is wanting to detect whether an enemy can see a player and whether that specific player is hidden under a "shadow object" or not (each player has a "safe" variable set to 0 or 1). The way I'm doing it at the moment is messy and only works for ONE enemy object. I think the "type" thing should do the trick, though...
Logged

Matt Thorson
Level 7
**

c'est la vie


View Profile WWW
« Reply #4 on: January 05, 2010, 06:44:54 PM »

You can also check object_index.

Say PLAYER1 is obj_player1 and PLAYER2 is obj_player2:
Code:
if (object_index == obj_player1)
    //...
else if (object_index == obj_player2)
    //...
Logged

Ninja Dodo
Level 4
****



View Profile WWW
« Reply #5 on: January 07, 2010, 05:19:49 AM »

Cool, thanks!
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic