Godot get dictionary value via variable(s)

Lust Demon

Member
Apr 22, 2018
100
85
I created a dictionary and I'm trying to get a value out of it based on variables. I've spent hours searching and trying various things and can't get it to work. Considering the content, I don't feel comfortable posting the question elsewhere. The best I can get it to do is to return a text string of what should be the key, based on inputs in the char creation scene. The documentation for GDscript only tells you how to add more stuff, not recall it, and searches only yielded results which required a better understanding of advanced scripting, that I don't have, to understand. Any help would be appreciated.

You don't have permission to view the spoiler content. Log in or register now.

You don't have permission to view the spoiler content. Log in or register now.
 

lobotomist

Active Member
Sep 4, 2017
834
746
sorry I don't really know gdscript that well, but given what you're trying to do this should be a cleaner more extensible way to do things:
 
  • Like
Reactions: Lust Demon

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
1,005
[the code you need is at the bottom, read the rest for my little adventure explaining things :p]

As far as I remember GDscript is like java, C# or C++. no wait, it was suppose to be like a better python (based on how it uses var and func, it il like a mix of python and C#). I use to use it around the 3.0 update, and I had to stop, I wanted to make games, not rewrite the documentation, but all the documentation for API was wrong after the 3.0 rewrote them. So I don't know whath the current situation is in the Godot zone of the internet.

Looking at your code it looks like a 4 level dictionary (body, then height, then body type, then breast size), and it looks like you are trying to use a technique where you can get the exact breast type that matches the body type (to height, etc) by layering these systems together.

Smart in theory, but your execution seems to lack an understanding of how to use variables, how the code interpret code, and the concept of pointers (which is hidden under the code).

you are making a dictionary called bra, with heights 0 1 and 2, in each heights you have body types 0 to 4, and in each body type you have tit types 0 to 4.

The issue is how you are setting the variables and getting them
Code:
var myDictionary = {4: 5, "A key": "A value", 28: [1, 2, 3]}; # coppied from the documentation,
you want to use an key, the colons, and a value. however, currently you are just saying H0 = { stufff}, H1 = { more stuff}
all you are doing is putting in arrays without a key value, so try H0 : {stuff}, H1 : { more stuff}

the next issue, H0 is nothing more than a pointer to the array, the code does not actually user H0 as the name for this pointer under the hood. after the previous fix, H0 is now just an undefined variable and you are going to get an issue, instead do this, add quotes

Code:
"H0" : {stuff}
by doing this, it should work as you are trying to use it with

Code:
var size2 = "H%s.B%s.T%s"
var size3 = size2 % [pcvar.height2, pcvar.body2, pcvar.tits2]
var size4 = bra.size3
#pcvar.bra_size = bra.key0.key1.key2
almost

because it now handles "H0" as a key string, which is what you are trying to do with
Code:
"H%s" % pcvar.height2
the height goes into the string, and by passing this into the dictonary as a key, it can pull out the {stuff} which is just a pointer to the next dictonary level.

however your code is just going to commit suicide again.

Code:
var size2 = "H%s.B%s.T%s"
I see what you are trying to do there, however, this is not going to work. what you are trying to do is use the '.' operation to access the next dictionary, however, when you put it in a string, this is no longer code, this is literal text. Not to mention, that is to assess a submember/function, that actually does not treat it as a dictionary.

you have to act with each dictionary separately

to get stuff out, use
Code:
dictionaryName["key"]
to get the contents out of the dictionary).

so the code you want would be more like this

Code:
var key0 = "H"+str(pcvar.height2)
var key1 = "B"+str(pcvar.body2)
var key2 = "T"+str(pcvar.tits2)

var if_you_want_height = bra[key0] # I forget if this languages uses ; at the end of each line or not.
var if_you_want_body_type = if_you_want_height[key1] # uses the previous variable as a pointer to the next layer in the dictionary
var if_you_want_tit_size = if_you_want_body_type[key2] # uses the previous variable as a pointer to the next layer in the dictionary

# key 0 should be bodyHeight, key 1 bodyType, and key 2 titIndex or some good variable names

# OR IF YOU ONLY CARE ABOUT TIT SIZE

var size = bra[key0][key1][key2] # each [] concludes what we assume to be a pointer to another dictionary, so we use 3 [] assuming it is a multilayer dictionary, it is similar to the code above, but does not waist time saving pointers or other tricks
So this would be your final code

Code:
# where ever you make this variable

var breastSizeDictionary = {    "H0" : {    "B0" : {    "T0" : "28AA", # hopefully this is a better way of organizing the text
                                                        "T1" : "28B",
                                                        "T2" : "28D",
                                                        "T3" : "28F",
                                                        "T4" : "28H"
                                                    },
                                            "B1" : {    "T0" : "30AA",
                                                        "T1" : "30B",
                                                        "T2" : "30D",
                                                        "T3" : "30F",
                                                        "T4" : "30H"
                                                    },
                                            "B2" : {    "T0" : "32A",
                                                        "T1" : "32C",
                                                        "T2" : "32E",
                                                        "T3" : "32G",
                                                        "T4" : "32I"
                                                    },
                                            "B3" : {    "T0" : "34B",
                                                        "T1" : "34D",
                                                        "T2" : "34F",
                                                        "T3" : "34H",
                                                        "T4" : "34J"
                                                    },
                                            "B4" : {    "T0" : "36C",
                                                        "T1" : "36E",
                                                        "T2" : "36G",
                                                        "T3" : "36I",
                                                        "T4" : "36K"
                                                    }
                                        },
                                "H1" : {    "B0" : {    "T0" : "30AA",
                                                        "T1" : "30B",
                                                        "T2" : "30D",
                                                        "T3" : "30F",
                                                        "T4" : "30H"
                                                    },
                                            "B1" : {    "T0" : "32A",
                                                        "T1" : "32C",
                                                        "T2" : "32E",
                                                        "T3" : "32G",
                                                        "T4" : "32I"
                                                    },
                                            "B2" : {    "T0" : "34B",
                                                        "T1" : "34D",
                                                        "T2" : "34F",
                                                        "T3" : "34H",
                                                        "T4" : "34J"
                                                    },
                                            "B3" : {    "T0" : "34C",
                                                        "T1" : "34E",
                                                        "T2" : "34G",
                                                        "T3" : "34I",
                                                        "T4" : "34K"
                                                    },
                                            "B4" : {    "T0" : "36D",
                                                        "T1" : "36F",
                                                        "T2" : "36H",
                                                        "T3" : "36J",
                                                        "T4" : "36L"
                                                    }
                                        },
                                "H2" : {    "B0" : {    "T0" : "30A",
                                                        "T1" : "30C",
                                                        "T2" : "30E",
                                                        "T3" : "30G",
                                                        "T4" : "30I"
                                                    },
                                            "B1" : {    "T0" : "32B",
                                                        "T1" : "32D",
                                                        "T2" : "32F",
                                                        "T3" : "32H",
                                                        "T4" : "32J"
                                                    },
                                            "B2" : {    "T0" : "34C",
                                                        "T1" : "34E",
                                                        "T2" : "34G",
                                                        "T3" : "34I",
                                                        "T4" : "34K"
                                                    },
                                            "B3" : {    "T0" : "34D",
                                                        "T1" : "34F",
                                                        "T2" : "34H",
                                                        "T3" : "34J",
                                                        "T4" : "34L"
                                                    },
                                            "B4" : {    "T0" : "36E",
                                                        "T1" : "36G",
                                                        "T2" : "36I",
                                                        "T3" : "36L",
                                                        "T4" : "36M"
                                                    }
                                        },
                                "H3" : {    "B0" : {    "T0" : "32B",
                                                        "T1" : "32D",
                                                        "T2" : "32F",
                                                        "T3" : "32H",
                                                        "T4" : "32J"
                                                    },
                                            "B1" : {    "T0" : "34C",
                                                        "T1" : "34E",
                                                        "T2" : "34G",
                                                        "T3" : "34I",
                                                        "T4" : "34K"
                                                    },
                                            "B2" : {    "T0" : "36D",
                                                        "T1" : "36F",
                                                        "T2" : "36H",
                                                        "T3" : "36J",
                                                        "T4" : "36L"
                                                    },
                                            "B3" : {    "T0" : "38E",
                                                        "T1" : "38G",
                                                        "T2" : "38I",
                                                        "T3" : "38K",
                                                        "T4" : "38M"
                                                    },
                                            "B4" : {    "T0" : "40F",
                                                        "T1" : "40H",
                                                        "T2" : "40J",
                                                        "T3" : "40L",
                                                        "T4" : "40N"
                                                    }
                                        },
                                "H4" : {    "B0" : {    "T0" : "34C",
                                                        "T1" : "34E",
                                                        "T2" : "34G",
                                                        "T3" : "34I",
                                                        "T4" : "34K"
                                                    },
                                            "B1" : {    "T0" : "36D",
                                                        "T1" : "36F",
                                                        "T2" : "36H",
                                                        "T3" : "36J",
                                                        "T4" : "36L"
                                                    },
                                            "B2" : {    "T0" : "38E",
                                                        "T1" : "38G",
                                                        "T2" : "38I",
                                                        "T3" : "38K",
                                                        "T4" : "38M"
                                                    },
                                            "B3" : {    "T0" : "40F",
                                                        "T1" : "40H",
                                                        "T2" : "40J",
                                                        "T3" : "40L",
                                                        "T4" : "40N"
                                                    },
                                            "B4" : {    "T0" : "42G",
                                                        "T1" : "42I",
                                                        "T2" : "42K",
                                                        "T3" : "42M",
                                                        "T4" : "42O"
                                                    }
                                        }
                        } # end of breastSizeDictionary
                      
# later on in the code, define your function
# this function lets you use any character
# so for example mainCharacterBraSize = braSize(pcvar)

func braSize(characterParameter)
    var heightIndexingKey = "H"+str(characterParameter.height2)
    var bodyTypeIndexingKey = "B"+str(characterParameter.body2)
    var titIndexingKey = "T"+str(characterParameter.tits2)
    return breastSizeDictionary[heightIndexingKey][bodyTypeIndexingKey][titIndexingKey] # returns only the final bit of text, the braw size, like "32B" for H2B1T0
WARNING this only works if the GDscript documentation is accurate, which I don't know it is since I do know it use to be very wrong in the past. this is why I stopped using godot, because it was still too early in the development for general use.
 
  • Like
Reactions: Lust Demon

Lust Demon

Member
Apr 22, 2018
100
85
[the code you need is at the bottom, read the rest for my little adventure explaining things :p]

As far as I remember GDscript is like java, C# or C++. no wait, it was suppose to be like a better python (based on how it uses var and func, it il like a mix of python and C#). I use to use it around the 3.0 update, and I had to stop, I wanted to make games, not rewrite the documentation, but all the documentation for API was wrong after the 3.0 rewrote them. So I don't know whath the current situation is in the Godot zone of the internet.

Looking at your code it looks like a 4 level dictionary (body, then height, then body type, then breast size), and it looks like you are trying to use a technique where you can get the exact breast type that matches the body type (to height, etc) by layering these systems together.

Smart in theory, but your execution seems to lack an understanding of how to use variables, how the code interpret code, and the concept of pointers (which is hidden under the code).

WARNING this only works if the GDscript documentation is accurate, which I don't know it is since I do know it use to be very wrong in the past. this is why I stopped using godot, because it was still too early in the development for general use.
Godot itself is written in C# and borrows a lot from python and lua. Actually, they keep adding in more python stuff because that's what people want.

That is exactly what's going on. I'm bumbling my way through learning programming as I work on this project. I've written/tweaked little snippets before, but never did anything on this scale. You have no idea how much I appreciate the explanation.

Yeah, the documentation is notoriously bad. And I'm finding that most of the 3rd party explanations skip over levels of knowledge/understanding because "everyone already knows that" and/or "it would take too long to explain." When I need to figure out a new concept or need the terminology for something, I usually search for it in python first and then use that to figure it out in GDscript.

Thanks again. I will definitely try that out later today. Even if the code doesn't work, the explanation alone, will help immensely.
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
1,005
if you are ok with a brief intro to some core ideas, here are some key things to know

var number = 5

a variable is like a jar, it holds data, and the jar has a lable. in reality "number" is the keyword that represents some place in memory where the data is stored. the language handles are the dark magic related to memory managment. the key word var just says that you are making a variable. In languages with a property called "strong type" you actually have to use special key words that let the program know what kind of variable you are going to use, and to the language knows how to handle how the keyword "number" is used in code. Python and GDScript are "weak type" and will on the fly figure out what to do and data can be transformed however you want.

Not all variables hold data, like text, number, etc. There are other variables that hold pointers, references, or other names for the same thing. Rather than acting like raw data, they point to a location in memory where the data is actually at. This is very powerful as you can do lots of neat things. an example is to have a variable that points to a color scheme in your User interface. if that variable is shared with all the graphical user interface icon, than what you can do is treat the pointer as if it were data (different languages do this in different ways), such as myBackgroundColor = colorScheme.background, where colorScheme is a pointer and you can use the "." operator to treat it like an object with submembers. the trick is you can set colorScheme to equal (point to) a different color scheme, and you only need to do this once and all you UI objects/elements will change color because now all the code asking for color automatically reroutes to a new set of colorScheme data.

while in some languages you have to handle pointers directly (very dangerous, possible to point to the wrong thing, if your operating allows, you can even corrupt your computer) or it is handled automatically. The most common case of automatic handling is data types that hold more than one value (Ignoring old fashion structs from c). This is because it groups the data together, and just have you point to the first bit of data. The code then has to figure out how to handle the pointer.

Arrays are an example of multiple variables and something being handled in a very special way
int myPrimeNumbers[5] = {1,3,7,13,23}; // from any C languages
int firstPrime = myPrimeNumbers[0];

because the code understands that myPrimeNumbers is an array type variable, it allows me to code it in certain ways (such as using [] ) and automatically handles pointers for me. if i tried this with other data types, it may not work, such as if they are not arrays, or if they are an array of a different data type.

Then there are more general uses of pointers, such as what python and GDScript use. Both python and GDScript are called interpreter languages. what happens is that code (C for python and C# for GDScript) reads your code and simulates your code. your code does not actually run, but rather a different set of code interprets your code and simulates it. so for python, a C code reads through it, sees you need variables, and sets those up. Because of this, the code can be very flexible (weak type) because it can figure out what you need and change on the fly (a var can have a number, and then later be replaced with the string version of that number. the variable change from holding an integer/float/double/long data type to string type), but it comes at the cost of being the slowest code execution of all language types.

general pointers are what interpreter languages use a lot. a general pointer is a pointer that points to and represents a concept called an "object" or class. since about mid 90's "object oriented programming" [OOP] has been the industry's favorite language paradigm. The idea is to organize code such that it represents objects or ideas. objects can hold data (like hair color) and functions specific to that object (like a TNT block in minecraft can explode). These concepts are intuitive so they are easy to learn... at first. How it is handled in code however can be very complex, and I have heard how OOP can be great or absolute garbage and why most programmers should know other paradigms, but its industry standard because (as some claim) the industry and hire new programmers cheaper this way because it is easier to get them started.

for example, a general pointer is just a pointer to an object, and often objects can be used to act as containers. I did this with java to get it to simulate C# behavior. containers can have all sorts of properties, mainly, var in your code is actually an object, with code to check the variable, return the data, or changes state if need be. so in actuality your code is purely pointer based, but it handles everything in a way that looks nicer and does not require manual pointer handling because the interpreter code handles that, but just know everything you are doing is a lie because the interpreter is what is actually doing the data management.

I think learning interpreter languages (or at least mastering them) is harder than other language types because you have to learn how other languages work and then how an interpreter works and then you can figure out what the crazy error messages mean. because the code is not actually doing what you think it is doing, it is just trying to figure out what you what to do, and does it in its own crazy round about way due to being an interpreter code that actually does the work.

I say C# is better to learn, but there are no easy ways to learn it (Unity kinda is a pain to learn because it too much info at once and not enough guidance). You can learn the core of coding using Processing (java like api, no complex code handling), I think once you know how to make a game engine (a game loop is like a drawing phase, phsics phase, etc), you pretty much understand code at that point. From that point on it is all about learning tricks. Infact, I started to have a lot of fun with C# and just started to research all the hidden things you can do and i tried to write a list down... but then I realized something. once you understand how to do something, how to put it in code, you will reuse the same trick over and over again every time you need to perform the same task (such as using event calling to get objects to communicate with eathother, such as when a player wants to look in someone else's inventory without a direct function pointing to the unknown object). So what I ended up doing reacently is started to make my own language, a translator language. it only has 4 commands really, but you describe what you want, and it recognizes the core behaviors used in game making, and it takes your description and turns it into code (I am thinking of translating to C# because I love it so much, despite it being a half interpreter language, just in time complaining, very high level so not as fast as C or C++ but so much more easier to understand and code for with things like delegates to allow for the code to restructure itself on the fly just as how pointers allow for colorscheme changing). So far I have been working on the UI stuff because it tries to guide the programmer with the assumption that they have never coded before, so I don't have a working model yet. now I am just rambling.
 

Lust Demon

Member
Apr 22, 2018
100
85
I don't mind the rambling. I learned a long time ago that if you shut up and listen when someone else does it, you learn stuff. Might not be immediately applicable, but you learn stuff.

Without getting into my own personal stuff, let's go with "my understanding of things is oddly spotty because most of my career was in a related field." So, I understand things like "holding data" and "truth tables", which I used to use all the time; albeit with hardware. And I understand the concept of high/low/machine level code; though only minimally. I'm still learning how much I don't know; though it's punctuated with occasional bits of clarity from other stuff. Not sure that I'm really explaining that in a way that's understandable.

Pointers is a semi-new concept to me; I know it happens, but I never really thought about it because I didn't need to. Dealing with translation issues like "No Mr. Script, that's a '0' not a boolean false" has been entertaining. Pretty sure most of what I've scripted so far is very "hamfisted" and will be for quite some time. I'll look back at in a few months and go, "WTF was I doing?!" LOL. It's the way these things go.

Honestly, I put off learning this side of things for over a decade because I quickly realized that I didn't have the patience for it. That seems to have changed somewhat with time.

Even if I never finish it, the project or more specifically, the act of working on it, has been useful.
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
1,005
I understand things like "holding data" and "truth tables"
You sound like an electrical engineer, I'm one, but programming is quite important with today's education (last year of college).

Honestly, I put off learning this side of things for over a decade because I quickly realized that I didn't have the patience for it.
kinda similar but kinda the opposite for me, in just before 8th grade I wanted to make computer games (I had just learned my first language the summer before, pbasic), but I read some where you needed to have a good understanding of physics (like the math for how mario jumps). math scared me (despite it being my only skill set), so I wanted to wait to senior year to learn physics when they teach you it... after half a year I got bored, so I went to study physics, turns out I had to learn calculus for that, etc, etc. it's not so much about knowing before hand, it's more about running into a brick wall, finding issues and slowly ripping through them, that is the best way to learn I think. skill is nothing more than knowing your own past stupidity. So you're doing fine, but I certainly wouldn't have tried to start out with godot, still too alpha for me.
 

lobotomist

Active Member
Sep 4, 2017
834
746
I just wanna point out how would i do this bear in mind that my experience is mostly unity, but the general idea should apply to godot (if you watched the tutorial I posted earlier).

create a base resource that contains:
  • String for girl's name.
  • An enum for the breast size. (or better yet a reference for a 3D morph, (think DAZ's shape dials) with it's amount)
  • An enum for body type. (same thing here for morph)
  • A float for the height.
  • optional values (or morphs) for misc things like hairstyle, butt size etc...
  • A reference to the 2D sprite or 3D model
You could also add useful things to your base resource, like:
  • Love amount
  • A numerical id (for ease of identification)
  • clothes equipped (im not sure about this one, as I've not yet delved deep into an inventory system, might be better to make it a separate resource)
  • Fun stats like amount of fucks, bj's etc...
  • Her current mood
You can then create a dictionary (or list or array?) wich consists of an ID or name and the girl resource. To keep track of all the girls. then on the onready function load based on resource.
 
  • Like
Reactions: Lust Demon

Lust Demon

Member
Apr 22, 2018
100
85
You sound like an electrical engineer, I'm one, but programming is quite important with today's education (last year of college).
Depends on the sub-field. With what I specialize in, I'll never touch programming on the job.

kinda similar but kinda the opposite for me, in just before 8th grade I wanted to make computer games (I had just learned my first language the summer before, pbasic), but I read some where you needed to have a good understanding of physics (like the math for how mario jumps). math scared me (despite it being my only skill set), so I wanted to wait to senior year to learn physics when they teach you it... after half a year I got bored, so I went to study physics, turns out I had to learn calculus for that, etc, etc. it's not so much about knowing before hand, it's more about running into a brick wall, finding issues and slowly ripping through them, that is the best way to learn I think. skill is nothing more than knowing your own past stupidity. So you're doing fine, but I certainly wouldn't have tried to start out with godot, still too alpha for me.
Again, depends on what you actually do. More and more they're requiring degrees for jobs that don't really need them. If you're in your final year, I guess you've found that calc either gets easier or it gets impossible, depending on how you think. Ironically, you'll likely never use it unless you work in a lab somewhere doing cutting edge stuff. When it comes to what the college tells you, you will and won't need in the real world, just remember, "It's not like it is in the books."

BTW, current and now working function code at the bottom.

I just wanna point out how would i do this bear in mind that my experience is mostly unity, but the general idea should apply to godot (if you watched the tutorial I posted earlier).

create a base resource that contains:
  • String for girl's name.
  • An enum for the breast size. (or better yet a reference for a 3D morph, (think DAZ's shape dials) with it's amount)
  • An enum for body type. (same thing here for morph)
  • A float for the height.
  • optional values (or morphs) for misc things like hairstyle, butt size etc...
  • A reference to the 2D sprite or 3D model
You could also add useful things to your base resource, like:
  • Love amount
  • A numerical id (for ease of identification)
  • clothes equipped (im not sure about this one, as I've not yet delved deep into an inventory system, might be better to make it a separate resource)
  • Fun stats like amount of fucks, bj's etc...
  • Her current mood
You can then create a dictionary (or list or array?) wich consists of an ID or name and the girl resource. To keep track of all the girls. then on the onready function load based on resource.
I did watch it and it was pretty informative. Currently, I only have the player's scripting to date and it's not finished yet. Using class for the npc's or for the master copy to have the system build them from sounds about like how I'm going to end up doing it when I get that far.

"pcvar" in the example above is a singleton script (most likely the wrong term) that only holds the player's variables that need to be saved or are used across more than one script, and a dictionary for saving them to it. And the only real need for the dictionary, at this point in that script is so that I can save the data to disk. Some of it will have to differ for npc's since their stats will be different, but should work as a starting point for the others.

A good chunk of the mechanics I want to implement, I have the math for; and most of the starting dialogues in a series of Word docs. To date, I've spent less than a month with the game engine and 6 months off and on writing the other stuff.

You mentioned art... ugh. I keep going back an forth on that. I'm great with logic, numbers, etc. but I have horrible artistic sense. I know how I want to make the base system, but I'll have to feel it out for the GUI and I doubt I'll sort much out otherwise than backgrounds and profile stuff.

My current plan is to get Act 1 functional, figure art out, and then add in the rest of the stuff to make the game world feel more like a game world (random events, more choices for key things, more dialogue choices, etc.). Some of that is planned, but I kept cycling back to adding more mechanics in and well... yeah... You've got to stop adding requirements at a point, figure out the minimum, and get to work.

That said, Saki_Sliz your function example and explanation was the key. I didn't need to change the dictionary itself at all (lua style/syntax works). I just tested the following and the profile field I wanted to set the text for is now setting the way it should be. Thanks!

You don't have permission to view the spoiler content. Log in or register now.
 

lobotomist

Active Member
Sep 4, 2017
834
746
BTW this is unity but the gist of it should apply to godot.

This vid just came out today and it kinda reminded me of what I said about making them separate entities. He doesn't really use scriptable objects, but you can see in the comments how they are a good alternative.

Also at the end he mentions the factory pattern wich might come in handy in your situation.