Check for one match in large string


#1

Hello!

I have a pretty primitive inventory-system (using variables and “&”:s) and I’m wondering if it’s possible to check for one particular word/present variable in a string (which consists of items with spaces to separate them). As it stands now, I’m basically using a bunch of if/elseif-statements; that becomes tedious very fast.

If there’s no function to check for a certain word, is it possible to reuse a set of “ifs” as a subroutine? There’s a function called “*gosub”, but I’m not sure if it’s what I’m after.

Thanks!


#2

I do not think that there is any way to check if a string contains a specific series of letters, unless that series of letters is the whole entire string. If a string is “Milk, bacon, cereal, nomnoms, health potions”, there is no way to *if contains(the_name_of_the_string, “bacon”) to see if bacon is included. I really wish there was. Also no way to see what variables were used to assign a new value to another variable. After you *set Grocery_list new_food_stuffs, there is no way to know that new_food_stuffs was used in changing Grocery_list… well, unless you go through the extra effort to make one. For now I’d suggest having separate variables for each inventory item (has_bacon = true or bacon_strips_owned = 128).

Also, if I understand what you’re doing you can totally use *gosub. I don’t really know what you are doing, though. Can you post the stuffs? You can use < code> to start the fancy keep-spaces-and-use-the-color-yellow-to-imply-game-code thing and < /code> to end it (no space) and get back to regular text.


#3

Hey, peglegpenguin, thanks for your reply.

I’m very interested in your “has_bacon = true”-example, though I’m not sure if it’s a good idea with more than half a dozen items; unless it’s possible to make variables temporarily hidden that is. I might have to take the long road, but thanks for your idea! :slight_smile:

P.S: Never mind the *gosub-part, I have a bunch of *goto:s in the if:s so it’ll probably jump like crazy.


#4

First, there is no way to check if a variable contains any particular string of characters. You’ll need an individual variable for each object. There is, however, already a good way of how to handle this (See the CoV language system: http://www.choiceofgames.com/vampire/scenes/choicescript_stats.txt for where I’m lifting this from).

To begin we’ll need some example items, lets just use ‘milk’, ‘eggs’ and ‘bacon’, each set either to true if you have the object, or false if you do not. Then what we need is one variable that will actually become our list (we’ll use the generic ‘inventory’), and one that will properly put commas between things in our list (we’ll use ‘comma’ for that).

Now for our actual code:*set inventory "" *set comma false *if milk *set inventory &"Milk" *set comma true *if eggs *if comma *set inventory &", " *set inventory &"Eggs" *set comma true *if bacon *if comma *set inventory &", " *set inventory &"Bacon" *set comma true Inventory: ${inventory}.

If you want to display this in game, rather than just on the stats page, just put a “*label inventory” above the first line and “*return” after the last, copying it into each scenes you intend to use it, and just use “*gosub inventory” to display it. Additionally, if you want to just add something to the list, all you have to do is copy any of the already included ‘items’ and change the *if command to the new item’s variable, and change the word the variable adds to your inventory.


#5

That is exactly what I do in my game, but bacon = spiked baseball bat.


#6

Intriguing.

Bearing in mind my own relative noobness, am I correct in thinking that you’re missing an extra indent on the two *set inventory &", " lines, as I can’t see this working properly without that extra indent?

Or am I barking up the wrong tree and need to go read up some more?


#7

For my latest game I have a large inventory. As such, I’m displaying the items in a grid with 3 items per row. To do this, each item is a separate variable set to true if you own it. If you don’t have the item, another variable of blank spaces fills that place. That’s literally a variable I created as " ". I made a few of these variables (as the items have a different number of letters) and now have a working, 18 item grid on my stats page and when coding I don’t need to worry about the order you get items or do anything extra. You get an item, it shows in the grid.


#8

@Vendetta Hm… No you’re right. I know, however that the indents were there, as I literally copied the bit out of the CoV stats page and just changed a few words.No Indent One Space Indent Two Space Indent One Space Indent Two Space Indent
It seems to work fine. Maybe I just done goofed and deleted the spaces on accident when I checked what I was typing. It wouldn’t be the dumbest error of mine. (Although I seem to recall someone posting something and saying the same thing, that their two space indents were cut down to one space indents.)


#9

@JimD

Any balanced diet requires a plate of mashed potato and fried, spiked baseball bats. :slight_smile:

@Reaperoa

Thanks, it’s working well! I also added spaces to follow the format I’m comfortable with - to avoid getting backtracked that is. :slight_smile: However, I’m still not sure how this helps when I later want to check whether the inventory contains a certain item or not. I have an inventory system, but no way to easily remove or check for certain items.

@andymwhy

Interesting approach!


#10

@reaperoa That’s a relief. It’s not been easy for me to learn this stuff so for a while there I thought I’d have to go back to the beginning and start all over again! :smiley:

BTW, looking at your second example above, it’s definitely not extra indenting the “Two Space Indent” lines for me, so I don’t think that first one was your mistake, it looks to me like a display issue on the forum (just some browsers, perhaps?–I’m using firefox atm).


#11

No, it’s not a browser thing, 'cause I’m seeing the same thing you are, and I know for sure that it was displaying them when I first posted it. That is really strange. Really don’t know what to make of it though.


#12

Hmm, surely then it can’t be anything other than the < code > < /code > not working properly for second & subsequent indents? It might just need a tweak . . .