i have a signal and it has the var name pokemon and lets say pokemon s value is “mudkip”
lets also say i have a dictionary named mudkip
i want this function to return said dicionary does this make any since?
funnily enough i think this bing ai came up with a good answer for this one
var pokemonDicts: Dictionary = {
“mudkip”: mudkip,
“pikachu”: pikachu,
# Add other Pokémon dictionaries here
}
func getMudkipDictionary(pokemon: String) → Dictionary:
return pokemonDicts.get(pokemon, {})
I usually use arrays, but worst case scenario, a Dictionary really is the solution here. Not sure why you would need that, but I’ve made weirder code before.
idk man it just makes it look nicer for me i could do it all in one script file but i chose the hard route
That’s really discouraged because later down the line, when your project is bigger, it becomes really hard to read code that was made without explicit variable names, it’s the whole reason why obfuscation exists in the first place.
If you’re doing something novel or you think in a different way than is standard or it’s one of those situations where there is no other way to do things, then there might be an argument about it being useful, but it is generally not a good idea. I don’t know what your project will look like, so don’t be discouraged to do things your way, but do keep this in mind.
its just like a list of pokemon names that i have to append to whenever i add a new mon it shouldent be that bad when its all contained in its own file
You can make things less prone to mistakes with enums.
class_name Database
static var Pokemon : Dictionary = {PokeName.Mudkip : "Mudkip", PokeName.Pikachu : "Pikachu"}
Just save a simple enum script in a folder.
enum PokeName
{
Mudkip,
Pikachu,
}
class_name Pokemon extends Node
@export var Pokemon_ID : PokeName
var CurrentPokemon : String :
get : return Database.Pokemon[Pokemon_ID]
set(value) : Database.Pokemon[Pokemon_ID] = value
This is so much better (i have never used enums before) less typing for me!
heres some of my games very goofy gameplay
Define your dictionary
mudkip = {
“type”: “Water”,
“ability”: “Torrent”
}
Define the function
def get_pokemon_dict(pokemon_name):
# Use globals() to access the dictionary by its name
return globals().get(pokemon_name, None)
Example usage
pokemon = “mudkip”
result = get_pokemon_dict(pokemon)
print(result) # Output: {‘type’: ‘Water’, ‘ability’: ‘Torrent’}
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.