Page 1 of 1

Why this code doesn't work?

PostPosted: Mon Apr 06, 2015 10:42 pm
by koala
The idea is to make 5 squares on position (x, y) = (200, 200) and make those 5 squares move to positions 0, 0; 10, 10; 20, 20; 30, 30 and 40, 40 respectively. I've wrote this code:
view->Create Actor->Script Editor
Code: Select all
int i;

for (i = 1; i <= 5; i++) {
    CreateActor("player", "square", "no parent", "no path", 200, 200, true);
    MoveTo("player", i*10, i*10, 10, "Game Center", "");
}
It just moves all of them to position 0, 0 (it doesn't have to do anything with the fact that position of first square is 0, 0, it'll anyway move all of them to the center).
Why it doesn't work?

Thanks!

Re: Why this code doesn't work?

PostPosted: Mon Apr 06, 2015 10:53 pm
by bat78
When you create actors that already exist, you area dealing with clones.
Typically for them is their member cloneindex, being increased as the number of clones grow. clonesindex is 0 for regular actors as well as first clone.
Keep in mind these 3 members, part of the Actor structure.
name - Returns name of the actor as a string
cloneindex - Returns actor's clone index
clonename - returns the name + the clone index (e.g "square.0")

If you have looked at the Game-Editor Scripting Reference you'll notice that the function CreateActor returns pointer to that Actor. Which means you have access to the clone's members including the clone's index.

So your code will look more like this:
Code: Select all
int i;

for (i = 1; i <= 5; i++) {
    Actor* clone = CreateActor("player", "square", "no parent", "no path", 200, 200, true);

    MoveTo(clone->clonename, i*10, i*10, 10, "Game Center", "");
}


Also note that indexing in C starts from 0, not from 1.
So change your loop statement to:
Code: Select all
for(i = 0; i < 5; i++)

and the body's index requirement to
Code: Select all
MoveTo(cloneName, (i + 1)*10, (i + 1)*10, 10, "Game Center", "");

Re: Why this code doesn't work?

PostPosted: Mon Apr 06, 2015 10:56 pm
by lcl
bat78 wrote:So your code will look like this:
Code: Select all
int i;

for (i = 1; i <= 5; i++) {
    Actor* clone = CreateActor("player", "square", "no parent", "no path", 200, 200, true);
    char cloneName [32];

    sprintf(cloneName, clone.clonename);
    MoveTo(cloneName, i*10, i*10, 10, "Game Center", "");
}

Except the sprintf line should be clone->clonename, because you're dealing with a pointer. :)

Re: Why this code doesn't work?

PostPosted: Mon Apr 06, 2015 10:58 pm
by bat78
lcl wrote:
bat78 wrote:So your code will look like this:
Code: Select all
int i;

for (i = 1; i <= 5; i++) {
    Actor* clone = CreateActor("player", "square", "no parent", "no path", 200, 200, true);
    char cloneName [32];

    sprintf(cloneName, clone.clonename);
    MoveTo(cloneName, i*10, i*10, 10, "Game Center", "");
}

Except the sprintf line should be clone->clonename, because you're dealing with a pointer. :)


Yeah lol I saw that the time I reviewed my post to put the note haha. Wasn't quick enough. You came up out of nowhere by the way O_O

Re: Why this code doesn't work?

PostPosted: Mon Apr 06, 2015 11:00 pm
by koala
Thank you very much! :D

Can I use strcpy() instead of sprintf()?

Re: Why this code doesn't work?

PostPosted: Mon Apr 06, 2015 11:04 pm
by bat78
koala wrote:Thank you very much! :D

Can I use strcpy() instead of sprintf()?


Yes you can. That is not a huge difference as the speed in a loop as such will be not tangible.
Thought I would prefer sprintf as for the safety.