added data not saving in array?

Hello all! I am currently working on my first major project, which is a tower defense game. after getting working towers and enemies, I tried implementing the system for upgrading towers, but there seems to be an issue. no matter what I try, I cannot seem to have the array properly save added upgrades to the tower. the code of how I am currently saving possible upgrades for a tower is shown below

because I am currently just trying to get upgrading working, the upgrade data is hard coded into the tower itself (i intend to make a data structure to better store this later). in this case, damage and speed upgrade are children of the upgrade base class (which is what unlockableupgrades is an array of), which holds necessary functions and the structure for upgrade data. the delay and extra calls for the upgrade objects are parts of attempts I have tried to solve this issue, though none of them had made any changes. getunlocks is a function from the upgrade class to find upgrades it unlocks when purchased, but it doesnt have any actual impact on this area of the program.

Whenever I print out the data of the upgrades, it seems to work perfectly, but when I call for the data within the array it comes completely empty. I should note that it does not give the default values of the upgrade object, and just returns an empty value of the variable in question (i.e. level comes back as 0, when default is 1, and name comes back empty, when default is “sample”), meaning that the array’s data is completely empty for some reason. I have very little experience with this coding language previously, so what may be causing this issue?

Ok… I have 2 questions:

  1. What is the type/class of the upgrades and by extension the type of the objects in the array?
  2. Where/when are those objects (Speedupgrad and Damageupgrade) created?

My guess is they are either never created or they are destroyed when you try to access them.

(note that object reference type variables are not initialized and there are no objects they point to by default)

2 Likes
  1. the base upgrade class is an object blueprint. speed and damage upgrades are both their own classes, which are children of the base upgrade class. the array is of base upgrade, because I thought that the children would count as the parent for being within the array.

  2. the speed and damage upgrades are hard coded into the tower blueprint’s variables section (where this screenshot is taken in). I assigned the defaults in their respective blueprints, and as a precaution I initialized their variables in this blueprint as well (all of their variables are in the structures created in the “make upgradedata” section of the screenshot)

I was not aware you had to initialize an object variable in the events list like that, so this is likely the issue I have run into. I am away from my pc atm, but I will try to initialize them when Im back to my desktop

Check out this setup:

My guess is that you are missing either the Construct or the setting. If you don’t set it into a variable the garbage collector might clean it out.

Other than that - this is a local object. You’ll have to do some C++ work to make it replicate.

1 Like

I see, ok. When i get back to my desktop tonight I will try to initialize the variable before sending it into the array to see if that fixes it. Admittedly most of my programming experience is in standard text based coding, so I am inexperienced with block based structures such as this. Thank you for the advice!

after getting back to my desktop, I tested out constructing the object first, and that seemed to fix the issue I was having. thank you

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.