GM:S 1.4 Help With a Script

Discussion in 'Programming' started by MagicFool64, Sep 13, 2019.

  1. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    I'm trying to make a chain lighting attack, by making the "bullet" jumping to the next target (max 3), but I think I'm making in the bad way.

    Here is the script in the bullet
    Code:
    //Create
    target = ds_list_create();
    ds_list_add(bersagli, "obj_enemy");
    ds_list_add(bersagli, "obj_enemy");
    ds_list_add(bersagli, "obj_enemy");
    
    Life = 3
    
    //Destroy
    ds_list_destroy(target)
    
    //Step
    move_towards_point( obj_enemy, obj_enemy, 20 );
      
    //Collission with Enemy
    Life = -1
    
    x = obj_enemy;
    y = obj_enemy;
    Every time the bullet hits an enemy, it loses a life. With a Drag'n'Drop script I added that the bullet gets destroyed after it loses the life

    The script doesn't work, I don't know that to do. Can you help me?
     
  2. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    578
    Youre creating a ds_list every time a bullet is created and not even using it

    move_towards_point( obj_enemy, obj_enemy, 20 );
    What that does is pick an instance of Obj_enemy at random and picks that so the code actually reads something like

    move_towards_point( bob, bob, 20 );

    and because its a collision event its losing 1 life point every step its touching bob
     
    ParodyKnaveBob likes this.
  3. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    What do you suggest to me?
     
  4. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    578
    Code:
    //Create
    target = ds_list_create();
    ds_list_add(bersagli, "obj_enemy");
    ds_list_add(bersagli, "obj_enemy");
    ds_list_add(bersagli, "obj_enemy");
    What your doing here is creating a list
    saving a random obj_enemy to list bersagli 3 times

    What you should do is have a for loop
    Code:
    var _Enemys = instance_number(obj_enemy);
    
    for (var i = 0; i < _Enemys; i++)
    {
    
    }
    That saves the amount of enemy's in the room to a variable and compare it to i
    if i is less then the amount of enemys then we execute the code in the brackets until it is false
    what we should put here is this

    Code:
    {
        var found_enemy = instance_id(instance_number(i));
        ds_list_add(bersagli, found_enemy);
    }
    something like that, forgive me its been a while since I've coded something like that
    but then you need to go to the nearest enemy and kill it then move to the next

    thinking about it I wouldnt use a list I'd just use instance_nearest(obj_enemy) and a check to see what enemy we've been to and if its been 3 enemys
     
  5. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    Thanks for the support. By the way, it says that "instance_id" is an error in the last script. Are you sure to have put the right script?
    Anyway, I made I mistake: I haven't translated "bersagli".
    It's

    Code:
    target = ds_list_create();
    ds_list_add(target, "obj_enemy");
    ds_list_add(target, "obj_enemy");
    ds_list_add(target, "obj_enemy");[/CODE
     
  6. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    578
    Oh yes I used it wrong, my bad
     
  7. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    What's the right one?
     
  8. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    What do I have to write instead of "instance_id"?
     
  9. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,470
    See: instance_find()
    Code:
    var _Enemys = instance_number(obj_enemy);
    
    for (var i = 0; i < _Enemys; i++)
    {
        ds_list_add(target, instance_find(obj_enemy, i));
    }
    But I'm not a huge fan of these instance_find() loops for performance and verbosity reasons. I always use with blocks for this purpose.
    Code:
    with (obj_enemy)
    {
        ds_list_add(other.target, id);
    }
    
    While rookies who only play by standard C-family control structures will complain about this being "confusing" or "not self-documenting", I can tell you that spending a little time to master the behaviour of with blocks is well worth the expense. It's not as unpredictable or hard as most rookies claim, and once you master it, you can easily do a lot of things that the rest of the herd could spend hours in vain trying to replicate.
     
    EvanSki likes this.
  10. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    I wrote this script in the bullet step event
    But the bullet doesn't move. Am I missing something?
     
  11. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,470
    There's no movement logic in that piece of code, only logic for adding instance IDs to a list. You need to take entries from that list and then move towards them. Here's an example:
    Code:
    var currentTarget = target[| 0];
    if (instance_exists(currentTarget)) {
      move_towards_point(currentTarget.x, currentTarget.y, 20);
    } else {
      ds_list_delete(target, 0);
    }
    
    You can use the "Collect all instances with property sorted in ascending order" pattern from this article to collect all enemy instances in an ordered list, then pick the first 3 from that list, then move towards each one in turn as you touch each.

    Before that, I think you should read up on the difference between objects and instances. Your code clearly shows you don't know the difference, and there's no way to make it work until you do.
     
    ParodyKnaveBob likes this.
  12. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    Now the bullet goes to the target, but doesn't go to the nearest one. Sorry if I bother you
     
  13. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,470
    It bothers me how you don't seem to be thinking through any of this. If you didn't collect entries in that list in order of distance (which the instance_find() loop certainly isn't), then you can't expect its first entry to be the nearest. Like I already said, you have to redo the code collecting instance IDs for the list so that it is in order ("Collect all instances with property sorted in ascending order" from this article; the property is point_distance(x, y, other.x, other.y)). Then you can iteratively pick entries out of the list, as you collide with them in turn.

    What you're doing is like taking your local Yellow Pages phone book and calling the first person on its first page. Chances are your nearest neighbour won't be the one picking up the call.
     
    ParodyKnaveBob likes this.
  14. MagicFool64

    MagicFool64 Member

    Joined:
    Feb 3, 2019
    Posts:
    59
    If this makes you happy, I found a way to resolve it
     
  15. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    510
    For future people with similar problems seeking answers, would you mind posting your solution? $:^ J
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice