Hulle
Member
I want to understand code better, and so, please help me understand why the top code works when running my game, but not the buttom code:
TLDR: I change a local var DSlist. It is destroyed each step it is called/not needed. (top code)
to a DSlist in from create-event, that is cleared when not needed (collidedslist). But it stops adding to hitdslist, when it is created this way.
TLDR: I change a local var DSlist. It is destroyed each step it is called/not needed. (top code)
to a DSlist in from create-event, that is cleared when not needed (collidedslist). But it stops adding to hitdslist, when it is created this way.
Code:
STEP EVENT:
if state == "blackflower" //Touch undeads
{
if listcreate = false
{
hitdslist = ds_list_create(); //This var is initialized in create
ds_list_clear(hitdslist);
listcreate = true;
}
var collidedslist = ds_list_create(); //Shoutout to Shaun Spalding,
ds_list_clear(collidedslist);
var enemy = instance_place_list(x, y, obj_enemy_parent, collidedslist, false); //Find if we collide with an enemy
if enemy > 0 //If we collide with more than 0
{
for (var i = 0; i < enemy; ++i;) //Run a loop through them
{
var hit = ds_list_find_value(collidedslist,i) //find current ID
var pos = ds_list_find_index(hitdslist, hit) //
if pos == -1 //see if it doesexsist
{ //If it does not:
ds_list_add(hitdslist, hit); //We add it to the list
if i == enemy
{
ds_list_destroy(collidedslist);
}
size = ds_list_size(hitdslist) //The we get the amount of enemies added to the list
if size >= hituniqueamount
{
itemname = "blackflower"; //Change my name
image_index = 3; //Turn black
state = "blackdeath"
}
}
}
}
}
Code:
STEP EVENT:
if state == "blackflower" //Touch undeads
{
if listcreate = false
{
collidedslist = ds_list_create(); //Create and clear a few lists
ds_list_clear(collidedslist);
hitdslist = ds_list_create(); //Theese are both initialized in create
ds_list_clear(hitdslist);
listcreate = true;
}
var enemy = instance_place_list(x, y, obj_enemy_parent, collidedslist, false); //Find if we collide with an enemy
if enemy > 0 //If we collide with more than 0
{
for (var i = 0; i < enemy; ++i;) //Run a loop through them
{
var hit = ds_list_find_value(collidedslist,i) //find current ID
var pos = ds_list_find_index(hitdslist, hit) //
if pos == -1 //see if it doesexsist
{ //If it does not:
ds_list_add(hitdslist, hit); //We add it to the list
if i == enemy
{
ds_list_clear(collidedslist);
}
size = ds_list_size(hitdslist) //The we get the amount of enemies added to the list
if size >= hituniqueamount
{
itemname = "blackflower"; //Change my name
image_index = 3; //Turn black
state = "blackdeath"
}
}
}
}
}
Last edited: