Pages: 1
Author Topic: Strange For Error  (23,137 Views)
Offline (Unknown gender) FroggestSpirit

Member
Joined: Mar 2013
Posts: 79
View profile
Posted on: December 22, 2015, 02:25:00 PM
I'm getting an error that I can't seem to figure out. I'm trying to write a small template for GUI buttons, and I'm finding it does not run properly. It seems that the variable totalButtons changes at some point.

Createint clicked=0;
int totalButtons=1;
unsigned char *buttonID;
buttonID= new unsigned char [totalButtons];
signed short *buttonX;
buttonX= new signed short [totalButtons];
signed short *buttonY;
buttonY= new signed short [totalButtons];
unsigned char *buttonW;
buttonW= new unsigned char [totalButtons];
unsigned char *buttonH;
buttonH= new unsigned char [totalButtons];


Drawdraw_set_color(c_white);
for(int i=0; i<totalButtons; i++){
// if(i+1==clicked){
// draw_set_color(c_gray);
//draw_rectangle(buttonX[i],buttonY[i],buttonX[i]+buttonW[i],buttonY[i]+buttonH[i],false);
// draw_set_color(c_white);
// }else{
//draw_rectangle(buttonX[i],buttonY[i],buttonX[i]+buttonW[i],buttonY[i]+buttonH[i],false);
// }
}

draw_set_color(c_black);
//for(int i=0; i<totalButtons; i++){
//draw_rectangle(buttonX[i],buttonY[i],buttonX[i]+buttonW[i],buttonY[i]+buttonH[i],true);
//draw_text(buttonX[i],buttonY[i],buttonText[i]);
//}

I commented out a lot to try to narrow it down, but it does work if the first for is changed to a variable or number other than totalButtons

EDIT: I fixed it by changing "int totalButtons" to "local int totalButtons"
Is this normal? or an oversight?
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #1 Posted on: December 23, 2015, 02:01:06 PM
It is normal. If you type "int button", then the variable is temporary to that script (in this case create event code). Just like if you type "var button" in GM or ENIGMA, because "var" is actually a type "variant", just like "int" is "integer" and so on. If you want to use types, but also want them to be local to the instance, then you must add "local" in front. So basically totalButtons in Draw Event was zero, because it was undefined. If you ran in Debug Mode it should have errored at that point. There is a slight explanation here: http://enigma-dev.org/docs/Wiki/Moving_from_EDL_to_C%2B%2B . Sadly good docs, examples and tutorials is also a thing we are missing in ENIGMA project, because it just requires a lot of manpower we don't have.

I am surprised C style pointer arrays worked though. I would suggest using a data structure or just GM/ENIGMA array. But if this works, then okay. See if "delete buttonH" also works.
Offline (Unknown gender) FroggestSpirit

Member
Joined: Mar 2013
Posts: 79
View profile
Reply #2 Posted on: January 04, 2016, 03:57:15 PM
Sorry for the late reply,
delete buttonH[];
delete buttonH;
delete *buttonH;
all come up with an error saying that delete expects a pointer.
delete buttonH*;
gave me a different error
Pages: 1