Community

SCI Programming => SCI Syntax Help => Topic started by: Doan Sephim on November 25, 2011, 10:08:06 AM

Title: User created names
Post by: Doan Sephim on November 25, 2011, 10:08:06 AM
Suppose my character is to name a dog. This is easy enough. You set up a var and:
Code: [Select]
EditPrint (@doggyName 14 "Aren't you a good doggy? Your name shall be:" #at 70 40 )
                Format(@buffer "You shall be known as %s." @doggyName)
                Print(@buffer #at -1 40 #mode alCENTER #width 250)

But perhaps my user just hits enter without naming the poor doggy. I have been unsuccessful in giving the dog a default name. I know it's got to be a simple solution, but I've looked through all the tutorials and I just can't find it.
Title: Re: User created names
Post by: OmerMor on November 25, 2011, 12:00:49 PM
I believe an Inline If Statement should help you out:
http://www.sierrahelp.com/SCI/Wiki/index.php?title=Inline_If_Statements (http://www.sierrahelp.com/SCI/Wiki/index.php?title=Inline_If_Statements)

Then you could probably use something like this:
Code: [Select]
EditPrint(@doggyName 14 "Aren't you a good doggy? Your name shall be:" #at 70 40)
Format(@buffer "You shall be known as %s." iif(= @doggyName "" @doggyName "Rexy"))
Print(@buffer #at -1 40 #mode alCENTER #width 250)

Keep in mind that I never written SCI code in my life, so it'll probably not compile, and might be complete nonsense.

Some background about the iif statemenet can be found here: http://sciprogramming.com/community/index.php/topic,377.0.html (http://sciprogramming.com/community/index.php/topic,377.0.html)
Title: Re: User created names
Post by: Doan Sephim on November 25, 2011, 12:58:42 PM
Ah! I got it to work. The inline if may work easier than what I did (or perhaps in the same way. Honestly I don't quite get them). But here is what works for me:
Code: [Select]
EditPrint (@doggyName 14 "Aren't you a good doggy? Your name shall be:" #at 70 40 )
(if(== doggyName 0)
   Format(@doggyName "Fido")
)
Format(@buffer "You shall be known as %s." @doggyName)
Print(@buffer #at -1 40 #mode alCENTER #width 250)

I needed to test the variable doggyName and when I tested it as a word == @doggyName "", it didn't work. But if the player puts nothing, then the value is zero. The second part, the part that was easier, is to set the variable as a word. = doggyName "Fido" will not work. But Format(@doggyName "Fido") gets the job done.

Edit: if they player enters in a space however, his name will be " " . Since the variable in this case will have a value, my code doesn't correct this slight problem, but I really doubt it will have much bearing...
Title: Re: User created names
Post by: OmerMor on November 25, 2011, 01:39:14 PM
Trimming for spaces and other non printing chars (tabs?) is a good idea. It'll convert "   Rexy   " to "Rexy" which is better.
Then you could add a check for the empty string ("") as well, and use a default one instead.
I don't know if a trimming method exists in SCI's current class library, but I guess it wouldn't be too hard to write one manually.
Title: Re: User created names
Post by: gumby on November 25, 2011, 04:50:23 PM
I needed to test the variable doggyName and when I tested it as a word == @doggyName "", it didn't work.
Yeah, I don't think you can do string comparisons that way - but that will work for more primitive data types (integers, decimals, etc).  For comparing strings, you'll want to look into the StrCmp() kernel function.