Page 1 of 2

Change a particular clone parameter

PostPosted: Mon Apr 20, 2015 11:25 am
by lverona
When the game starts, I have 30 clones created. Is there a way later on in the game to, say, change yvelocity of a particular clone, say, clone number 26?

Re: Change a particular clone parameter

PostPosted: Mon Apr 20, 2015 2:40 pm
by koala
lverona wrote:When the game starts, I have 30 clones created. Is there a way later on in the game to, say, change yvelocity of a particular clone, say, clone number 26?
Hi, lverona! :D

Global code:
Code: Select all
#define MAX 5 // number of clones
#define NAME 31 // clone's name length

char cloneName[NAME]; // clone's name
Actor *clonePlayer; // pointer to a clone


Global Variables:
- index - clone's index
- acc - initial index value after you press [SPACE]

view -> Create Actor -> Script Editor:
Code: Select all
acc = -MAX; // initialize
When you press [space] first time and start the program, acc will be 0.

choose (text) -> Draw Actor -> Script Editor:
Code: Select all
sprintf(choose.text, "choose clone: %i", index);
This shows index of current clone.
Every frame it activates this code because it is in Draw Actor. Writes string in choose.text.

view -> Key Down (space) -> Repeat: Disable -> Script Editor:
Code: Select all
int i;

// delete all previos clones
for (i = acc; i < acc + MAX; i++) {
    sprintf(cloneName, "player.%d", i);
    DestroyActor(cloneName);
}
// first i goes from 0 to 4, next time i goes from 5 to 9, and so on,
// because when you destroy previous clones and create new, those new clones have their indexes
// new clones will have new indexes

acc += MAX;

index = acc;

// create new clones
for (i = 0; i < MAX; i++)
    CreateActor("player", "square", "no parent", "no path", (i * 100 - 200), 0, true);

// create text and border, but only when game is started
if (acc == 0) {
    CreateActor("choose", "no animation", "no parent", "no path", -280, 200, true);
    CreateActor("border", "no animation", "no parent", "no path", -300, -220, true);
}


Changing index:
When left arrow key is pressed index decreases:
view -> Key Down (left) -> Repeat: Disable -> Script Editor:
Code: Select all
index = (index - 1 < acc) ? acc + 4 : index - 1;

Similarly, when right arrow key is pressed index increase:
view -> Key Down (right) -> Repeat: Disable -> Script Editor:
Code: Select all
index = acc + fmod (index + 1, MAX);


Moving up and down (on y-axis):
Moving up:
view -> Key Down (up) -> Repeat: Enable -> Script Editor:
Code: Select all
sprintf(cloneName, "player.%d", index);

clonePlayer = getclone(cloneName);
(*clonePlayer).y -= 5;

Moving down:
view -> Key Down (down) -> Repeat: Enable -> Script Editor:
Code: Select all
sprintf(cloneName, "player.%d", index);

clonePlayer = getclone(cloneName);
(*clonePlayer).y += 5;


player (main actor) -> Collision: When collides with border_top, or border_bottom activate Physical Response and set Final Velocity Multiplier of Event Actor (player) to 0. This Event needs to be repeated.

border (canvas actor) -> Create Actor -> Script Editor:
Code: Select all
erase(0, 0, 0, 1);

setpen(255, 255, 255, 0, 1);

lineto(width - 2, 0);
lineto(width - 2, height - 2);
lineto(0, height - 2);
lineto(0, 0);
Draw border when canvas actor border is created.

Actors Filled Regions: border_top, border_bottom; border_left and border_right are not necessary because clones move up and down.

Links to files same as in attachments:
executable file:http://www.mediafire.com/download/392q0f6orxf8s70/clone_motion_exe.zip
source (ged and data files):http://www.mediafire.com/download/b61ulbm5cvtt1ry/clone_motion_ged.zip

Re: Change a particular clone parameter

PostPosted: Mon Apr 20, 2015 3:27 pm
by MrJolteon
koala wrote:tl;dr

Or instead of doing all that, just use the built-in cloneindex variable:
Code: Select all
if(cloneindex == 420)
{
    //whatever you want clone #420 to do
}

Re: Change a particular clone parameter

PostPosted: Mon Apr 20, 2015 5:51 pm
by lverona
Guys!

Big thanks for the very detailed replies. I had to add more details, but I think part of what you suggested would still be applicable.

So I have 30 clones of an actor.

How can I change yvelocity of clone number 26 from another actor, say, an actor that awaits a button press? So this is not something I can necessarily do from within the clones.

My hunch was that you can do ThatActor.26.yvelocity=3;
But this does not work. You can do ThatActor.yvelocity=3; But it will target the lowest index actor, by default ThatActor.0

Re: Change a particular clone parameter

PostPosted: Mon Apr 20, 2015 6:04 pm
by MrJolteon
lverona wrote:Guys!

Big thanks for the very detailed replies. I had to add more details, but I think part of what you suggested would still be applicable.

So I have 30 clones of an actor.

How can I change yvelocity of clone number 26 from another actor, say, an actor that awaits a button press? So this is not something I can necessarily do from within the clones.

My hunch was that you can do ThatActor.26.yvelocity=3;
But this does not work. You can do ThatActor.yvelocity=3; But it will target the lowest index actor, by default ThatActor.0


Code: Select all
if(actor.cloneindex == 26)
{
    actor.yvelocity=3;
}

Re: Change a particular clone parameter

PostPosted: Tue Apr 21, 2015 12:02 am
by lcl
Code: Select all
getclone("ThatActor.26")->yvelocity = 3;

It's better to learn to use getclone than to use if checks for cloneindex. Think if you have hundreds of clones and you tell them all to check if their cloneindex is 26 just in order to change that specific clone's yvelocity.. Not efficient.

Re: Change a particular clone parameter

PostPosted: Tue Apr 21, 2015 4:01 am
by koala
lverona wrote:When the game starts, I have 30 clones created. Is there a way later on in the game to, say, change yvelocity of a particular clone, say, clone number 26?
Maybe this topic can help you somehow. It doesn't refer to your question, but it uses clones, so it might help. :)
http://game-editor.com/forum/viewtopic.php?f=27&t=13558

Re: Change a particular clone parameter

PostPosted: Tue Apr 21, 2015 4:43 am
by lverona
lcl wrote:
Code: Select all
getclone("ThatActor.26")->yvelocity = 3;



Yes! This is the syntax I was asking about!

But now is there a way to make the number of clone random? I tried variations but could not find a way to incorporate a variable into getclone.

Re: Change a particular clone parameter

PostPosted: Tue Apr 21, 2015 10:58 am
by lverona
I tried this, but it does not seem to do anything.

Code: Select all
char random_item[1];
short int ch;

ch=rand(26);

sprintf(random_item, "item1.%d", ch);
getclone(random_item)->yvelocity=-3;

Re: Change a particular clone parameter

PostPosted: Tue Apr 21, 2015 12:02 pm
by lcl
The problem is in your definition of the variable random_item. You've made it to be an array of characters that is only one character long. Then you're trying to store in it something that is either 7 or 8 characters long. And obviously that doesn't work. Make the array to be lengthy enough, put anything bigger than 7 in there and it'll work.

Re: Change a particular clone parameter

PostPosted: Wed Apr 29, 2015 12:55 pm
by lverona
Thank you, it worked when I added characters.

char random_item[8];

Re: Change a particular clone parameter

PostPosted: Sun May 03, 2015 1:22 pm
by Zivouhr
This is an awesome thread with very helpful information, and a great first question too! Thank you all. 8)
I was trying to figure out without any references, how to change the color of an individual clone, but I kept trying to code with case 0, case 1 and couldn't get it to work.

clone/create actor/script editor:
Code: Select all
if (cloneindex==22)  // "robot.22" is the one clone of say, 100, that I need to change color or do something.
{
b=-55;
g=235; //both make yellow
}



The GetClone code is also helpful too.

Re: Change a particular clone parameter

PostPosted: Sun May 24, 2015 2:51 pm
by lverona
What if I want to use this construction in a MoveTo function? This function will not accept a char.

Re: Change a particular clone parameter

PostPosted: Sun May 24, 2015 2:55 pm
by lverona
Also, how do I read variables from a clone?

Re: Change a particular clone parameter

PostPosted: Sun May 24, 2015 3:46 pm
by koala
You read variables form a clone using
getclone(clone_name)->variable

Argument of MoveTo() shouldn't be char, but string. String is an array of chars that ends with '\0'.
You can make a string this way:
Code: Select all
char name[31];

sprintf(name, "clone.%d", clone_num);