Howdy, Stranger!

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

Can I use negation - !

edited April 2015 in Help! with 1.x
In some scripting languages, you can do things like

if !(Variable = 0), then

Anything like this in Sugarcane? I'm looking for something that would let me write a condition if this string is not zero characters. I can't ask whether the string is greater than 0, because, if I'm not mistaken, you can't compare a string to a number or another string. You can't compare strings to anything. Right?

Why does it matter? Well, in my story I have Large weapons (swords, epees etc.) and Small weapons (daggers, knives, particular kinds of knives etc.). So a sword, for example, has to answer to two kinds of checks: as a Large weapon (for frisking) and as the actual weapon that it is, for damage and so on. These things overlap, but not completely. Every sword is a Large weapon, but not every Large weapon does sword damage. And contrariwise.

So if I want to give the player a sword, I must set two variables every time, like so:

<<set $hasLargeweapon to 1>>
<<set $Largeweaponis to "sword">>

(Or do a long list of conditionals for every Large weapon in the status panel that's <<display>>ed down every passage. The conditionals would set $hasLargeweapon to 1 whenever a weapon from the list was picked up or put down. That can work, but it requires updating the list whenever I want to introduce a new weapon. Hmm.)

Now, other than the workaround I just gave, it's always two variables that have to be set. I can then run checks on what $Largeweaponis gives me: <<if $Largeweaponis eq "sword">>... do 5 damage. And the first check takes care of the weapon category. But it would be so much better to be able to ask

is the string $Largeweaponis longer than zero characters?

That way I wouldn't need the $hasLargeweapon at all, and I could invent new Large weapons on the fly (a timber log? Think about how easy it would be to create, say, a list of general offenses - not just items but actions, all checking for the non-emptiness of a couple of strings). Any length of the $Largeweaponis string would be enough to show that the player is carrying a Large weapon, in addition to the primary job of specifying what the weapon is. It would cut the work in half, no pun intended. And when the player throws down his sword (or any Large weapon), I would just <<set $Largeweaponis to "">> for those frisking checks.

Of course, I can check for "" already, if I want an empty string. But a) "" is not 0, which I could work with; b) I want to detect a non-empty string - probably via negation, to the effect of << if $Largeweaponis is not "">>. But it has to include the condition that the data, in addition to not being an empty string, is not any number either, in case I do want to stick a number there for some reason. And because value 0 means unarmed.

Just thinking out loud here: maybe I could ride an error? If I try to do a number-only operation on a string, that's going to throw me a 0, right? And I can put an "equals" condition to that.

<<set $lifeordeath to $Largeweaponis / 5>> this should give a 0 - no?
<<if $lifeordeath eq 0>>... then the player is carrying a Large weapon. The trick is to set $Largeweaponis to a number other than 0 when the player put away the weapon. So I now only need to

<<set $Largeweaponis to 5>> when he puts a Large weapon away
Then just set the string to the weapon name, when picked up, no matter what kind
<<if $lifeordeath eq 0>> would be the universal check for Large weapons

What do you think? Will this work, if there is no ! operation?

---

Update: just realized the player would see "you are carrying a 5" when unarmed. But that can be solved through the panel, where health and so on are shown. Like so:

<<display "Status bar">>
And in the Status bar passage:
<<if $lifeordeath eq 5>><<print "You are unarmed">><<else>><<print "You are carrying a " + $Largeweaponis>><<endif>>

Comments

  • temnix wrote:

    I can't ask whether the string is greater than 0, because, if I'm not mistaken, you can't compare a string to a number or another string. You can't compare strings to anything. Right?


    I'm quite sure you can write <<if $largeweapon gt 0>>.
  • Well, I tested it with this piece of code:
    <<set $a to "mewo">>
    <<set $b to "gir">>
    <<if $a > $b>><<print "Yes length can be compared">><<else>><<print "0">><<endif>>

    <<if $a > "">><<print "Yes you can check to see that a string is longer than zero characters">><<else>><<print "0">><<endif>>
    Yes and yes. So, what does it mean for my case? I can set <<set $Largeweaponis to "sword">>, then <<if $Largeweaponis > 0>>... then armed. But that doesn't show that the weapon is Large, that information is in the variable's name. I don't see how I could make it any shorter, though. It will have to be $Largeweaponis and $Smallweaponis. But that's two checks again. Dammit. I guess there is no way to reduce it to a single check, so long as only some weapons are forbidden. I guess I'm going to have to do a string of conditionals that fix the category for every particular weapon.

    Or maybe if I could mark up the strings somehow, you know. Like put a detectable but invisible character in the strings? You know, <<set $Weaponis to "daggerS">> or <<set $Weaponis to "swordL">>. The player wouldn't see L and X, but the if condition would, looking at the strings. Anything like that existing?
  • Claretta wrote:

    I'm quite sure you can write <<if $largeweapon gt 0>>.


    That works if $largeweapon is set to a number, but a string? I tried this:
    <<set $largeweapon to "sword">>
    <<if $largeweapon gt 0>>Yes it can<<else>>0<<endif>>
    Gives a 0. It means you can't compare a number and a string (this string is longer than zero characters and, well, just bigger than 0 in every way. :D) But you can compare string length, apparently, with the < and >, or gt and lt, which I assume work the same way. The documentation article on expressions says strings can only be added.
  • One way to associate multiple properties with a single variable is to use objects.

    A very simple example:

    <<set $weapon = {
    name: "sword",
    size: "large"
    }>>\

    You are holding a <<print $weapon.name>>, it is a <<print $weapon.size>> weapon.

    <<if $weapon.size neq "small">>Your weapon is not small<<endif>>
  • And where do I put the first part? What I have in the story is a status bar on the bottom, just a <<display>> thing checking different variables every passage. When something in the story is clicked, the passage obviously changes, the status bar reloads updated. So, should I make a separate Weapons passage with code like you gave, for each weapon? Then the "You are holding" part would be in the status bar. Would that work?
  • There is no one answer to your question, it depends on how complex / feature rich you want your RPG to be.

    note: The following examples have not been tested, so the syntax may not be correct.

    You could use a variable to represent each of your items:

    <<set $sword to {name: "sword", size: "large"}>>
    <<set $dagger to {name: "dagger", size: "small"}>>

    <<set $weapon to $dagger>>

    You are holding a <<print $weapon.size>> <<print $weapon.name>>
    You could use an array of objects, where each element represents a items:

    <<set $items to []>>

    <<set $items.push({name: "sword", size: "large"})>>
    <<set $items.push({name: "dagger", size: "small"})>>

    <<set $weapon to 1>>

    You are holding a <<print $item[ $weapon].size>> <<print $item[ $weapon].name>>
    You may want to look at my answer to your list based question
Sign In or Register to comment.