How should I use Strings?

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
kylekrack
Liquid Metal Slime
Posts: 1188
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

How should I use Strings?

Post by kylekrack »

I didn't realize before trying to use 100 as a string ID that there is a 0-99 limit on strings, so I'm thinking I might have to rewrite some of my scripts for the game I'm working on. I'm using strings to create quest menus. Various locations offer different quests, and the menus only add menu items for quests that are available to the player. Anything they haven't fulfilled requirements for will not be displayed. Right now I just have a script that defines a series of string IDs, 50-59, with appropriate quest names. However, this list will grow, as I implement more quests.

Would it be wiser to instead use one long string that holds all of the quest names separated by periods or commas or hyphens? I'm not sure how many strings I will be using all at once for this game, but with the way it's going, I anticipate needing a lot more, further down the line. Or is there an even better way to allocate strings? Each quest menu is only opened one at a time, so in theory, I could have one string that is recycled every time a menu is opened.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

I can't tell why you are using strings (or 'plotstrings' as I generally call them, to avoid ambiguity) at all.
Is your menu displayed using "show string at", or are you using a normal menu? It sounds like the latter, and it's not just a menu with all the names of the quests (which could be defined in Custom). So you are embedding plotstrings in menu items using ${S#}? I take it you have a generic quest menu where only the quest title changes, embedded with ${S#}. Then you can just do something like:

Code: Select all

switch(quest number) do(
  case(0)  $0="Clear the basement"
  case(1)  $0="Vanquish the rodents"
  case(2)  $0="Repel the mosquitoes"
)
If you also have a menu containing the list of all quests and you don't want to duplicate the quest definitions in two places, then you could build that menu with a script.
Last edited by TMC on Mon Sep 19, 2016 3:12 am, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1188
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Basically, I'm using plotstrings and globals to avoid using tags. In a menu, the only method I'm aware of to disable menu items is by using tags. So instead of having a list of tags to check when the menu is loaded, I have a near empty menu that I add menu items to using plotscripting. The menu has one item in the editor: "back". Each time the menu is opened, a script runs that checks 1. whether the quest has been started, 2. whether the quest has requirements, and 3. whether the player meets these requirements. If the quest hasn't been started and the player meets the requirements, if any, the script adds a menu item to the menu. The script then sets the caption for the menu item by referencing a list of strings and sets the plotscript for the menu item to run when selected.

I hope this makes more sense. I'm not saying that's the best way to do things, that's just what I have right now. Based on what you said, I'm having second thoughts and trying to come up with a better way. Should I just use tags? For some reason I don't like using them for plotscripting.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

No problem.

In your case it seems like you have a whole lot of if blocks, for each quest, containing something like:

Code: Select all

setmenuitemcaption(menu item, 50)   #string 50 is set elsewhere, the name of quest 0
Why not just change it to

Code: Select all

$0 = "Quest 0: catch a butterfly"
setmenuitemcaption(menu item, 0)
Alternatively, use a switch statement like I showed. Put it in a script "get quest name".

Code: Select all

script, get quest name, quest number, begin
  switch(quest number) do(
    case(0)  $0="Clear the basement"
    case(1)  $0="Vanquish the rodents"
    case(2)  $0="Repel the mosquitoes"
  )
  return(0)   # returns the string ID
end

...

setmenuitemcaption(menu item, get quest name(0))  # get the string for quest #0
Last edited by TMC on Mon Sep 19, 2016 9:10 am, edited 1 time in total.
User avatar
Bob the Hamster
Liquid Metal King Slime
Posts: 7460
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

I often only use 2 strings. I use string ID 0 for copying strings into string slices and menu items, and I use string ID 1 for things like "string format" or string concatenation.

As TMC has demonstrated, you only need one string ID to fill your entire menu. There is no need to have a unique string ID for every menu item.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1188
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Fantastic. Thanks TMC. I'll use a switch statement like your example there. I guess I was over/underthinking it. I will be able to move forward with this.
My pronouns are they/them
Ps. I love my wife
Post Reply