Iterate Through Instances

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Sun Jun 21, 2009 12:57 am

I swear, this is my last question for a while. :P Okay, now that I know how to set up a static vector for storing pointers to other objects with the behavior, I need to know how to iterate through the pointers in the vector and retrieve data. I know it involves something similar to this:

[code:h8yfsghd]for (i = sameBehavior.begin(); i != sameBehavior.end(); i++)
{

}[/code:h8yfsghd]
But I don't know how to properly setup the iterator or retrieve data from the current instance.

Sorry if this is more general C++ than SDK specific, but I can't seem to find what I need anywhere else. Any help would be greatly appreciated. :)
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692

Post » Sun Jun 21, 2009 8:47 am

Don't be afraid to ask questions - this is what the forum is for. Each question you ask gets answered and stays here, readable by everyone who decides to dabble with SDK. So, yeah, you're in fact helping form a FAQ :P
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Sun Jun 21, 2009 9:40 am

An iterator is defined as what it is you are iterating.

vector<CRunObject*>::iterator i = sameBehavior.begin();

Then continue as you were with the loop. Because you're iterating pointers, you access the data of the CRunObject like so: (*i)->.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sun Jun 21, 2009 5:08 pm

[quote="Mipey":37c2v3re]Don't be afraid to ask questions - this is what the forum is for. Each question you ask gets answered and stays here, readable by everyone who decides to dabble with SDK. So, yeah, you're in fact helping form a FAQ :P[/quote:37c2v3re]
Blah, you're probably right. :P

[quote="Rich":37c2v3re]An iterator is defined as what it is you are iterating.

vector<CRunObject*>::iterator i = sameBehavior.begin();

Then continue as you were with the loop. Because you're iterating pointers, you access the data of the CRunObject like so: (*i)->.[/quote:37c2v3re]
Sweet! It works! I was doing something very similar to this before, but this actually worked. :P Thanks for the help, Rich. :)
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692

Post » Sun Jun 21, 2009 7:14 pm

Just thought I'd mention this for anybody else doing something similar to this. If you're making a behavior and want to share the properties of the objects they're linked to, you should use this:

[code:ncps86ui]sameBehavior.push_back(pLink);[/code:ncps86ui]
Not this:

[code:ncps86ui]sameBehavior.push_back(this);[/code:ncps86ui]
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests