• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Help With a Script

M

MagicFool64

Guest
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?
 

Evanski

Raccoon Lord
Forum Staff
Moderator
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
 
M

MagicFool64

Guest
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
What do you suggest to me?
 

Evanski

Raccoon Lord
Forum Staff
Moderator
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
 
M

MagicFool64

Guest
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
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
 

Evanski

Raccoon Lord
Forum Staff
Moderator
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
Oh yes I used it wrong, my bad
 

FrostyCat

Redemption Seeker
What do I have to write instead of "instance_id"?
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.
 
M

MagicFool64

Guest
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.
I wrote this script in the bullet step event
var _Enemys = instance_number(obj_enemy);

for (var i = 3; i < _Enemys; i++)
{
ds_list_add(target, instance_find(obj_enemy, i));
}
But the bullet doesn't move. Am I missing something?
 

FrostyCat

Redemption Seeker
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.
 
M

MagicFool64

Guest
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.
Now the bullet goes to the target, but doesn't go to the nearest one. Sorry if I bother you
 

FrostyCat

Redemption Seeker
Now the bullet goes to the target, but doesn't go to the nearest one. Sorry if I bother you
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.
 
M

MagicFool64

Guest
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.
If this makes you happy, I found a way to resolve it
 
Top