I made an actor class that has a function.
The function is GetItemMesh, this function returns the static mesh of the static mesh component.
The thing is, I am trying to call this function from a CLASS, not a spawned actor. How can I do it? The function doesn’t appear when I grab the class node?
To call a function on a class you have to either make it a static class or instantiate it or cast an object of the same base class to it.
in this example I’ve cast “Self” to “ClassExample” and call a “New Function” on it after it has been cast to the class with the function I want. Self should be replaced with an externally referenced(collected) instance of the parent class of the object from which you would like to call the specific functions.
(“This example is terrible, because apparently you can not cast the parent class to one of its children type within blueprint”) This is not at all what you are trying to achieve.
May i suggest instead creating a function library and implementing the function there?
Creating a new function library would provide you the same functionality but with more flexibility and portability of the function. You can also share function libraries between projects if you put them in the Engine Content folder.
Example result:
The above image shows “New Function 1” as a node but without requiring a cast.
This is awesome. Much thanks!!!
but I didn’t understand the casting… I 't exactly know the Class at my situation. I am receiving the class as a “variable”
For reference: http://i.imgur.com/j4Z8xgR.png
I know that the Hands class is a parent class of a specific class that I have the functions on though
That’s fine, what I mean is, you 't need to know the class if all you’re doing is calling a function. You can create a function library with inputs that perform actions on inputs instead of specific functions in a specific class. This can also be achieved with an interface. Implementing the interface will then allow you to implement functionality of a function in multiple objects with the same name but with a different implementation.
Casting is what you would want to do if you wanted to access objects of a specific base type and wanted to check if the item was a specific child of that base type to then access specific functionality created in that child.
For instance, if i wanted to collect all the enemies in the game, i could then cast them to specific enemy types and if they succeed in casting, perform specific actions on/from those objects.
It’s like entering a room and surveying all the people to find those who’s names are or Michelle or Jesus… If they are not any of these three people, then you 't want anything to do with them, however if they pass the test, you have something you would then want to tell them or ask them to do. You wouldn’t perform that same test on a chair though. Trying to cast a piece of furniture to a person would be silly.
Another example:
Collect all the animals in the zoo. Now feed them.
First you gotta know what kind of animal they are, so you check to see what kind of animal they are by accusing them of being specific types of animal. If you evaluate an animal that is a dog as a cat, it will fail. Upon fail, you should check to see if it is another animal (hopefully a dog). If all cases fail you should then handle that by giving yourself some kind of warning in the log.
You get the idea.
A weird thing i’ve noticed is that you can’t cast parent class of animal to dog or monkey within animal. This could be a blueprint limitation, but if you try to cast within Animal base class to Dog or Cat, you will get a message that says “Dog does not derive from Animal” even though it does.