The following code has been bothering me for a bit:
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Engine/BasicShapes/Sphere"));
sphere->SetupAttachment(RootComponent);
if (SphereVisualAsset.Succeeded()) {
sphere->SetStaticMesh(SphereVisualAsset.Object);
}
}
Namely that I am hard coding a path. What if I decided I wanted to use a different object down the road? Imagine I had multiple pawns all hardcoded to use this asset. In cpp there is no easy way to change all these references. I had the bright idea to expose my mesh choice to blueprints to leverage Unreal's reference handling like so:
UPROPERTY(EditDefaultsOnly, Category = "References")
UStaticMesh * m_staticMesh;
However the following does not create a mesh when I inherit from this class with a blueprint and set the default mesh (m_staticMesh):
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
sphere->SetupAttachment(RootComponent);
if (m_staticMesh) {
sphere->SetStaticMesh(m_staticMesh);
}
}
I'm using Unreal 4.21.2 both methods compile, but the latter fails to work.
Any suggestions on how to make the above function properly are appreciated. If you know of a better way to avoid hardcoding paths please let me know.
Using Static Constructor Helpers is generally not recommended at all.
Composing an Actor of Components and utilizing Unreals Reflection system to expose those Components and their properties to the Editor to be modified in Blueprints is the ideal pattern.
/* Weapon Mesh. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Weapons")
UStaticMeshComponent* Mesh;
Marking a Component that is created during construction with VisibleAnywhere will cause it to appear in the Component Hierarchy within the Actors Component view after creating a child Blueprint of that Actor.
You will then have access to modifying its properties such as the StaticMesh.
It is useful to know what the valid Property Specifiers are when working with UPROPERTY as well as all the Function and Class Specifiers and their MetaData.
Property Specifiers
Related
I'm trying to create simple game in C++. At one point I want to have some setting, save and load from config file.
The config file should be read from the beginning, and should be accessible anywhere it needed.
So far I only see Singleton pattern as a solution.
Another way is to create an object an pass it down, but it can mess
up the current code.
I've also search and found something called Dependency Injection.
Is dependency injection useful in C++
Which design patterns can be applied to the configuration settings problem?
But I don't quite understand it, you still have to create an object in main and pass it down, right?
Singleton is quite simple, but some consider it antipattern, while pass it down the tree can mess up my current code. Is there any other Patterns?
P/S: I'm also curious how games load their setting.
I would suggest something simple as the following example, which circumvents any singleton-related or initialization order issue:
struct global_state
{
config _config;
};
struct game_state
{
global_state& _global_state;
};
int main()
{
global_state globals{load_config_from_file()};
game_state game{globals};
game.run();
}
Since _global_state is a member of game_state, it can be used in member functions without the need of explicitly passing it as a parameter:
void game_state::update_ui()
{
const float text_size = _global_state._config.get_float("text_size");
_some_text.set_size(text_size);
}
I have a game object (a cube, let's say) which exists in the scene, and I want it to have an injectable component. I want to be able to say, for example: My cube has an IShotFirer member, which can resolve to either a BoomShotFirer or a BangShotFirer MonoBehavior component, both of which implement IShotFirer. When binding happens, I want this component to be added to the cube object.
public class CubeBehavior : MonoBehaviour
{
[Inject]
private IShotFirer shotFirer;
}
Is it possible to do this without 1) needing an existing prefab which contains one of these Bang/Boom components, or 2) needing an existing scene object which has one of these components attached?
In other words, I want to be able to dynamically add the component to my game object depending on the bindings, and not relying on anything other than the script files which define either BoomShotFirer or BangShotFirer. But the docs seem to imply that I need to find an existing game object or prefab (e.g. using .FromComponentsInChildren(), etc.)
Is it possible to do this without 1) needing an existing prefab which
contains one of these Bang/Boom components, or 2) needing an existing
scene object which has one of these components attached?
Yes, it is.
Zenject provides a host of helpers that create a new components and bind them -- quoting the docs:
FromNewComponentOnRoot - Instantiate the given component on the root of the current context. This is most often used with GameObjectContext.
Container.BindInterfacesTo<BoomShotFirer>().FromNewComponentOnRoot();
FromNewComponentOn - Instantiate a new component of the given type on the given game object
Container.BindInterfacesTo<BoomShotFirer>().FromNewComponentOn(someGameObject);
FromNewComponentOnNewGameObject - Create a new game object at the root of the scene and add the Foo MonoBehaviour to it
Container.BindInterfacesTo<BoomShotFirer>().FromNewComponentOnNewGameObject();
For bindings like this one that create new game objects, there are also extra bind methods you can chain:
WithGameObjectName = The name to give the new Game Object associated with this binding.
UnderTransformGroup(string) = The name of the transform group to place the new game object under.
UnderTransform(Transform) = The actual transform to place the new game object under.
UnderTransform(Method) = A method to provide the transform to use.
That list is not even exhaustive, be sure to check the readme and the cheatsheet (from both of which I have extracted the info above).
Also understand that, as usual, you can append .AsSingle(), .AsTransient() and .AsCached() to achieve the desired result.
I've created an instance of ATriggerSphere in my game, and placed it at my character's location, like so:
//Create activate trigger radius
activateRadiusTrigger = GetWorld()->SpawnActor<ATriggerSphere>(ATriggerSphere::StaticClass(),GetActorLocation(), GetActorRotation());
activateRadiusTrigger->SetActorHiddenInGame(false);
I need to adjust its radius now. I see that in blueprints there's a parameter under Shape that lets me change the value of "Sphere Radius", but I can't find the C++ equivalent field. Can someone tell me how that's done? Thanks!
If you look up TriggerSphere.h in the engine headers, you can find its public interface there. It looks like ATriggerSphere uses a collision component that you can get using GetCollisionComponent(). I'm going to assume this component is a USphereComponent, and you can call SetSphereRadius on that!
so try:
Cast<USphereComponent>(activateRadiusTrigger->GetCollisionComponent())->SetSphereRadius(NewRadius);
I want to create a contact listener in such a way that I'll be able to create a joint when sprites from different classes meet. I found a useful question and answer which has helped me - Getting the world's contactListener in Box2D partly. The following code and insrtuctions where recommended:
std::vector< std::pair<b2Fixture*, b2Fixture*> > thingsThatTouched;
//in BeginContact
thingsThatTouched.push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
//after the time step
for (int i = 0; i < thingsThatTouched.size(); i++) {
b2Fixture* fixtureA = thingsThatTouched[i].first;
b2Fixture* fixtureB = thingsThatTouched[i].second;
// ... do something clever ...
}
thingsThatTouched.clear(); //important!!
For this to work you'll need to make the thingsThatTouched list visible from the contact listener function, so it could either be a global variable, or you could set a pointer to it in the contact listener class, or maybe have a global function that returns a pointer to the list.
I am using Cocos2d and don't know much C++. How can I "make the thingsThatTouched list visible from the contact listener function" in Cocos2d? Should
std::vector< std::pair<b2Fixture*, b2Fixture*> > thingsThatTouched;
be in the ContactListener.h file? How will it differ in Cocos2d? Thanks.
Put this in a header file:
typedef std::pair<b2Fixture*, b2Fixture*> fixturePair;
typedef std::vector<fixturePair> fixturePairVector;
extern fixturePairVector g_touchingFixtures;
Then include the header wherever you need to use the list. You will also need to have this in a source file (.mm or .cpp) somewhere, just once:
fixturePairVector g_touchingFixtures;
Of cause, the typedefs are not necessary but they may help if you don't like looking at too many of the wrong kind of brackets.
You could store this list in a singleton class, then you can access it from anywhere, even C++ code. Something like that:
NSArray* things = [SomeSingleton sharedSingleton].thingsThatTouched;
I am working on a system that uses drools to evaluate certain objects. However, these objects can be of classes that are loaded at runtime using jodd. I am able to load a file fine using the following function:
public static void loadClassFile(File file) {
try {
// use Jodd ClassLoaderUtil to load class into the current ClassLoader
ClassLoaderUtil.defineClass(getBytesFromFile(file));
} catch (IOException e) {
exceptionLog(LOG_ERROR, getInstance(), e);
}
}
Now lets say I have created a class called Tire and loaded it using the function above. Is there a way I can use the Tire class in my rule file:
rule "Tire Operational"
when
$t: Tire(pressure == 30)
then
end
Right now if i try to add this rule i get an error saying unable to resolve ObjectType Tire. My assumption would be that I would somehow need to import Tire in the rule, but I'm not really sure how to do that.
Haven't use Drools since version 3, but will try to help anyway. When you load class this way (dynamically, in the run-time, no matter if you use e.g. Class.forName() or Jodd), loaded class name is simply not available to be explicitly used in the code. I believe we can simplify your problem with the following sudo-code, where you first load a class and then try to use its name:
defineClass('Tire.class');
Tire tire = new Tire();
This obviously doesn't work since Tire type is not available at compile time: compiler does not know what type you gonna load during the execution.
What would work is to have Tire implementing some interface (e.g. VehiclePart). So then you could use the following sudo-code:
Class tireClass = defineClass('Tire.class');
VehiclePart tire = tireClass.newInstance();
System.out.println(tire.getPartName()); // prints 'tire' for example
Then maybe you can build your Drools rules over the interface VehiclePart and getPartName() property.
Addendum
Above make sense only when interface covers all the properties of dynamically loaded class. In most cases, this is not a valid solution: dynamically loaded classes simply do not share properties. So, here is another approach.
Instead of using explicit class loading, this problem can be solved by 'extending' the classloader class path. Be warn, this is a hack!
In Jodd, there is method: ClassLoaderUtil.addFileToClassPath() that can add a file or a path to the classloader in the runtime. So here are the steps that worked for me:
1) Put all dynamically created classes into some root folder, with the respect of their packages. For example, lets say we want to use a jodd.samples.TestBean class, that has two properties: number (int) and a value (string). We then need to put it this class into the root/jodd/samples folder.
2) After building all dynamic classes, extend the classloaders path:
ClassLoaderUtil.addFileToClassPath("root", ClassLoader.getSystemClassLoader());
3) load class and create it before creating KnowledgeBuilder:
Class testBeanClass = Class.forName("jodd.samples.TestBean");
Object testBean = testBeanClass.newInstance();
4) At this point you can use BeanUtils (from Jodd, for example:) to manipulate properties of the testBean instance
5) Create Drools stuff and add insert testBean into session:
knowledgeSession.insert(testBean);
6) Use it in rule file:
import jodd.samples.TestBean;
rule "xxx"
when
$t: TestBean(number == 173)
then
System.out.println("!!!");
end
This worked for me. Note that on step #2 you can try using different classloader, but you might need it to pass it to the KnowledgeBuilderFactory via KnowledgeBuilderConfiguration (i.e. PackageBuilderConfiguration).
Another solution
Another solution is to simply copy all object properties to a map, and deal with the map in the rules files. So you can use something like this at step #4:
Map map = new HashMap();
BeanTool.copy(testBean, map);
and later (step #5) add a map to Drools context instead of the bean instance. In this case it would be even better to use defineClass() method to explicitly define each class.