Page 1 of 1

Variable declaration and pointers

PostPosted: Thu Nov 19, 2009 6:04 am
by zygoth
Ok, I have two questions here. Is there any way to make a pointer actor variable? What I mean is that GE complains if I try to reference a variable created in the Create Actor event in the Draw Event. Since the only way I can see to make actor variables is through GE's GUI(which does not support pointers), that leaves me with a problem if I want to have actors with references to other actors. In addition, what is the proper way to refer to a variable of a pointer's object? Is this okay:

Actor* mypointer = &someActor;
mypointer->someActor.x = 10;

I suppose I could just make a global array of pointers, but I would prefer not to have to.

Zygo

Re: Variable declaration and pointers

PostPosted: Thu Nov 19, 2009 7:35 am
by skydereign
Don't completely follow what you are trying to do, to me it seems your problem and what you want done are different... To reference variables from an Actor *, just do it like this.
Code: Select all
Actor* mypointer = &someActor;
mypointer->x = 10;


An Actor* is the actor you want, so you don't need to respecify the actor name, just what actor variable you want from the pointer. To try to address your other question, you cannot make pointer actor variables. The variables created in specific script editor events are just like those in a function, meaning they don't last. Now you could make a global array to hold these variables, or just make actor variables, and whenever you want to treat it as a pointer, convert it... You can do this with most variables, but at this point it is usually unnecessary, as you wouldn't have to convert. Again I don't really know what you want to do with the pointer. If you want actors to be able to reference other actors, they can do that already. Also, you can tie actors together, by having certain variables that would be used to retrieve the Actor* of another actor. This would require one or two actor variables, one for cloneindex, and one for actor name, though really neither are all important. Using these, you can use the Actor* to hopefully do whatever it is you are trying to do...

Re: Variable declaration and pointers

PostPosted: Thu Nov 19, 2009 6:08 pm
by zygoth
I hadn't thought about using the name and cloneindex to store it...Thanks for your suggestions, I will try that out and see what happens.

EDIT: ok, what syntax should I use for this? say nameReference is the name of the actor and indexReference is the cloneIndex.

DestroyActor(nameReference.cloneIndex); just does not work. I have experimented a little bit and only managed to crash GE, which was pretty exciting I guess. But not what I was looking for.

What I'm doing is making an RTS, and I want each unit to have references to its health meter which follows it around, as well as some other actors.
So, does the name of each actor act as a struct with an array of actors? So MyActor.4 refers to the 5th clone of that actor type? I know more about Java than C...Thanks for any help.

Re: Variable declaration and pointers

PostPosted: Thu Nov 19, 2009 10:41 pm
by skydereign
Well the storing of an actor name and variable is meant for getting an Actor* of the given actor. You could set up an actor char array, to hold all the actor types, and then one for the proper cloneindex for each, but I don't think this is optimal given what you want.

Just checking, have you tried using parent? I know using parenting for things like this is not always the best method, but you can make your units the parents of all of the following actors. If not, then you can also use creator, either of these allows you to do this.
Code: Select all
parent.x=100;
collide.x=100;


This does exactly like it looks, but if this kind of actor variable referencing is still not what you need, as it might not fit in the system you are using right now, then you can always use the method I suggested before. To do that, you would need to place this function into global code.
Code: Select all
Actor *
getCloneIdx (const char *cname, int cindex)    //g_GetCloneByIndex, its original name
{
    char buffer[50];
    sprintf(buffer,"%s.%i",cname,cindex);
    return(getclone(buffer));
}


Then if you have your actor variable string array, then you can put each required actor name into the array, and upon creation, you can set the cloneindex into a corresponding int array. Whenever you want to access an actors variables, you would do something like this.
Code: Select all
Actor* healthActor = getCloneIdx(nameStorage[0], indexStorage[0]);
healthActor->x = 100;


If you are going to do something like that, I would either define what each part of the array is, with either #define, or enum. That way you can easily track which actor you are getting. If this didn't make sense for any reason, I can explain it more in depth, or post a demo showing it. I don't know what you have already done for creating the units and following actors, so I can't say what would be the best method given the circumstances.

Re: Variable declaration and pointers

PostPosted: Fri Nov 20, 2009 1:00 am
by zygoth
This is making me go insane...I have a global array of Actor pointers. In a function, I have:

playerSelected[i][playerColor]->isSelected = 0;

GE says I am attempting to write beyond the allowed area. What????????

Re: Variable declaration and pointers

PostPosted: Fri Nov 20, 2009 1:08 am
by skydereign
Well, that means the i, or playerColor, is going beyond the boundaries of your Actor* array. I guess a global array of Actor*s would work, though problems such as this are likely to occur. If instead you just got the Actor* of certain actors when you need them, then it would eliminate things like this. Something else that may cause this is not setting the Actor*s. If the Actor*s are left NULL then it will give an error too.

Re: Variable declaration and pointers

PostPosted: Fri Nov 20, 2009 1:15 am
by zygoth
this is my function:

void clearSelected()
{
int i;
for(i = 0; i < 100; i++)
{
if(playerSelected[i][playerColor] != NULL)
{
playerSelected[i][playerColor]->isSelected = 0;
playerSelected[i][playerColor] = NULL;
}
}
playerSelectedNum = 0;
}

playerSelected is an array of Actor pointers with length 100.

EDIT: Ok, so I created a new file with nothing except 2 actors called referencer and referencee. I created a global Actor pointer pointing to referencee. I then put in keydown event of referencer: ptr->x++;

GE crashed every single time, saying "READ error in invalid memory error" If I can't even use pointers, than it is impossible for me to make the game I was planning on. If anybody can confirm this, I really need to start coming up with something else because my current project is shot. Am I just doing something wrong? please tell me that's the case. Thanks for any help.

Zygo

Re: Variable declaration and pointers

PostPosted: Fri Nov 20, 2009 7:30 am
by zygoth
So, for anybody still interested, I have tried to store references to actors by making a global array of x and y coordinates, then using getactor to get the reference. GE is not crashing anymore, but nor is it really doing what I want. I have an actor that is supposed to be following another actor. It's x and y are being changed by using the pointer given by getactor. It follows for a few frames, then stops. The ridiculous thing is, I created a text actor that displays its x position, and it says it is right where it is supposed to be. I'm stumped. At this point it seems to be some fundamental flaw in GE's data handling...makslane, do you have any suggestions? I can upload the file if that would help.

Re: Variable declaration and pointers

PostPosted: Fri Nov 20, 2009 8:21 am
by skydereign
Okay, I know this can be done, and that this is not a problem with memory, as I have done something similar to this. The problem probably has something to do with the approach. If you don't mind putting the files up, I could take a look at it. It would be much easier on my part, as I can't see what is really going on, and actually test in your ged environment.

Re: Variable declaration and pointers

PostPosted: Fri Nov 20, 2009 5:47 pm
by zygoth
Here is an example of what I am talking about. A and D move the referencer right and left, and Enter creates a referencee Actor. it is referred to by using getactor(referenceLocation[0], referenceLocation[1]) , where referenceLocation is a global array of length 2. Glitchy!

EDIT:

OOOkay, I think I found a way around this. when I create actors I store their clonename in an actor string variable of the creator. This works fine, and I can change variables from the actor with the pointer. The only problem is I have to declare variables for each different link I want to have, but in the game I am making I only need a couple for each actor. So happy days are here again :) Reply if you want any details or a demo of the working code.

Zygo