Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Implementing and editing a complex object (Sugarcube)

Alright, so let me begin with an "I'm fairly new to Twine and most of my shenanigans so far have been tinkering. This is no exception."

So, let's say I have an object $car. A car is not made up of just one object, but of many.
So a $car would have four $wheels, one $engine, one $trunk and maybe five $seats, among other things.
Each of those $wheels, $seats, etc, would be its own object.
So we'd have something like
<<set $wheel to {
tyre : "michelin",
tyreDurability : 10
}>>
<<set $engine to {
brand : "mitsubishi",
speed : 10
}>>
/%Rest of fluff here%/
<<set $car to {
wheels : [$wheel, $wheel, $wheel, $wheel],
engine : $engine,
trunk : $trunk,
seats : [$seat, $seat, $seat, $seat, $seat]
}>>

So, my first question is: Is this the best way to implement a complex object in Twine? Is there a better way, and if so, what is that way?

Secondly, what would be the most efficient way to change parts of the car? Using
takes up a huge amount of space in the passage and is very difficult to keep track of. It's easy to forget, say, a $car.wheels[2].tyreDurability to 17 when typing out about twenty other things. Additionally, if I wanted to have a cascading change, it's impossible to do that without typing it all out myself. For example, if I changed the car's overall brand from a Ford F-150 to a Reliant Robin, then in that very same section, I would have to change the engine, the wheels, etc. A better result would be that when I want to change the car's overall brand, I call a function that reads a global variable somewhere and changes anything that needs changing. Want to change the car's electronics? Function changes the GPS, Radio and heating system. Stuff like that.
I know what needs to be done, but I'm not sure how to best approach the code. Perhaps this is a result of my object being flawed in the first place, and I'm just too dumb to see it, haha.

If anyone has any ideas or any obvious stupidity on my part, it'd be great if you could help out.

Comments

  • First of all, what story format are you using? It helps knowing that. Generally, extensive scripting with javascript is best done with twine version 1.x. If this is the only feature you plan on implementing then using twine 2 should be perfectly fine.

    As far as I'm concerned, ain't nothing better than a function to call on to do stuff, it should clean up your passages with considerably less clutter. If you're planning on using scripts and links to other passages simultaneously then I suggest making use of macros like click or button.

    If you have further questions don't hesitate to put them forward.
  • You can cheat with JSON.

    Initialize:

    <<set $showroom to { Honda: { engine: "honda", wheels: [ square, Square, Square, square], ...},
    { Ford: { engine: "ford", wheels: [ oval, oval, square, square] }}>>

    ...then you can...

    <<set $car to clone($showroom.Honda)>>

    ...and it'll copy over all the variables you've got set up in your showroom. You'll need to keep any upgrades or hindrances that are supposed to be persistent in another structure.
  • warning:
    One thing to be aware of, because of the way History works as soon as the Reader navigates to another passage the object referenced by your $wheel variable and the object referenced by your $car.wheels[0] variable will no longer be the same object, they will instead reference two new objects both of which will have the "same" value as the original.

    This means that in the passage you defined the two variables in changing the value of $wheel.tyreDurability to 9 would also change the value of $car.wheels[0].tyreDurability to 9, but in any future passage changing the value of $wheel.tyreDurability to 8 will not change the value of $car.wheels[0].tyreDurability at all.
Sign In or Register to comment.