Questions regarding arrays, data processing and good coding practices

0 favourites
  • 3 posts
From the Asset Store
A system for storing and processing information online
  • Hello! I'm working on an action side-scroller project and right now I'm trying to code the attack system. My questions are more like "what's the best way of doing this" than "how to do this". To give a little bit of context of what I'm trying to achieve:

    • Player character can cast a spell by pressing the attack button;
    • The equipped spell can be switched;
    • Different spells have different properties (e.g. MP cost, element...)

    I've created an array JSON file to store the spells and its properties and it's looking something like this (all values are placeholders):

    name MP cost power element type
    Fireball 5 2 fire basic
    Stalagmite 30 10 earth attack
    Ice Spike 20 12 water attack

    These values will always stay the same. There will be modifiers added to it (e.g. an equipment that raises fire damage), but those will be applied after getting the value from the array.

    Every time the player attacks, I need to know the MP cost, power and element of the currently equipped spell to calculate damage, reduce player's max MP, etc. I'm thinking that searching an array every time the player attacks might cause an unnecessary amount of data processing. Is this a valid concern?

    A possible solution that I thought was saving the current spell properties to player's instance variables or global variables. Every time the current spell is switched these values would be updated. Then, when the player attacks I can get these values directly from the variables instead of searching an array. Replicating these values don't sound like a good practice to me though, looking from a "clean code" perspective.

    I guess my main questions about this are:

    1. Is searching an array during every attack an issue, considering it will happen often?
    2. Is array JSON files a good way of storing the spells list? Any other best option?

    Another related question that I have is what's a good way of saving the player character's current properties, e.g. HP, MP, current equipments, learned spells, etc. Thought about creating a Dictionary specific for this, what do you think?

    Thanks in advance for any help provided!

  • You are definitely taking the right approach. But for this task I would probably recommend using JSON object. It will be easier to work with and you won't have to search the entire array when you need any bit of data.

    Here is a possible format:

    {
    "Fireball": {"mp": 5, "power": 2, "element": "fire", "type": "basic"},
    
    "Stalagmite": {"mp": 30, "power": 10, "element": "earth", "type": "attack"}
    }
    

    If you need to get the power value for the fireball for example, you can use this expression:

    JSON.Get("Fireball.power")

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That's exactly the type of approach I was looking for! Thanks for the suggestion, it looks much cleaner now!

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