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.
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.
So would I put something like this in CRunObject?
static vector<Type> sameBehavior;[/code:jm9vetpa] And if so, what's the type supposed to be?
I think it would be CRunObject* type, link
Develop games in your browser. Powerful, performant & highly capable.
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.
Okay, I put this in CRunObject:
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.
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:
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.
When I put that in, it claims that the vector isn't a member of CRunObject. If I change the code to this:
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?
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
Okay, thanks for all the help.