Author Topic: User created names  (Read 1941 times)

0 Members and 1 Guest are viewing this topic.

Offline Doan Sephim

User created names
« 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.


Artificial Intelligence Competition

Offline OmerMor

Re: User created names
« Reply #1 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

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

Offline Doan Sephim

Re: User created names
« Reply #2 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...
« Last Edit: November 25, 2011, 01:02:03 PM by Doan Sephim »
Artificial Intelligence Competition

Offline OmerMor

Re: User created names
« Reply #3 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.

Offline gumby

Re: User created names
« Reply #4 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. 
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition


SMF 2.0.14 | SMF © 2017, Simple Machines
Simple Audio Video Embedder

Page created in 0.097 seconds with 23 queries.