Concatenate strings to form variable reference

  • Hi everyone,

    I want use a function to change variables in Object A. The function is supposed to look up the value in Object B. For that I want to compose the variable reference from some strings, but I cant get the syntax right to have the function understand when it's a string and when a reference.

    Hope I'm making myself clear here.

    imgur.com/ZfncmIo

    btw how are images supposed to work now?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Please tell me it's possible!

    I'm getting bad vibes when looking at similar posts ...

  • That "E_Config_something" - is it some object, like a sprite, with Health instance variable?

    .

    Unfortunately, what you are trying to do is not possible. You can't refer to objects or variables using their string names.

    If you give more details about what you are trying to achieve, I may be able to tell you how it could be done better.

  • hey dop2000! yeah I was afraid so.

    I have enemies that have a rather large set of instance variables that define their behavior and how they go through a shared state machine. those are things like reaction ranges, fire rates, weapon type, etc, based on their archetype.

    To further make them more flexible i want to exchange the full set of instance variables, to basically swap out their entire behavior, like going from ranged to melee combat.

    The way i go about it so far is by swapping the enemy out for a separate object entirely, same enemy with different variable values. But that creates a huge configuration overhead that i'd like to get rid of.

    So i was hoping to just fetch those value sets from some kind of config table; i was looking into dictionaries, but found them unwieldy to set up and maintain for each archetype and all their variations.

    i now wanted to just use empty enemy objects for data storage, that via family would be easy to get set up and update. but not being able to reference them dynamically kinda breaks the concept.

    I've decided to go with a json table, which is at least somewhat convenient to edit and traverse.

    I'd be happy to hear about other ideas though! (Sorry for the long read ...)

  • I would recommend using a dictionary. Hear me out:

    You need to assign each instance of Enemy its own unique instance of dictionary. If you have only one Enemy object, it's super easy - put the dictionary and enemy objects into the same container. If you have multiple Enemy objects in a family, containers will not work. You'll have to "manually" create a dictionary instance and link it with enemy instance, for example using Enemy_UID instance variable.

    .

    Dictionaries have a few advantages over instance variables:

    • they are expandable, add as many keys as you need
    • you can access keys using strings - that's what you want!
    • you can easily swap data - Dictionary1 load from JSON Dictionary2.AsJSON
    • it's easy to save them in local storage
    • etc.
  • dop2000

    And that's the thing, I do have multiple enemy types, all sharing a family, each with a container holding different sets of objects per enemy type.

    I was considering dictionaries, but if i want a centralised way of exchanging the enemy configurations - not a massive switch statement with each variable of each type listed - I'll end up in the same situation, wanting to map dynamical references to enemy variables. Plus I'd have to maintain each enemy type's dictionary individually, to eg keep them in identical.

    thanks for talking through this with me though, appreciate the input!

  • I was considering dictionaries, but if i want a centralised way of exchanging the enemy configurations - not a massive switch statement with each variable of each type listed - I'll end up in the same situation, wanting to map dynamical references to enemy variables.

    I don't quite understand. Say, you need to swap Orc's configuration with Zombie configuration. You don't need any switch statements, you can do this literally with two simple actions - just re-assign Orc's dictionary to Zombie and Zombie's to Orc.

    Plus I'd have to maintain each enemy type's dictionary individually, to eg keep them in identical.

    I'm not sure if I explained it correctly - you will only need one universal dictionary object for all enemy types. On creation of any "Enemies" family member, you spawn a new Dictionary instance and "attach" it to the enemy instance. You can also create a bunch of helper functions for getting/setting values in the dictionary, for easier management.

    • you can easily swap data - Dictionary1 load from JSON Dictionary2.AsJSON

    dop2000

    Oh, now wait - I missed that bit earlier.

    Do I get you correctly there, each enemy type could have an instance of the same dictionary via their container, hence the state machine can reference the variables/keys - since its from the same dictionary for everyone.

    I'd load the enemy type's data into it upon spawning, and also swap the data per instance when needed?

    But where would I store and edit the different configurations before loading them into the instances? I would still need an external json file, right?

    Now that would mean rewiring the entire state machine to the dictionary, but it does sound great. If that's what you're saying?

  • each enemy type could have an instance of the same dictionary via their container

    No.. Since you have several enemy sprites, you can't add one dictionary object to their containers. You will need several dictionaries (one for each enemy type), and this will only make things more complicated.

    What I suggest is that you don't add the dictionary into enemies' containers. You create one dictionary object "Stats", add an instance variable "Enemy_UID" to it. Then do this:

    On EnemiesFamily created 
     -> Create Stats
     -> Stats set Enemy_UID to EnemiesFamily.UID
    

    And now you have an instance of Stats for each enemy instance, linked by Enemy_UID.

    If you need to access Stats dictionary for any given enemy, you will need to pick Stats instance first using an event like "Stats compare variable Enemy_UID=Orc.UID". It's a bit inconvenient, but as I said, you can create a couple of helper functions for this. For example, you call "GetEnemyStat" function, pass two parameters - Orc.UID and "health" - and it returns health value of this orc.

    .

    I'd load the enemy type's data into it upon spawning, and also swap the data per instance when needed?

    But where would I store and edit the different configurations before loading them into the instances? I would still need an external json file, right?

    Yes, if you need to load some default stats data into every enemy (like base health, damage, speed) you will probably need to create a bunch of JSON strings. Or you can use some other data storage, CSV table for example.

  • Gotcha, thanks for elaborating.

    I like that approach.

    Funny to think how much easier all this could be if C2 had simple editable arrays.

  • C3 does have an Array Editor.

    In C2 I like using CSV plugins - you create a table in Excel, save as CSV and import to Construct. CSV is a text file, so it's easy to make changes to it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)