Obtaining pointer to any arbitrary actor is OVERLY slow.
Doing if-checks in 100 actors is still faster (especially if the phenomenon
Branch Prediction gets involved)!
So in terms of efficiency, I would go with the Jolteon's straightforward approach.
Assuredly, If I had to choose between either of these.
OP:
You are invoking
Undefined Behavior in your code, by committing
Stack Overflow. Specifically in that part of your code:
- Code: Select all
char array [1];
sprintf(array, "long text" ....
Don't ever do that again, always allocate enough space to contain the formatted string.
And no, LcL, this isn't an array of ONE character, it is an array of TWO characters, which index is either 0 or 1.
Only in order to be a valid STRING, the last character must remain NULL. (If you already know the length of the string you don't really need it to be null-terminated)
Now, concerning the performance (since it appears to be widely questioned in here)
I wouldn't go with neither of your approaches. Sure, obtaining a pointer to any actor on startup time so to not slow down the gameplay is a great idea.
Hblade
I think it is very wasteful to create a function for something that could be achieved on the same basis in one line.
Not only wasteful but a bad practice as functions are meant for processing, not to spare some lines. Macros are meant to spare some lines.
If you use gE1.4.1b you can use the
getclone2 function, just like that:
- Code: Select all
Actor* c = getclone2(myactor.name, 27); c->yvelocity = 2;
or
- Code: Select all
getclone2(myactor.name, 27) -> yvelocity = 2;
where myactor is the actor you want to get a pointer to and where 27 is its clone index.
If you don't use the latest Game-Editor, then create the function yourself:
- Code: Select all
#define ACTOR_MAX_NAME_LENGTH (26)
#define ACTOR_MAX_CLONES_DIGIT (3)
Actor* getclone2 (const char cloneName[ACTOR_MAX_NAME_LENGTH + 1], const int cloneIndex)
{
char newName [ACTOR_MAX_NAME_LENGTH + 1 + ACTOR_MAX_CLONES_DIGIT + 1]; // maximum 999 clones for a reason
// If padding 0s were not a concern..
//sprintf(newName, "%.*s.%.*i", ACTOR_MAX_NAME_LENGTH, cloneName, ACTOR_MAX_CLONES_DIGIT, cloneIndex);
sprintf(newName, "%.*s.%i", ACTOR_MAX_NAME_LENGTH, cloneName, cloneIndex);
return getclone(newName);
}
If you declare
- Code: Select all
Actor* c;
in global code, you can initialize it from any actor's code:
- Code: Select all
c = getactor(myactor.name, 27);
And use it everywhere.
In functions that require the target actor to be presented as a string, you can do:
- Code: Select all
c->clonename
Example:
- Code: Select all
MoveTo(c->clonename, 0.000000, 0.000000, 2.000000, "Game Center", "");
or
- Code: Select all
MoveTo(getclone2(actor.name, 2)->clonename, 0.000000, 0.000000, 2.000000, "Game Center", "");
Depending on your intentions with the actor pointer.