I’m relatively new to Unreal blueprints but I can code so bare with me.
My Goal
I am creating a 2d top down space ship game. I am trying to use the AttachActorToComponent node to attach the ships turret configuration. I have made a few empty actors (no mesh or code) just arrows for where I want projectiles to spawn, the arrows in these various blueprints have the tag “Turret”. My idea was on firing/mouse click, to use GetComponentByTag to find “Turret” and spawn a projectile for each one. That way, no matter what weapon the player currently has, it will always work. I’m effectively trying to write it so the player can eventually get upgrades like double shot, triple show, diagonal arrow etc and it just switch the configuration by destroying the old config and attaching a new one. Maybe there’s a better way?
My Issue
I need an array of all the possible weapon configurations/actor blueprints. That way I can search the array for getDisplayName == desiredWeapon and add the result to the actor variable currentWeapon. Then use currentWeapon, getComponentByTag “Turret” to spawn projectiles. I tried to make an actor array in the GameMode and it’s telling me I can’t edit it in class defaults.
Can anyone think of a way around my issue or a better way of doing it? I wanted this way as it could evetually lead to modular ships, if I can figure out how to switch in and out components on the fly.
You need an array of all possible Weapon fonfiguration-Actor.
Have you made an array of the right type?
Maybe you dont need an actor reference, you need a ““Weapon-Configuration-Actor”” reference. If you make an actor blueprint, you can have the same kinf of actor-reference
Let me know if you find out, if not post a screenshot please.
By the way:
Be careful that if you plan to have multiple type of werapon active or different cooldown for them,
your search for all actor with tag will be messy.
I suggest you to create a most complex system,
where you save the type of weapon in some variables, to work like slots.
So when you fire, you can check,
the main weapon is active? have ammo? is in cooldown? ok shoot,
The side weapon is too?
if you save reference to the weapons and build the guns firing in different actors , you will get a modular ship game.
Your ship should call
Fire to the Main weapon, with LeftMouse
Fire to sideweapon with Right mouse
Then get the reference to the actor you have there, and you dont know wich weapon is, and via Blueprintinterfaces, say her to fire.
Then the Actor weapon laser, will spawn 60 little energy dots, and then wait for a cooldown of 5 sec before be active again.
the Vulcan weapon wiill shoot always when called but only 4 bullet for second
Where do I put the array though? That is my main issue, I don’t know where to save/store this array of configurations. I tried to put it in the game mode but it won’t let me add defaults. I didn’t want to put it in the player character because I will probably need the same array for AI. How do I group all the weapon configurations in to 1 searchable array to be accessed on BeginPlay
I made a C++ Class called WeaponConfigurations with the parent class actor. That way every weapon is a WeaponConfiguration class. I wanted to make an array of WeaponConfiguration which contains all the weapons. Then I want to be able to search it and attach one. Where do I save this array?
Or is there a better way?
Eventually I want several arrays for hull, wings, cockpit, weaponconfig etc. I want to be about to switch them in and out seamlessly as the player upgrades/buys new parts. Is there a better way to do this?
Try making Structures
You have to go in blueprint - structures, and make all kind of data, here a sample of arrays.
Basically this structure now is a “default”,
going in any actor and adding a variable of this type, will give them the same list.
If you have multiple kind of things you can also , keep them organized and then, in realtime, merge them togheterr to search for all kind of class.
In the end , another interesing thing.
Enumerators. if you dont know them , sometimes they are useful.
Thanks Est, useful to know but not really an answer to my question! I wanted to know WHERE I save this array as it wasn’t letting me set defaults in the GameMode.
I fixed my issue myself but I’ll answer my own problem in case anyone reading this has a similar problem:
I couldn’t write default values in my array of actors because I was saving it as Object References, not Class References. As soon as I changed it I could put the array in my GameMode and write defaults to it.