I'm making a story that's a little like Sims or Age of Empires, where you watch over a certain number of people.
An example of one of the pieces of code I've written for the game involves the object class or associative array.
<<set $human to {name: "Bob", gender: "male", height: "72"}>>
$human.name is a $human.gender who is $human.height inches tall.
Now, I'm wondering how you can call a variable to an equation based on its objects. For instance, if two humans get together and make a baby, it obviously has to be a male and a female. It's worth noting I want the system to specifically track the mother and father, so if one the humans dies, it knows whether or not that was the mom that just died or another human. The mom will be upset if the father dies, that sort of thing.
So I'd have to find a way to say, the variable $human that has the object $male, or if this variable has $human.gender = true. when calling it into the random equation of "<<this human>> and <<this human>> have shacked up"
Is there any way to do this? Any help is appreciated.
Comments
Seriously. Not being snarky. You've totes lost me here.
To be honest, I don't really know what I'm talking about either.
Basically, there's a function in Harlowe called datamap, where you can set attributes within a variable, so $weapon can have things like "name, small sword" "damage, 10" or "cost, 30" or "next weapon, big sword"
It's my understanding Sugarcube does not have datamaps, but instead has something similar to JavaScript with objects, and you can set attributes there. So you can have the variable $human to have an object after their "name: Bob", "gender: Male" and "height: 72". And that all prints out just fine in Sugarcube, you know, $human.name, $human.gender, etc.
But once you have these objects, or as I think they're called associative arrays, though that may be something completely different, I'm wondering if you can put them into an equation, like you can with a variable.
For instance, the gender could be randomly assigned among the variables. Like <<set $human1.gender = either("male", "female")>>
So for simplicity's sake, lets say it randomly generates out so that $human1-10 are set to be "gender:male" and $human11-20 are set to be "gender:female"
I want to call two random humans together, because in this story I want humans to be able to reproduce (the main way of getting more humans), but it needs to be one random human that has "gender: male" and one that has "gender:female".
So it'd like to have set the parameters of a random generation, instead of "random(1, 10)" with 1 and 10 being the parameters, it'd have to be like "random(all humans that are gender:male)" or something, and then the same for "gender:female", to produce one random human out of all of them that have the attribute "male" and one random human that has the attribute "female"
Is this possible at all, or am I over-reaching the capabilities of what Twine and Sugarcube can do?
a. StoryInit b. Start note:
a. I used a individual $variable for each of the human because your original example did.
b. The above was written on in long hand to make it easier to under each step, it would be relatively easy to condense.
c. All the above steps could be replace by custom Javascript functions, one that returns a random Male and another a random Female.
Thank you for the informative and deliberate "long hand" version to clearly indicate to a relative novice like myself what each function does. This helps me dissect the code and really get a personal understanding of the syntax. However, as someone who's very passionate about this kind of programming, I'm very interesting in your notes.
a. I used an individual variable for each human because I'm utterly unaware how to depict the problem otherwise. I'd love to know if it's possible to not have each human with their own variable and still have this work. The scope of how many humans I want the player to control is quite large and making 200~ variables for each possible human is unnecessary and rather bad form if it can be done by the computer.
b. I would love to see a condensed version.
Thank you for all the help.
Maps are actually a somewhat recent JavaScript feature. It depends on the version of SugarCube as to whether they're guaranteed to exist or not. SugarCube 2.x provides a polyfill which guarantees that Maps (and Sets) exist, regardless of browser. SugarCube 1.x, not so much. TL;DR: SugarCube 2.x has Maps, SugarCube 1.x only has them if the player's browser supports them (so don't depend on it).
And, yes, you can use generic objects for much the same purpose as a Map. The primary difference between a Map and a generic object (besides the API) is that you can use anything as the key in a Map, while an object's keys (property names, really) must be strings or numbers. There are others, but that's probably the key difference for most people.
Generic objects are not associative arrays. You may use them like that, certainly, but there are differences.
Now on to specific replies.
Yes. You can assign to an object's property just as you've done there. Though, I wouldn't do it like that exactly, otherwise you might end up with a woman named Jeff (yikes).
What you want to do is completely possible (not even hard, really).
Are you planning on these imaginary people each having their own $variable, as you've shown (i.e. $human1, $human2, etc)? I ask because it seems like you might be better off keeping them in two arrays based on gender (you could keep them in one, but since it seems like gender is going to be important, it'll be easier to segregate them). For example (probably best to do this bit in the StoryInit special passage): With that it's relatively easy to pick out a male and female villager. For example, to very simply select one man and woman at random:
This also works quite well. Thank you.
Could you explain to me what the FOR command does? And that $i variable for $i to 0; $i < 10; $i++ stuff. I don't understand how any of those work.