Hello,
I'm trying to figure out how to build a relationship matrix within Twine 2/Sugarcube 2. I feel I am missing something very basic here.
I have $people[$i] where my people are described. For starters they have attributes of $people[$i].id, $people[$i].business, $people[$i].age, $people[$i].gender, $people[$i].favoriteSportTeam, and so on. What I want to be able to do is add something like $relationship[$k].id (which would be a people[$i].id, $relationship[$k].relationship (mother, brother, friend, etc.) and $relationship[$k].relationshiptype (probably a numeric scale of -10 (hatred and loathing) to 10 (love and trust)).
Can you have multidimensional arrays in Twine/Sugarcube? If so, how exactly are they referenced? I've been struggling trying to word my searches to find an answer. My best guess is $people[$i].$relationship[$k].id (or other attribute), but that doesn't seem to be working.
Thanks!
Comments
That said, based on your descriptions, it seems like that's not what you want in the first place.
I'd give some examples, but you didn't bother to show examples of any of your code, so I can't say which would be the best way for you to do so.
To access someone's relationships, you'd do something like:
Also, have you though about using generic objects all the way down, rather than arrays? Which is better for your needs depends on what you're doing, obviously, but accessing such a system could look like the following:
Sorry about the lack of code. Let's see if this will give you a better idea. It's a bit more random in some of the details right now, but that's just so I can see what I can do with populating an array.
gives a result like:
So, the question is really how do I go about making a relationship matrix?
I want something that I can later put more code around so that I can add this kind of data.
Does that help?
Your examples show a refence scheme $people[$i].relationships[$k].id so if I am undestanding correctly (using my data above):
Am I understanding you correctly?
If the people referenced within the relationship objects should come from within the $people array, then you cannot add the relationship objects until the $people array is complete. Basically, you'll create each person's relationship array as part of their creation, though it will remain empty at that time. After the $people array is complete, you'll iterate through its objects filling in each person's relationships.
In short, since you're creating each person with randomized details, you cannot fill in their relationships until you have all of the people created.
So your current code would simply need to add a relationships property which holds an empty array. After the initial loop finishes creating all of your people, you'd then iterate over them all creating their relationships. For example: The /* TBD */ bits are the parts where I have no clue how you want to go about assigning relationship details. For example:
Randomizing simple details is fairly easy. Randomizing relationships/connections is a tad more complicated.
It seems like you're on the right track.
~Z
and I am getting the following error response from Twine:
For my testing, I have no story javascript nor any story stylesheet. I've attached a screen shot of the errors with the mouse hover-over.
It seems I have to define the relationships: [] part of the code before I can push them into the $people array. Am I interpretting that error correctly?
As to what is. I'm assuming that the issue is that $people is not being initialized as an array. I didn't include that in my original example as I kind of assumed that it was implied, and I didn't expect that you would not include it yourself. I did include the $names and $teams arrays, so I suppose I should have added $people as well. Mea culpa.
FIND: REPLACE WITH:
I see a few issues straight away, and, again, I suppose that I should have been clearer.
I didn't mean that you'd literally be able to use a simple expression to determine the ID of the relations when I wrote: You cannot simply do something like: Unless you really want to assign the first person within $people, whose ID happens to be 1000, a relationship with themselves, as the first iteration of 1000 + _k will be 1000.
Additionally, you're probably also going to need code, after pushing the current relationship object, to add a similar object to the other person—i.e. you create a relation within person A with person B, so you should also create the reciprocal relation within person B with person A.
For example: (I also changed most of the variables to try to make it a little clearer as to what's going on)
It isn't your responsibility to write my code for me. I do appreciate the pointers and help. As I said, I am rusty at coding (haven't done it for over a decade), so this is a re-learning experience for me. I've tried to follow the examples I've found and the documentation as best I can, but at times it seems like I am missing some things, or confusing my old programming experience with the new languages/formats.
Again, I REALLY do appreciate your help and patience.
That said: My first attempt was to make it a for loop with an if statement to not add an relationship.id that equaled the $people.id. When I got the errors, I trimmed it back to just shoving some test data into the setting in order to simplify my testing. My initial code attempt for data fill was:
I apparently clipped the $people array initializer line when I was updating the code with your suggestions. Thanks for the catch. That cleared up the majorty of my code errors.
Here is my final test code: An example out put is:
This was just to make sure that it was possible before I started down a path of insanity only to find out 10,000 lines of code later that it couldn't be done. (Been there, done that.)
I appreciate your suggestions for the reciprocation of relationship data, and will keep them in mind when I get to the point of coding the relationships. It's going to need some age and gender and other relationship checks to get it going (like for Bob to be your uncle, he needs to be related to your parents first).
FYI: This was just a test script. I intend for there to be many many more than just 6 people in the game, but I don't intend for each person to have an relationship array with data for every other person. It would be ridiculous to code for 100+ people and enter data for each persons relationship to all 100 other people if 90% of that data is going to be 'no relationship'. Not having an entry in the array would be 'no relationship'. Additionally, a lot of the randomness displayed in the test code will be done away with as it was just a way to fill data into the variables for testing.
Thank you again, TheMadExile. Your assistance has been very helpful and is greatly appreciated.