Page 1 of 1

getclone/CreateActor, Actor* "." vs "->", ERRORS

PostPosted: Sun Jan 27, 2013 6:14 pm
by EvanAgain
With getclone:
Code: Select all
//WORKS
Actor* myActor = getclone("ItemSlotHitBox.0");

int r_x = myActor->xscreen-xscreen;
int r_y = myActor->yscreen-yscreen;

moveto(r_x , r_y);


Code: Select all
//DOESNT WORK
Actor* myActor = getclone("ItemSlotHitBox.0");

int r_x = myActor.xscreen-xscreen;
int r_y = myActor.yscreen-yscreen;

moveto(r_x , r_y);



Code: Select all

//DOESN'T WORK
Actor* myActor;

myActor = getclone("ItemSlotHitBox.0");

int r_x = myActor->xscreen-xscreen;
int r_y = myActor->yscreen-yscreen;

moveto(r_x , r_y);



This was written into my Canvas actor.


While with CreateActor:

Code: Select all
Actor* myActor;
myActor = CreateActor("Draggable", "simplegear", "(none)", "(none)", x, y, false);


That is perfectly acceptable? What is the difference?

Re: getclone/CreateActor, Actor* "." vs "->", ERRORS

PostPosted: Sun Jan 27, 2013 6:59 pm
by skydereign
The difference is that one you are breaking how you declare variables in C. Unlike languages you are probably used to, C requires you to declare all variables at the beginning of a code block. So, that means at the beginning of an event, or after an open bracket.
Code: Select all
int var0;
int var1;

x+=5; // this doesn't declare a variable, therefore you can no longer declare more variables
// int var2; // this would give an error if you remove the first comment on this line

if(x>10)
{ // beginning of a code block
    int var3; // you can declare variables again
    x=0; // now you can't
}

Re: getclone/CreateActor, Actor* "." vs "->", ERRORS

PostPosted: Sun Jan 27, 2013 7:45 pm
by EvanAgain
I understand that but that is not the problem. Here let me seperate the code so it doesn't look like I have that all AS IS.

Re: getclone/CreateActor, Actor* "." vs "->", ERRORS

PostPosted: Sun Jan 27, 2013 8:41 pm
by skydereign
The thing is that is that is one of the problems with your code. You do declare ints after, in some of those code examples. But the real problem you are encountering, is that you have to use myActor->xscreen. The reason you didn't catch it, is that your two examples checking dot and points to, are different. You changed two things.

So, you have to use the -> (points to) operator when dealing with pointers. It dereferences the pointer while accessing the variable. You could do the same thing like this.
Code: Select all
(*myActor).xscreen;

But there isn't really a point to dereferencing it like that. You don't use points to when dealing with structs that aren't pointers, for instance collide, creator, parent, actorName, and so on. Those you just use the actor.variable.

Re: getclone/CreateActor, Actor* "." vs "->", ERRORS

PostPosted: Sun Jan 27, 2013 9:33 pm
by EvanAgain
OH, so if I move the getclone function below the int delcaration it will work.

But also, I thought that the variable was just a pointer to the struct in memory. It was just my misunderstanding. Coming from C++ and going to C brings a lot of differences. Especially thinking about structs, pointers, and classes(C++).

Some of the things in C++ were just that "." practically meant the same thing as "->" I think just not having coded in a long time is making me think about these things weird. Also, the way I keep switching back and forth between accessing the Actor struct and accessing a POINTER to the Actor Struct. Gets confusing.

Thanks for the help, its getting confusing and frustrating I am missing obvious things.