I am trying to set up a generic button object which can be configured with variables to perform variations on an action.
What is the expected result
I have a number of different resources (Energy, Battery, System) that can be transferred between each other. For example, I can move Energy into Battery or Battery into System. This all works fine with simple events to check Min and Max values, control the rate of flow and when the transfers can take place (e.g. pressing R moves Battery into Energy until Energy is full or Battery is empty)
This is controlled by object variables on the Player or scene variables
Player.Energy.CurrentEnergy
Player.Energy.MaxEnergy
Player.Battery.CurrentBattery
Player.Battery.MaxBattery
System.CurrentSystem
I am now creating a menu system to allow the player to action these transfers. This has a “Transfer” object which is the button the player interacts with, one for each direction between each resource pair.
I could obviously create manual expressions based on a button ID but I want to have the Transfer object have a pair of variables which configure the From and To side of the transfer.
Transfer.FromResource (set to the name of the required source resource variable)
Transfer.ToResource (set to the name of the required destination resource variable)
Then the event when the Transfer button is selected would be something like:
Change the variable [Transfer.FromResource] of Player: subtract 0.1
Change the variable [Transfer.ToResource] of Player: add 0.1
What is the actual result
I’ve tried dynamic variable references using forum examples going back over the last 8 years and none of them make sense. I’ve tried creating custom behaviours and functions but they feel like I’m going further down the wrong path. There must be a way to wrangle this. I’m going to need a similar system later when I have equipment slots with different variables being effected. Basically, I’m out of ideas, inspiration and don’t know where else to look to find a way around this.
What about a general bank of energy that can be used for any type? As you drag a slider, it takes it from the bank and when you decrease a slider, it would go into the bank,
I don’t know if it would be a 1 for 1 swap or 1 resource would cost more than the other. I used the built-in sliders. You might want to use draggable sprites locked into the Y or some other system. That could be discussed if this type interests you. The sliders might show the maximum they can be increase to depending on the amount in the back. You would also need to prevent them from being dragged past that point.
It’s just a matter of subtracting the sliders from a maximum amount. The amount wouldn’t be large enough to fill all of the slider. The pool could grow as the game progressed.
I’ve got a UI using buttons and a resource bar object for each resource type as the balance between the resources and the risk/reward they represent is a key mechanic. I use the same resource bar objects in the main UI so there is continuity.
Sliders might be an approach to completely avoid the problem and at least get things moving again. I know I could hard code what I need as well but that creates tech debt for later in the project. I think I’m still going to have to get my head around the dynamic variable references at some stage as they are going to be important for the many power ups I want to implement.
edit: here’s the project (click the green [code] and then [download zip])
Edit 2: I decided to add a 2nd set of buttons that use to and from object variables instead of the ray cast.
You can use a ray cast to find the slider above and below the buttons. Once selected, you can check the values. This just increases and decreases.
You can store the values in a structure using an Name object Variable. You wouldn’t want this running constantly just when needed either though an event or an extension as an action. The resource objects have a name variable. The structure could be a structure of structures and it could contain the minimum and maximum values or steps or exchange rate. Whatever data you need.
Last addition to use object variable for up2 and down2:
This took some setting up. I added object variables to and from to the up2 and down2 arrows. The code is simpler. The up2 and down2 use the same code. So, I put them into a group.
You can modify the resource bar and/or the variable here or later.