Howdy, Stranger!

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

[SUGARCUBE 2] cycle

Hello everybody,

this is my first post in the forum and I'm new to Twine, so.. hi I'm Alessio and I'm from Rome :) sorry for my bad english in advance :P

I started building my very first game yesterday, downloaded sugarcube 2 and started to code my story.
I have an overall advanced knowledge of HTML and Javascript, so I tried to build a game with an heavy background logic. I looked at the sucarcube 2 wiki but I couldn't find anything that could help me.

So my problem his, characters in my game have stats, let's say I have those 3

<<set $jake to {
character : {
name : "Jake",
stats : {
power : 0,
look: 0,
intelligence: 0
},

....

}>>

now my dear Jake is wearing a jacket that gives him a +5 at the look stats, but also holds other items that he currently doesn't have equipped so my code is something like


<<set $jake.inventory to {
items: [ ... ],
gold: 150,
wear: [
{
name : "leather jacket",
isOn: true,
mods: {look:5},
description: "...."
},
{
name: "winter coat",
isOn: false,
mods: {look:1, coldResist:10}
}

],

....

}>>


now in the stats menu tab I want to show all his stats added with all the wearable items he currently has on (in this case just a +5 to look given by leather jacket

so after grabbing the mods object from all the objects in wear array with isOn set to true I'd like to cycle all the stats in there

Assuming for simplicity that I can have only an item with isOn set to true I can grab the mods object and store it in a
_wearMods object

then I'd like to cycle over it with a (for in) like this:

<<for _key in _wearMods>>
<<set _stats[_key]+=_wearMods[_key]>>
<</for>>

in this way I could add more stats later on in the game without having to rewrite all my code
that for just seems not to work, no error or such, but it doesn't work so i sticked to an if for all the stats i currently have planned


Probably I haven't been clear, tell me if something doesn't sound you,

thanks in advance!

K.

Comments

  • Karatus wrote: »
    Assuming for simplicity that I can have only an item with isOn set to true I can grab the mods object and store it in a
    _wearMods object

    then I'd like to cycle over it with a (for in) like this:

    <<for _key in _wearMods>>
    <<set _stats[_key]+=_wearMods[_key]>>
    <</for>>

    in this way I could add more stats later on in the game without having to rewrite all my code
    that for just seems not to work, no error or such, but it doesn't work so i sticked to an if for all the stats i currently have planned
    The <<for>> macro does not currently support an "in" syntax.

    Currently, you'll need to retrieve the object property names manually and then iterate over them. Try something like the following instead:
    <<for _i to 0, _keys to Object.keys(_wearMods); _i lt _keys.length; ++_i>>
        <<set _stats[_keys[_i]] += _wearMods[_keys[_i]]>>
    <</for>>
    


    PS: Your English wasn't that bad, really. :) One tip though. What you're trying to do here is generally referred to as looping or iterating. E.g. In this case, "I'd like to loop over {a thing}" or "I'd like to iterate {a thing}". We don't really use the word "cycle" to describe this particular scenario—its not wrong, just not idiomatic.
  • Ahah! Thanks for the tip, I'll keep that in mind :)

    As for the solution I'm gonna try it as soon as I have some free time... probably next week, I'll let you know if there are some problems with it but it seems ok :smiley:
Sign In or Register to comment.