Hundreds of features to explore
Games made in Construct
Your questions answered
Trusted by schools and universities worldwide
Free education resources to use in the classroom
Students do not need accounts with us
What we believe
We are in this together
World class complete documentation
Official and community submitted guides
Learn and share with other game developers
Upload and play games from the Construct community
Game development stories & opinions
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
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()
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.
Develop games in your browser. Powerful, performant & highly capable.
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:
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:
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:
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.