Getclone

From Game Editor

Jump to: navigation, search
Actor *getclone(char *cloneName);

This function returns the actor pointer to the clone specified. This allows you to read and change the variables of the actor, where normally you could not. getclone is used in several derivatives, such as getclone2, which allows precise clone finding. getclone can be used to overcome some of global code's actor variable problems. getclone returns an invalid actor if there is no actor. An invalid actor has a cloneindex of -1, and its name is an empty string.


getclone Input

cloneName

  • Any Actor name or clonename in the game. Unlike most gE functions, this does not support use of "Event Actor", "Collide Actor", "Creator Actor", "Parent Actor", or similar. The reason is that getclone will search for an actor with a matching clonename as the char* cloneName. Also, getclone will search for the first match, so if you input the actor's name instead of clonename, it will find the lowest indexed clone.


getclone Output

It is important to note that getclone will not return a NULL when it cannot retrieve a pointer to the actor. Instead it returns a nonexistent actor with a cloneindex of -1, and name of "". To make sure getclone succeeded, you can use the following code.

Actor* clone = getclone("actor.3");
if(clone->cloneindex!=-1)
{
    // code here
}


Using an Actor*

This is further discussed in Actor*, but simply put, to access an Actor*'s variables, use '->'. For example:

getclone("actor.3")->x=100;

This will set actor.3's x equal to 100.


Example:

Global functions have a problem accessing an actor's variables using actorName.actorVar. In order for you to be able to use that system, you will have to put a comment before the global function call, with actorName in it. This logically doesn't make sense, as comments should be ignored, so one way of bypassing it is by using getclone. The following global code would not work properly.

Global Code

void
moveToLevel ()
{
    view.x=100;
    view.y=100;
    player.x=view.x+view.width/2;
    player.y=view.y+view.height/2;
}

To make that function work, you would need to call it like this. playButton -> Mouse Button Down (left) -> Script Editor

// view player
moveToLevel();


Instead you can use this Global Code using getclone, and not have to add the comment.

void
moveToLevel ()
{
    Actor* viewRef = getclone("view");
    Actor* playerRef = getclone("player");
    viewRef->x=100;
    viewRef->y=100;
    playerRef->x=viewRef->x+viewRef->width/2;
    playerRef->y=viewRef->y+viewRef->height/2;
}