How To Set Actor Components To Be Other Components?
I have a tank game that uses modular upgrades for your tank that do different things. The way I'm designing the modularity in blueprints is that I'm using scene components that simply get tacked on to a tank blueprint. I have a base class with all the variable declared, and the inheriting class sets them based on its diversity. How do I set these blueprints?
Gameplay will want to change what references my tank mods will hold. For example Cannon is a type of Gun Mod and there are other Gun Mods that can be plugged into this variable.
I can make it a purple class (whatever that means) instead of a blue reference, even then I still can't find other Gun Mod types in the list to set this variable to.
I can however use the set function at runtime to achieve this with the purple class. But I can't for whatever reason call functions from it.
There is where in code I would do this->gunMod = new Cannon(); So what is the blueprint equivalent of instantiation?
Other posts similar to my problem.
Well, better proceed to points:
1) A reference is a pointer to a memory allocation. What it mean? Whenever you spawn an actor (or construct an object) you allocate a little part of the memory to keep the data of the class you spawned. All that data can be edited at runtime. The reference contain the required address to find the data. If you set a ref variable = to an other ref you DON'T obtain 2 objects, you make both the variables points to the same object.
2) A class (purple variable) points instead to your """pure""" object. For ex: You have a class called: "fruit" with a bunch of variable (color, shape, peelThickness) and child classes (banana, apple, orange). Now if I spawn a banana (default color: yellow) you can set the color of THAT banana to blue in runtime editing the variable through the reference BUT if you get the banana class default you'll found that the color is yellow.
You can get variables from a class through "get class defaults" node but you can't call any method (because blueprints methods aren't statics). If you want to call a method you need to spawn that class and call object's method through ref. If you really need to call a function without spawning an object you can create a function in a Blueprint Function Library, that functions are static and you can call them whenever and wherever you want.
"Yeah doctor, it's all cool but how can I make my fu*king cannon to fire?"
Well if you have a method "fire" in your parent class and an overrided method foreach child class you can simply call "fire". Ex: I've the generic class "GunMod" with a "fire" event with no nodes attached, then I made a "BananaShooter" class, inherit from "GunMod" with a "fire" node that spawn big banana projectile, and a class "Orangethrower" (same inherit) with a method "fire" that spawn a cone of orange juice. You don't need to know what class your GunMod is to call "fire" event (because GunMod has a fire event), if it's a BananaShooter the event fire'll shoot a banana, if is an OrangeThrower will burn your enemy eyes.
EDIT: for what you are making you have to perfectly know classes, references and polymorphism. I'm ending a project similar to yours (not with tanks or other vehicles, don't worry) and the use of classes and polimorphism is fundamental.
Well this is super counter intuitive. I've been trying to find ways to instantiate an actor component and have it replace an existing component for days now and it turns out I have to "Add" a component first.
What ever Add is doing, its creating/spawning/instantiating the object I want and setting it to my gun mod variable. So now when I reference that variable to call a method from it, its called from Cannon and not Gun Mod anymore.
This was super frustrating to figure out.
answered Feb 02 '17 at 09:46 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here