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

Need Help assigning Structs to List Position

B

Bonzo

Guest
I am following a Coding Train tutorial on youtube but I'm not allowed to link it here

I am looking into basic linear regression. I decided to represent each point with an array, and assign the array to a struct with an x and y value. I have done similar things in previous projects, but when I my code showed the values in the struct were either not assigned or accessed correctly I replaced the x and y variables to make sure I wasnt somehow interfering with builtin variables and replaced my array assignments with ds_list calls in a desperate effort to figure out what I am doing wrong. It is entirely possible I am making some small mistake, in which case I would appreciate someone to double check this code:

Error Message:
Screenshot_2.jpg
This happens when I try to place a second data point and the linear regression region is ran for the first time.

Step Event:
Screenshot_1.jpg
 
Wouldn't pointLast actually be ds_list_size(point)-1?

Also, in the future, it's much more preferable to actually copy and paste your code into [ code] [/ code] blocks so that other users can just copy and paste instead of having to type your code out themselves.
 
B

Bonzo

Guest
Wouldn't pointLast actually be ds_list_size(point)-1?

Also, in the future, it's much more preferable to actually copy and paste your code into [ code] [/ code] blocks so that other users can just copy and paste instead of having to type your code out themselves.
I have tried something similar by both trying to access pointLast-1 and trying to use pointLast+1 for the ds_list accessor but neither worked, and trying to set pointLast equal to one less returns -1 which is not within the valid space of the data structure.
1600579563978.png
In the above image I comment out the linear regression and the points are placed and drawn fine, but as soon as I try to access the points using the for loop I get an error after placing the second point. Is it possible this is a bug with structs in GMS 2?

//Draw Points//
#region
for (var i = 0; i < ds_list_size(point); i ++;) {
draw_circle(point[| i].px*400,point[| i].py*400,4,0);
}
#endregion

I am able to access the components of each struct just fine for this draw call but for some reason when I try to add the x and y component of each point to a local variable it doesn't work. :confused:
Sorry about not using inline code here is the step event again:

//Mouse Pressed//
#region
mousePressed = mouse_check_button_pressed(mb_left);

if mousePressed {
var mapMousepx = mouse_x/400;
var mapMousepy = mouse_y/400;
var pointLast = ds_list_size(point);
point[| pointLast] = {
px : mapMousepx,
py : mapMousepy
};
}
#endregion

//Linear Regression//
#region
if ds_list_size(point) > 1
{
var pxsum, var pysum;
for (var i = 0; i < ds_list_size(point); i ++;) {
pxsum += point[| i].px;
pysum += point[| i].py;
}
var pxmean = pxsum / ds_list_size(point);
var pymean = pysum / ds_list_size(point);

var num, den;
for (var i = 0; i < ds_list_size(point); i ++;) {
var X = point[| i].px;
var Y = point[| i].py;
num += (X - pxmean) * (Y - pymean);
den += (Y - pxmean) * (X - pxmean);
}

m = num / den;
b = pymean - m * pxmean;
}
#endregion
 
Ok, so the way that computers count starts at 0 (99% of the time, GMS uses 1 as a start point if you are dealing with string manipulation, but that's completely separate to this issue). So if you have a ds_list of size 1, the first entry is 0. Accessing a data structure like this: pointLast = ds_list_size(point); will forever put pointLast outside of the range of the ds_list, as ds_list_size will return 1, but the list entry you want to access is at position 0. What you need to do is run a check to see if the ds_list_size is greater than 0, and if it is, you can then set pointLast to ds_list_size(point)-1. Trying to do anything other than this is incorrect code when it comes to lists, arrays, grids, etc.
 
B

Bonzo

Guest
Ok, so the way that computers count starts at 0 (99% of the time, GMS uses 1 as a start point if you are dealing with string manipulation, but that's completely separate to this issue). So if you have a ds_list of size 1, the first entry is 0. Accessing a data structure like this: pointLast = ds_list_size(point); will forever put pointLast outside of the range of the ds_list, as ds_list_size will return 1, but the list entry you want to access is at position 0. What you need to do is run a check to see if the ds_list_size is greater than 0, and if it is, you can then set pointLast to ds_list_size(point)-1. Trying to do anything other than this is incorrect code when it comes to lists, arrays, grids, etc.
The //Mouse Pressed// region is for placing points. When there are zero points I have to place a point at the zero position in the list. When there are zero points ds_list_size returns 0, so at the instant I am placing a point it makes sense to place a point at ds_list_size since I never place more than one each step anyways.
I get what you are on about, but I have done the same thing with node editors in the past using arrays and the point placement is working fine when drawing each point starting form zero.
 
Ah, ok, looking at the code more carefully, you are right, you are simply inserting a point, not trying to read one from there. Have you put a breakpoint at the troublesome line pxsum += point[| i].px; and checked the values in the debugger? Might give you an idea as to what is happening. I'm assuming something funky is happening when you are adding points and the points are not in the positions you expect them to be (or are missing in some way).
 

Nidoking

Member
If you're adding values to the list, just use ds_list_add.

The actual problem is that you're trying to add the list value to the current value of a variable that you explicitly didn't assign a value to. You have to start with a number if you want to add a number. Give pxsum a value before you try to get that value.
 
B

Bonzo

Guest
Thanks for the help guys. I switched the data back over to arrays and launched the game in debugger mode with breakpoints. Nidoking ended up being right about how the variables I was adding to were unassigned and so I couldn't add them. I ended up changing var pxsum, pysum; to var pxsum = 0; var pysum = 0;
 
Top