Howdy, Stranger!

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

Getting a number from adding strings of variables?

Basically I'm setting monsters race, $npc.race, as either "$race1", "$race2" etc and having the actual $race1 etc being objects that house that race of monsters stats. I need to get to a number that is say, kept in $race1.variable.

So I need to be able to add $npc.race (returns $race1) and .variable together to get at the number kept in $race1.variable.

The only way I am able to figure out how to do this, it gives me strings. I have also tried Number() and parseInt(), but those return blank.

What trick am I missing?

Comments

  • I am going to assume your object looks something like the following, the actual properties may be different:
    <<set $race1 to {race: "Orc", "Armor Class": 13}>>
    
    There are two ways you can to access the properties of an Javascript object:
    objectName.propertyName
    
    or
    
    objectName["propertyName"]
    

    So based on this fact you could do something like the following:
    <<set $race1 to {race: "Orc", "Armor Class": 13}>>
    
    <<set $variable to "Armor Class">>
    
    The monster is an <<print $race1.race>> and they have an Armor Class of <<print $race1[$variable]>>
    
  • Thanks for the reply :)

    This is close, but what I am attempting to do is basically go one level deeper. Instead of print $race1.race, I am trying to have print $npc.race, which will have the value of $race1, or $race2, etc etc and using that to add to the property of .strength or any other property.

    For instance,
    I'll have
    <<set $npc to (name:"John",race:$race1)>>
    <<set $race1 to (name:"Human",strength:4)>>
    <<print $npc.race + ".strength">>

    That last line of code works, but the output is a string, not a number... I can tinker with it, but no matter how I try to fiddle with it, the output is always a string, or blank.

    I tried setting a temporary $variable to .strength or "strength" but it returns a blank also.

    Is there any way to add the output of $npc.race together with .strength to get the number in $race1.strength?

    Thanks for your time!

  • You can use the technique from my previous comment no matter what depth:
    <<set $race1 to {name: "Human", strength: 4}>>
    
    <<set $npc to {name: "John", race: $race1}>>
    
    <<print $npc.race["strength"]>>
    
    note: you need to declare $race1 before you assign it as a property of $npc.

    warning:
    Each time the Reader navigates from one passage to another the values of all the current story $variables are cloned, and that set of cloned values is what is made available to the next passage.

    This means that your $npc.race property will no longer reference the original object stored in $race1 variable, it will instead reference a copy. The $race1 variable will also no longer be referencing the original object, it references a copy as well and that copy is a different one to that now being referenced by $npc.race but all of those new objects will contain the equivalent values.

    eg. In the next passage $npc.race !== $race but $npc.race.name and $race.name will both equal "Human".

    This is not an issue if you don't plan to change the values stored in the properties of variables like $race1
  • You're right, that piece of code there works perfectly. I made an error, having race:"$race1" instead of race:$race1 caused it to give a blank output instead of the number.

    You've solved the problem I've been trying to solve for two days, thanks a ton!
Sign In or Register to comment.