Jump to content


Photo

Parent of Parent switching? (or better concept)


  • Please log in to reply
4 replies to this topic

#1 C_Pike

C_Pike

    GMC Member

  • GMC Member
  • 618 posts
  • Version:GM8.1

Posted 29 April 2012 - 02:56 PM

To begin, this is how my game is currently structured:
            obj_shipParent
            |       |
    obj_enemy       obj_ally----+
        |               |       |
    obj_enemyA     obj_player  obj_allyAi
        |                           |
    obj_enemyShip               obj_AllyShip

That makes it tedious to code as obj_enemyAi and obj_allyAi are the very same (with a difference in whom it will attack).
Naturally, I want to make something more like:
            obj_shipParent
            |       |
    obj_enemy       obj_ally
           \            |       
            obj_ai      obj_player 
               |                           
            obj_Ship              
In this case, obj_ai is switchable.
When the obj_ship is created it is by default branching to obj_enemy.I would like the program sets 'obj_Ship's' parent, 'obj_ai' to have the parent 'obj_ally'.
            obj_shipParent
            |       |
    obj_enemy       obj_ally
                    /    |       
            obj_ai      obj_player 
               |                           
            obj_Ship              
---------------------
I have tried some convoluted code:
    with (instance_create(destination_x+16,destination_y+16,obj_ship))
        {
        object_set_parent(object_get_parent(id),obj_allied);
        sprite_index = spr_enemy2;
        my_enemy = obj_enemy;
        };
    };
When I do this, the ai is very confused as to whom it should be shooting at. It will shoot at the enemy, but also intermittently its allies.
Clearly I don't understand how the functions work...

I was wondering if there is a way to do this, or if I am going about it all wrong and should be using a better concept for my ai structure.
Should I just put obj_player at the bottom of the pile and call events from obj_ship directly?
I am so confused...

Thanks!

---Edited to change some inaccuracies--

--Realized this should probably be in novice/Intermediate users--- Man, I suck at making topics.

Edited by C_Pike, 29 April 2012 - 06:01 PM.

  • 0

#2 Gamer3D

Gamer3D

    Human* me = this;

  • GMC Member
  • 1815 posts
  • Version:GM:Studio

Posted 29 April 2012 - 06:30 PM

Here's a simple solution:

obj_ship (With variable "team")
|-----------|
obj_player obj_ai_ship

Anyway, stop switching object parents. I know GM allows some weirdness like that, but it's not available in most languages.
  • 0

#3 C_Pike

C_Pike

    GMC Member

  • GMC Member
  • 618 posts
  • Version:GM8.1

Posted 29 April 2012 - 07:44 PM

Hmm...
Currently, if I do a standard check to give the AI some direction it looks something like:
// my_enemy is a pointer to either obj_allied or obj_enemy
if (path_assigned == "need_path") //we need a path
    {
    path_initalize = false;
    if (path_object == noone) // if we dont have an enemy
        {
        if (instance_exists(my_enemy)&&(distance_to_object(my_enemy)<ship_radarRange)) //look for an enemy
            {
            path_object = my_enemy;
            path_getLocation = "enemy_location"
            };
        else // if no enemy, random location
            {
            path_object=noone; 
            path_getLocation ="random_location";
            };
        };


Looking for an instance and then a variable, instead of an instance belonging to a specific group seems like it will make an
already bloated code more bloated (Bloating caused by Programmer Ability Limitations, in this case). Parent switching seems
like it should be fine as the parent is being switched on creation. But I must admit, concept vs reality is what I am up against. I have to
assume you have a handle on both, while I only am grasping at the concept. I will take your advise and look for another way.
Probably reliable ol' copy and paste.
Thanks for the reply.

The complete AI movement code (slightly different from the above code, as it is newer):
Spoiler

I added this for the sake of context in referring to my AI search code.

Edited by C_Pike, 29 April 2012 - 07:53 PM.

  • 0

#4 Gamer3D

Gamer3D

    Human* me = this;

  • GMC Member
  • 1815 posts
  • Version:GM:Studio

Posted 29 April 2012 - 08:03 PM

Looking for an instance and then a variable, instead of an instance belonging to a specific group seems like it will make an already bloated code more bloated (Bloating caused by Programmer Ability Limitations, in this case).

1 "if" statement per "with" statement is sufficient to replicate your desired behavior.

Parent switching seems like it should be fine as the parent is being switched on creation.

Last I checked, GameMaker parenting was a very poorly implemented system (For instance, the grandparent didn't affect anything). Some languages (C++ for instance) permit multiple parents, but as far as I know, GM is the only one with parent switching.

Anyway, let's consider the problems parent switching can cause. Example A:
  • 2 potential parent objects with completely different code and variables, 1 empty child object.
  • After child is created (Parent 1's creation code is inherited), parent is switched.
  • Now step event (inherited from parent 2) tries to use variables from parent 2's create event. Uninitialized variable error occurs (Note: It could be worse if all variables have the same names but different required values. In this case, you won't be informed of the error)

  • 0

#5 C_Pike

C_Pike

    GMC Member

  • GMC Member
  • 618 posts
  • Version:GM8.1

Posted 29 April 2012 - 08:22 PM

1 "if" statement per "with" statement is sufficient to replicate your desired behavior.

Sounds lovely! I will give it my best shot.

Anyway, let's consider the problems parent switching can cause. Example A:
2 potential parent objects with completely different code and variables, 1 empty child object.
After child is created (Parent 1's creation code is inherited), parent is switched.
Now step event (inherited from parent 2) tries to use variables from parent 2's create event. Uninitialized variable error occurs (Note: It could be worse if all variables have the same names but different required values. In this case, you won't be informed of the error)


I was "sure" (quotations used ironically) that I had accounted for these problems.

[obj_shipParent] contains all the variables
[obj_ally] & [obj_enemy] are empty
[obj_ai] contains the pathing code and is the one I desired to be switched
[obj_ship] is empty, and when it is created it attempts to have its parent switch alignment

Clearly I was wrong.
I wrote a simple draw code to show me what the heck was going on:
        draw_text_color(x,y,string(object_get_name(object_index))+"#"+
        string(object_get_name(object_get_parent(object_index) ))+"#"+string(object_get_name(object_get_parent(object_get_parent(object_index) ))),
        c_white,c_white,c_white,c_white,1);


It clearly showed that the functions I was using were NOT changing the obj_ai parent. I tried a bunch of things to get an object
to change parents. The only one that worked was:
object_set_parent(id.object_index,obj_player);

// this is what the code was supposed to be...
object_set_parent(object_get_parent (id.object_index),obj_allied);
But it sets every ship object to argument 2, naturally. damn...

Oh well.

Edited by C_Pike, 29 April 2012 - 08:33 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users