SOLVED [GML]Need help with collision and arrays please

my main question is I am unsure what i did wrong everything works for the most part but i want the character to get knockbacked to either the left or the right depending on the direction they are facing here is my code

GML:
if(collision_point(obj_damage.x,obj_damage.y,obj_damage,false,false))
{
    collison = true;
    if(global.phealth >= 1)
{
    global.phealth -= damage;
    global.healthremaining = global.phealth;
}
var dir = (sign(x-other.x)*10);
if(collison = true)
{
    hspd = dir * x;
    vspd = -25;
}
if(global.phealth <= 0)
{
    instance_destroy(obj_player);
    room_restart();
}
}

as for my arrays question can someone explain to me hopefully as simply as possible how arrays work i have read the manual and watched youtube videos but i cant quite grasp how they work i feel like there is something im just not understands or a piece of a puzzle im missing to finally go OOOOOHHHH lol

if someone can help me with my code and possibly link me to something or give a quick explanation on arrays i would be most great full
 

FrostyCat

Redemption Seeker
For the collision question, you need to learn the difference between objects and instances.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ but don't end in _list all return instance IDs. Save that instance ID into a variable, then use that as the subject to work with. DO remember to check that it is not noone before acting on it. DO NOT refer to this instance as other.
GML:
var inst_enemy = instance_place(x, y, obj_enemy);
if (inst_enemy != noone) {
    inst_enemy.hp -= 10;
}
For the array question, these three videos from samspade give a good explanation: Part 1 (basics) | Part 2 (multi-dimensional) | Part 3 (insert/delete functions)
 

Nidoking

Member
var dir = (sign(x-other.x)*10);
Is this in a Collision event? If so, then collision_point doesn't make much sense. If not, then "other" has no meaning here.

Actually, collision_point doesn't make much sense anyway:

if(collision_point(obj_damage.x,obj_damage.y,obj_damage,false,false))
Is there an obj_damage at the location of the obj_damage? Of course there is.

Then you set collison to true, and then check to see whether collison is true. It will always be true because you always set it to be true.

Basically, I think you need to return to simpler tutorials and get a grasp of basic Game Maker concepts, because I think arrays are the least of the important things you don't understand.
 
Is this in a Collision event? If so, then collision_point doesn't make much sense. If not, then "other" has no meaning here.

Actually, collision_point doesn't make much sense anyway:



Is there an obj_damage at the location of the obj_damage? Of course there is.

Then you set collison to true, and then check to see whether collison is true. It will always be true because you always set it to be true.

Basically, I think you need to return to simpler tutorials and get a grasp of basic Game Maker concepts, because I think arrays are the least of the important things you don't understand.
GML:
//yes in collision event but would not work without me adding this

if(collision_point(obj_damage.x,obj_damage.y,obj_damage,false,false))
{
    collison = true;
    if(global.phealth >= 1)
{
    global.phealth -= damage;
    global.healthremaining = global.phealth;
}
//i added this part to see what would happen 

var dir = (sign(x-other.x)*10);
if(collison = true)
{


    hspd = dir * x;
    vspd = -25;
}
if(global.phealth <= 0)
{
    instance_destroy(obj_player);
    room_restart();
}
}
yes it is in a collision event i know the collision point does not make sense but the code would not work other wise and i have no clue why it will only work after adding a collision point
 

Nidoking

Member
What, exactly, does it DO when you don't have the collision_point in there? Maybe, instead of randomly trying things, you could find a correct solution and do that instead.
 
What, exactly, does it DO when you don't have the collision_point in there? Maybe, instead of randomly trying things, you could find a correct solution and do that instead.
nothing actually happens its a platformer so i just fall off the screen or go right through it but for some reason when i add in the collision point the code starts working

doing things randomly is how i learn it lets me see cause and effect if i do blahblah then blahblah happens and if i add this then this does or does not happen and this is how so and so works by doing blah blah and i can add so and so or blah blah to make this happen this is just how i learn

i think thats why arrays confuse the hell out of me because i cant exactly play with them in that way atleast far as i can tell i can not play with them the same way i play with if statements and instances and so on
 

Nidoking

Member
nothing actually happens its a platformer so i just fall off the screen or go right through it
This is a contradiction. If "nothing actually happens", then you don't "just fall off the screen or go right through it". Falling off the screen is something happening. Going right through an it is something happening. Your problem may just be that you don't understand logic at all. This is a common problem, but one that will very much get in the way of any programming you try to do. You need to learn about things like cause and effect, experimentation, and sequences of steps leading to performance of a process. You probably have some basic understanding of what those things are, but you really don't seem to understand them to any functional degree. Playing with something only works as a learning tool if you look deeper into things and determine why the change you see is happening. You have the Bogosort of programming techniques: You randomly write a program, then check to see whether it is the program you want. There are steps you can take to determine the effect of a change before you make it, and then to determine what change you need to make without guessing and hoping.

Case in point: You haven't mentioned which object the event is in, and which object the collision event is with. What does the event look like when you don't have a meaningless collision_point call in it? And what is the actual effect that happens? Then, we can work toward fixing the actual problem instead of having to deal with problems that you've created for yourself.
 
Top