0 votes

Hello, I need an opinion from more experinced game creators on how to manage this aspect of the project.

I created a template node for the "production sites" of my game: this template has a lot of parameters that need to be filled (e.g.: input resources, output resource, number of workers, ecc).
These parameters depends on what type of production sites is being built (e.g. all grain fields produce 10 food, all coal mines produce 15 coals, ecc), and i have to assign them at the moment of the node instancing.

Can you address me on what's the best way to fill in these variables?
Right now i am thinking of creating a long "match" sequence that assign the value of each variable for each case, however this would require to write hundreds of line of codes (#types of production sites*#of parameters) that are constantly repeating, and I can foreseen this will be an hell the moment i decide to change anything.

Is there a better way to do it? Maybe reading data from an external file? (excel, maybe?) but i feel like it might be too slow while in game.
How have you solved it in your projects?

Thanks a lot for sharing!

in Engine by (1,500 points)
edited by

2 Answers

0 votes

It sounds like you have lots of variables because you have lots of types of production sites. I would not put them together if they are not actually common variables. That means you should not need a giant match by type. Instead, one function (or factory object) per type may do a more focused job. You can make it work on a registration basis, like a dictionary mapping type to function/factory to call, so it's easier to add more.

If there are common variables, I'd group them into a common data structure, and leave the rest to specific ones, instead of putting them all together. For example, each production site might have a common pack of data which can be re-used, and then packs of specific data that are found in one or more sites.
If most of that data is constant, you could indeed set this up in a configuration file, resource, or create them in advance in a static helper script (which behave like quick internal config files if you use const and preload/load).

Otherwise, if the rest needs to be set dynamically from variables of the game, then... well face it, assign them^^ No point in trying to hide them somehow.
The point is, if you repeat yourself in some areas, use static helpers, factories, eventually pack into components if a type has multiple types of common data attached (i.e attaching child nodes instead of inheriting, or attach resources to group datas, or eventually use duck typing magic to still have them together), find what's constant, find what's variable.

I think there is never a "best way" (as so commonly asked), but there are useful patterns to apply depending on your situation.

by (29,042 points)
edited by

Thanks for answering!
Actually yes, every variable is in reality a constant, and it is a common one (meaning that every ProductionSite has it).
Right now i created this structure: in the parent node i decleared a set of arrays, one for each constant, and in every array is stored the value for each type of production site.
After I instance a new empty ProductionSite template, i define it using the values stored in the array of the parent node.Here a rough structure to understand:

parent node:

const type=["well", "field", "mine", "orchard"]
const output=["water", "grain", "stone", "fruits"]
const whatever=[...,...,...,...]

ProductionSite node:

var type
var output
var whatever
func define(n): #n identify which elements of the arrays to use
  type=parent.type[n]
  output=parent.output[n]
  whatever=parent.whatever[n]

It works as intended, but writing down all the properties of each production type is a pain when the arrays get longer and longer, and easier to do mistakes.
Maybe in the future i'll write down a piece of code that does the same thing that define() does, but taking the data from an actual excel file (i guess it's fine if slow, it should be done only once at the beginning)

0 votes

When you create new node, you can modify its variables. So, for example if you want to change variable xyz you can assign it:

node.xyz = "123"

You can store values in dictionary:

{
  grain: {
    food: 10
  },
  mine: {
    coal: 15
  }
}

You define type of node and script refers to global dictionary values

by (102 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.