Get Objects With Same Behavior

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

Post » Fri Jun 19, 2009 11:26 pm

Is there a way to find and loop through objects that are using the same behavior? Basically, the behavior would be applied to different objects, and then each object with the behavior would be able to get the other objects' locations and stuff. Is this possible? A quick code snippet to illustrate would be handy. :)
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Fri Jun 19, 2009 11:35 pm

All you need is a static variable. Something like a static vector of instances in your CRunObject, which you add the 'this' pointer to in OnCreate and remove in the destructor. Then, all objects have access to the list of all objects with that behavior.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sat Jun 20, 2009 6:30 am

So would I put something like this in CRunObject?

[code:jm9vetpa]static vector<Type> sameBehavior;[/code:jm9vetpa]

And if so, what's the type supposed to be?
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sat Jun 20, 2009 8:05 am

I think it would be CRunObject* type, link
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sat Jun 20, 2009 3:28 pm

That's right, and you would do something like:

sameBehaviors.push_back(this); // in OnCreate()

sameBehaviors.erase(find(sameBehaviors.begin(), sameBehaviors.end(), this)); // in destructor

You'll need to include <algorithm> for std::find.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sat Jun 20, 2009 3:37 pm

Okay, I put this in CRunObject:

[code:tbmhib2w]static vector<CRunObject*> sameBehavior;[/code:tbmhib2w]

And this in OnCreate()

[code:tbmhib2w]sameBehavior.push_back(this);[/code:tbmhib2w]

But when trying to compile, I get this:
[code:tbmhib2w]Runtime.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class CRunObject *,class std::allocator<class CRunObject *> > ExtObject::sameBehavior" (?sameBehavior@ExtObject@@2V?$vector@PAVCRunObject@@V?$allocator@PAVCRunObject@@@std@@@std@@A)[/code:tbmhib2w]

I think it has something to do with it being static, but I'm not sure.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sat Jun 20, 2009 3:51 pm

In C++, static class member declerations also need to be declared in a .cpp file so the linker knows about them. The following line in runtime.cpp should fix it:

[code:2nr3odo6]vector<CRunObject*> CRunObject::sameBehavior;[/code:2nr3odo6]

This is not an SDK specific thing, you need to do this any time in C++ you declare a static member.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sat Jun 20, 2009 4:44 pm

When I put that in, it claims that the vector isn't a member of CRunObject. If I change the code to this:

[code:3rm47ee5]vector<CRunObject*> ExtObject::sameBehavior;[/code:3rm47ee5]

It doesn't complain, but I don't think it would work. I did a simple test with a static integer, being sure to declare it in runtime.cpp using:

[code:3rm47ee5]int ExtObject::integer;[/code:3rm47ee5]

Then in OnCreate() I have the value increased by one. I then set up an expression to return the value, but the value is always returned as 0. Am I still doing something wrong here?
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Sat Jun 20, 2009 11:42 pm

Oops, yeah the correct syntax is ExtObject. It should work OK with a static member variable declared like that. I'm not sure why it wouldn't, a couple of other behaviors already use this method to access other behavior's data :P
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sat Jun 20, 2009 11:50 pm

Okay, thanks for all the help. :) I'm gonna try and get all this working so I can really start work on the plugin. I'll likely be back later with some other question. :P
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Next

Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests