Why this code doesn't work?

Learn how to make certain types of games and use gameEditor.

Why this code doesn't work?

Postby koala » Mon Apr 06, 2015 10:42 pm

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!
Attachments
problem.zip
(1.81 KiB) Downloaded 163 times
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Why this code doesn't work?

Postby bat78 » Mon Apr 06, 2015 10:53 pm

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", "");
Last edited by bat78 on Fri Apr 10, 2015 4:59 pm, edited 4 times in total.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Why this code doesn't work?

Postby lcl » Mon Apr 06, 2015 10:56 pm

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. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Why this code doesn't work?

Postby bat78 » Mon Apr 06, 2015 10:58 pm

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
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Why this code doesn't work?

Postby koala » Mon Apr 06, 2015 11:00 pm

Thank you very much! :D

Can I use strcpy() instead of sprintf()?
Phascolarctos cinereus
YouTube: Marko Radivojevic
Google+: Marko Radivojevic
User avatar
koala
 
Posts: 301
Joined: Thu Mar 26, 2015 7:03 pm
Location: Serbia
Score: 30 Give a positive score

Re: Why this code doesn't work?

Postby bat78 » Mon Apr 06, 2015 11:04 pm

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.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest