• 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!

Variable is malformed error

D

dcamod

Guest
Well I have been going back through steering behaviors to get them memorized and I am trying to use a formula for seek that is a bug correction I saw watching a GDC conference on Context Based Steering. Anyway when I try to implement it I get this error. I believe it is because I am multiplying a 2d vector with a single number but I am not sure why it won't work. I am using PixelatedPope's revised MojoCollective vector scripts.

Here is the error.
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_AI:

Variable is malformed
at gml_Script_sb_seek (line 23) - _desired *= (maxSpeed/_weight);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_sb_seek (line 23)
called from - gml_Object_obj_AI_StepNormalEvent_1 (line 8) - steering = vect_add(steering,sb_seek(mouse_x,mouse_y,1))

The correction I am trying to implement is this one. Seek is called in the step event as the script sb_seek.
Code:
vec2 seek(vec2 target, Agent agent) {
    vec2 desired = target - agent.position;
    desired *= agent.maxspeed / desired.mag();

    vec2 force = desired - agent.velocity;
    return force * (agent.maxForce / agent.maxSpeed);
}

Here is the code that is returning the error.
I may be way off on how I am trying to implement this. My understanding of steering behaviors is not very good. I am doing this step by step to learn and creating exercises to help me understand so if I look all over the place and lost, well now you know why.
Code:
///sb_seek(x,y,_weight)

var _target = vect2(argument[0],argument[1]);
var _weight = argument[2];

var _desired = vect_subtract(_target,position);
_desired *= (maxSpeed/_weight);
var _force = vect_subtract(_desired,velocity);
return _force * (maxForce/maxSpeed);

UPDATE 1

I got rid of the error by multiplying each of the vector co-ordinates separately but it does not function the way it should at all. It is actually quite funny so anyway here are the newest changes.

Code:
var _target = vect2(argument[0],argument[1]);
var _weight = argument[2];

var _desired = vect_subtract(_target,position);
//_desired *= (maxSpeed/_weight);
var _desired_mult = maxSpeed/_weight;
_desired = vect2(_desired[0]*_desired_mult,_desired[1]*_desired_mult)
var _force = vect_subtract(_desired,velocity);
var _force_mult = (maxForce/maxSpeed)
_force = vect2(_force[0] * _force_mult,_force[1] * _force_mult);
return _force;

UPDATE 2

Well it works now but it is super strong and I am not sure why. It does not seem to me like I have done this right but here is the new update I have. I would also like to note that the topic name is now a bit inaccurate. I no longer receive a variable is malformed error. I was trying to multiply it the wrong way I assume, but I am still unclear as to why this was incorrect.
Code:
var _target = vect2(argument[0],argument[1]);
var _weight = argument[2];

var _desired = vect_subtract(_target,position);
var _desired_mult = maxSpeed/_weight;
_desired = vect_multr(_desired,_desired_mult)
var _force = vect_subtract(_desired,velocity);
var _force_mult = (maxForce/maxSpeed)
_force = vect_multr(_force,_force_mult)
return _force;
 
Last edited by a moderator:

YoSniper

Member
Your error might be the result of you trying to use the multiplication operator * with a vector object.

Telling a variable to *= something is the same as saying "multiply this value by that over there, then store the result in this variable."

Apparently vect_subtract is a user defined script, so you would have to tell us what kind of value it returns. In any event, I have a sneaking suspicion that's the issue.
 
D

dcamod

Guest
Here is the vect_subtract script written by TheMojoCollective and edited by PixelatedPope who's tutorial I followed and scripts for vectors I downloaded.
Code:
///vect_subtract(vect1, vect2)
//Subtracts each component of vect2 from each component of vect1
var i;
var v1 = argument0;
var v2 = argument1;
var v;

var num = min(v1[0],v2[0]);
v[0] = num;
for(i=1; i<= num; i++)
{
    v[i] = v1[i]-v2[i];
}
return v;

Not sure if you read my edits but I am experiencing a new problem while still trying to figure out why this happened.
 
You're dividing your max speed by weight. In the seek reference code, I don't see weight being used. Normally the weighting of each steering behaviour is applied outside of the actual call to seek() / flee() etc...

So where you have this in your reference code:
Code:
desired *= agent.maxspeed / desired.mag();
You want to be dividing the agents max speed by the magnitude of your desired vector.

Magnitude is sometimes called the length of the vector, pretty sure there should be a function that gets the length / magnitude of the vector.

You could still apply the weighting after this calculation.

So try this:
Code:
var _target = vect2(argument[0],argument[1]);
var _weight = argument[2];

var _desired = vect_subtract(_target,position);

// REPLACE vect2_length with your api's magnitude function
var _desired_magnitude = vect2_length(_desired);
var _desired_mult = maxSpeed/_desired_magnitude;
// APPLY Weighting
_desired_mult *= _weight;

_desired = vect_multr(_desired,_desired_mult)

var _force = vect_subtract(_desired,velocity);
var _force_mult = (maxForce/maxSpeed)
_force = vect_multr(_force,_force_mult)

return _force;
 
D

dcamod

Guest
Thank you. That is very helpful. I had no idea what magnitude was supposed to be so I assumed it was weight. I am an animator by trade and math does not come easy to me. I will try to implement this and give an update on my progress. I appreciate the detailed and clear explanation. That is a bit of a rarity sometimes. You are awesome.

Also I will mark this as solved if it works when I implement it.
 
Top