Robot AI Battle: Save Enemys Robot's Ids in ds_list. Problems with instance_exists.

iMilchshake

Member
Hayho Guys! ^-^
Hope you guys can help me. So basicly im doing a Robot AI (Team A) vs. Robot AI (Team B) simulation right now. There is a script that searches for a new target in the detection range for each Robot. The Script get triggered all 3-4 Seconds or if a Robot has no target (Target Died). Second doesnt work. That is because im saving all the Enemys Robots(In the Detection Range) ids in a ds_list:
Code:
ds_list_clear(spotted_enemys); //ds list where ids get saved in
        if team=="A" //checks with team the robot is in
        {
        for (var i=0;i<instance_number(RobotB);i++)
        {
        e=instance_find(RobotB,i)
        if distance_to_object(e)<detrange //checks if the checked robot is in the detection range
        ds_list_add(spotted_enemys,e) //if yes, adds it to the list
        }
        target=ds_list_find_value(spotted_enemys,irandom(ds_list_size(spotted_enemys)))
        }
Same for team=="B" of course.


When im trying to put a code like this in the step event:

Code:
if !(instance_exists(target))
{
scr_get_target();
}
I get this Error Message:
Code:
instance_exists argument 1 incorrect type (5) expecting a Number (YYGI32)
at gml_Object_Robot_StepNormalEvent_1 (line 6) - if !(instance_exists(target))
Looks like the Ids arent beeing recognised as ids in a ds_list. tried it with this already:
Code:
if is_undefined(target)
No Error message tho, but doesnt work as it should. When a robot's target dies it doesnt triggers the script automatically, because the variable "target" doesnt get cleared when the target dies. Hope someone understood my problem and can help me D:.


EDIT:

Idea: When a Robot dies it first checks if it was someones target. If yes it sets their variable "target" to undefined (dont know how to do that tho). Then
Code:
if is_undefined(target)
should work, right?
 

Yal

GMC Memer
GMC Elder
I'd use instance_exists() instead, it checks whether the instance (target) exists. Is_undefined() just checks whether the variable has an undefined data type, and any instance ID is a valid number, so it can't be undefined.
 

iMilchshake

Member
I'd use instance_exists() instead, it checks whether the instance (target) exists. Is_undefined() just checks whether the variable has an undefined data type, and any instance ID is a valid number, so it can't be undefined.
Well thats my problem, instance_exists() doesnt work :(.
Get this error message:

Code:
instance_exists argument 1 incorrect type (5) expecting a Number (YYGI32)
at gml_Object_Robot_StepNormalEvent_1 (line 6) - if !(instance_exists(target))
Instance_exists(), doesnt seem to work with a ds_list :mad:
 

Yal

GMC Memer
GMC Elder
How about this, then?
Code:
for(c = 0;c < ds_list_size(target);c++){
  if(instance_exists(target[|c])){
    thisone = target[|c]
    /*run code here*/
  }
}
 

iMilchshake

Member
How about this, then?
Code:
for(c = 0;c < ds_list_size(target);c++){
  if(instance_exists(target[|c])){
    thisone = target[|c]
    /*run code here*/
  }
}
Gonna try that one.. but what exactly does
Code:
[|c]
mean? Never saw or used that before :'D.
 

Yal

GMC Memer
GMC Elder
It's the accessor for ds_lists, it lets you use lists as you'd use an array. It's a really useful shorthand; it gets translated to the appropriate functions internally but is a lot less work to write.
Also check out [#c,d] for grids and [?c] for maps, both really handy.
 

iMilchshake

Member
It's the accessor for ds_lists, it lets you use lists as you'd use an array. It's a really useful shorthand; it gets translated to the appropriate functions internally but is a lot less work to write.
Also check out [#c,d] for grids and [?c] for maps, both really handy.
Whoah! Thanks man, that should fix my problem i guess.. And will help me in the future xD.
Im Going to try this tomorrow :)
 

Yal

GMC Memer
GMC Elder
Yay, glad I could help :3

Don't forget to mark your topic as solved (via prefix) once you know it works, too~
 
I know list work for this because it's just a number. I think it the last line is your problem. If that random number is the size of the list there isn't a value for it right? Since list start with a 0 index. ds_list_size -1 would fix it I think. Also try writing !instance_exists (target) instead of ! (Instance_exists ( )) . believe I had an issue with the way you write it with a similar function not sure how that effects it though.
 

iMilchshake

Member
Works like a charm<3.

Code:
///Die Script
var saveid=id;

with(Robot)
{
if target=saveid
{
target=-1;
}
}
instance_destroy();
Now i just check for
Code:
if target>0

instead of

if instance_exists(target)
Your Stuff unfortunately didnt work :/.
 
Top