Community

SCI Programming => SCI Syntax Help => Topic started by: Doan Sephim on February 17, 2021, 12:39:40 PM

Title: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 12:39:40 PM
I understand how to "FormatPrint" to allow for variable values to be imbedding in a print command, but I'm not sure how to do that with the Inventory's "description." I looked in the Inv script and there's something about IconPrint in the showSelf method, but I confess, I'm a bit baffled.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: gumby on February 17, 2021, 12:58:02 PM
Here's the code I think you are referring to in Inv.sc

Code: [Select]
   (IconPrint description view loop cel)

Essentially you need to pre-process the description string and do your variable replacement using the Format kernel call to make whatever substitution you want.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 02:14:08 PM
Here's the code I think you are referring to in Inv.sc

Code: [Select]
   (IconPrint description view loop cel)

Essentially you need to pre-process the description string and do your variable replacement using the Format kernel call to make whatever substitution you want.
Yup. That's it!

So, "pre-process the description string" ... Does that mean to set up the string (maybe in the main script) and then call that string as the "description?" I'm having trouble calling anything but a number or bracketed statement in the description. Surely I cannot call a text resource with a variable in it, correct?

Am I missing a step or comprehending something wrong?
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: lskovlun on February 17, 2021, 02:38:29 PM
Every inventory object can have its own showSelf method. So inside your object instance you just say:
Code: [Select]
(method (showSelf &tmp [buffer 40]) ; or whatever size you need
(if foo
    (Format @buffer "This object is a foo")
else
    (Format @buffer "This object is a bar"))
(IconPrint buffer view loop cel)
)
You can also change the view/loop/cel properties from within the if statement if needed.
Colonel's Bequest and KQ4 derive a new class from InvI in order to not have a description string at all, but it's the same basic principle.

EDIT: Aaand I forgot the title. Easy to add if it is static, but if it needs to be dynamic also, that's slightly more difficult because it needs to show in the list. But again, CB1 actually does that.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 02:58:50 PM
Thanks to both of you. I now understand what gumby meant by pre-process. I will tinker around with this, but I think I'm now understanding the basic structures.

Thanks. Will update when I get some more time to see what I can come up with.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 03:19:14 PM
I'm trying and failing to get a grip on how to write the "if foo" part to know which item I am in fact choosing. I tried:
Code: [Select]
(method (showSelf &tmp [buffer 40])
(if INV_TEST_OBJECT
(Format @buffer "Test to see if I can see this.")
(IconPrint buffer view loop cel)
else
(if description
(IconPrint description view loop cel)
else
(IconPrint objectName view loop cel)
)
)
)

But it just yielded back the description as outlined in the Main script.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: gumby on February 17, 2021, 03:24:08 PM
Here's an example that I hacked together.

First, create a text resource (text.000) that holds a single parameterized string in the zeroth position that looks like this:
Code: [Select]
  A parameterized string with a %s.

Then create a text resource (text.001) that holds a few (3) nouns.
Code: [Select]
  catapult
  rubber chicken
  bowling ball

Then in a room script, add this code:
Code: [Select]
(method (handleEvent pEvent &tmp [text1 100] [text2 130] [nounName 30])
   (super handleEvent: pEvent)
   (if (Said 'look')
      (GetFarText 1 (Random 0 2) @nounName)  ; get a noun out of text resource 1
      (GetFarText 0 0 @text1)                ; get the parameterized string out of text resource 0
      (IconPrint (Format @text2 @text1 @nounName) 0 0 0)
   )   
)

When the player types look, they'll get 'A parameterized string with a bowling ball'... or one of the 2 other possibilities.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 03:36:01 PM
Playing mad libs with SCI0! Nice  ;D

It's going to take me some time to go through and conceptualize that code (which is sad to me having been at this for so long to still not understand what are clearly basic concepts!)

The thing that's currently in my way is just figuring out how to properly check which item is being clicked and then customizing a description response for it. You're example is great for me to get used to using strings and printing them, but the trigger to do it all is the playing typing "look." I'm familiar with that, but I'm not sure about how to check which item is being clicked (as opposed to others.) Is that something I can even check in the template Inv script?
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: lskovlun on February 17, 2021, 04:44:48 PM
Code: [Select]
(method (showSelf &tmp [buffer 40])
(if INV_TEST_OBJECT
(Format @buffer "Test to see if I can see this.")
(IconPrint buffer view loop cel)
else
(if description
(IconPrint description view loop cel)
else
(IconPrint objectName view loop cel)
)
)
)
But it just yielded back the description as outlined in the Main script.
[/quote]
I am thinking the INV_TEST_OBJECT here is the issue. It's a constant, right - so why are you testing its value?
Either I'm misunderstanding completely, or you should just stick with one level of if.

Hmm, are you trying to put all this in the InvI base class? Because that was what I described how to avoid...
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: gumby on February 17, 2021, 05:22:28 PM
I think I see what you are driving at Doan.  When you bring up an inventory item, you want the description of the inventory item to be displayed with some sort of parameterization in it. 

For example, assuming you had 10 different boxes in your inventory.  When you pulled up any one of the boxes, have the description include the count of the number of things inside it:  "Test Box - contains 5 items" or something like that?
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: gumby on February 17, 2021, 05:58:36 PM
Ok, I think I figured out where things were going wrong.  You don't want to be changing code in Inv.sc.  The code you want is in Main.sc.

Start by changing the description on the Test_Object to what's below.

Code: [Select]
(instance Test_Object of Iitem
(properties
said 'object'
description {This is a test object in your inventory. View = %d}
owner 0
view 800
loop 0
cel 0
script 0
name "Test Object"
)
)

Next, change the Iitem class:

Code: [Select]
(class Iitem of InvI
(properties
said 0
description 0
owner 0
view 0
loop 0
cel 0
script 0
)

(method (showSelf &tmp [text1 100] [text2 130] [nounName 30])
           (Print (Format @text2 description view) #title objectName #icon view loop cel)    
)
)

Now when you look at the item in your inventory, it'll also print the view number as part of the description.  Is that the sort of thing you are looking for?
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 07:32:07 PM
lskovlun, I'm clearly out of my depth when it comes to editing the standard template. I think I now understand that you were talking about a new method per inventory item (at least I think I understand that now). I will try some more later. I'm still at a loss on what "foo" is or what I should be replacing it with. I'm supposing it's just a stand in for what "description" was in the original method?

Gumby. Essentially I'm trying to display an item, say crackers, but also let the player know how many crackers they have. My goal is to display in the description message a number of the particular item they have. Most items the player will just have (1 of), but I would like to have multiples for other items.

After looking thru your code, it seems like it's relatively easy to do. I think I see how I the %d in the description can work with the slightly changed In script method. I'm not at the computer now, but I will give it a shot when I get the time. Not having checked it myself, would that effect inventory items in not trying to associate with variables?

Thank you both for the help. Coding has never been my forte
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: gumby on February 17, 2021, 08:06:34 PM
I would suggest introducing a 'count' property to the inventory item class (Iitem).  That way you can keep track of the count within the inventory item itself and you won't have the headache of having to keep track of the count with other global variables.

In the code I posted, just swap out the reference that prints out the view # for your new count property.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 17, 2021, 09:24:53 PM
I would suggest introducing a 'count' property to the inventory item class (Iitem).  That way you can keep track of the count within the inventory item itself and you won't have the headache of having to keep track of the count with other global variables.

In the code I posted, just swap out the reference that prints out the view # for your new count property.
Excellent! Thanks for steering me in the right direction. I've made the alterations you've said and it does work just as it should.

My only problem now is figuring out how to manipulate the number. I've set up "count" as a new property of the iItem class and then added a new item:
Code: [Select]
(instance Crackers of Iitem
(properties
said 'cracker'
description {These are crackers. You have = %d many}
owner 0
view 610
loop 0
cel 0
script 0
name "crackers"
count 2
)
)

I can easily assign a number (like "2") to the "count" when acquiring the item, but how can I manipulate that number? I tried to assign it a variable, but the interpreter was pretty insistent that I only use a constant for the properties. Any thoughts?

Edit: I tried to set up a procedure in the Iitem class that would let me change the "count." Not sure if that's the right idea or not, but I can't get it working yet.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: ZvikaZ on February 18, 2021, 01:59:45 AM
Gumby. Essentially I'm trying to display an item, say crackers, but also let the player know how many crackers they have. My goal is to display in the description message a number of the particular item they have. Most items the player will just have (1 of), but I would like to have multiples for other items.
I haven't followed the discussion, but if I understand you correctly, there's a similar system at QFG1VGA (and maybe other QFGs?). You might want to take a look at its scripts.
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: gumby on February 18, 2021, 09:18:34 AM
Here's how I do it (there may be different/better ways).  Reference the entire inventory, then update based on the index of the item in the inventory using the constant defined for the inventory item in game.sh.

To set the value:
Code: [Select]
    ((gInv at: INV_TEST_OBJECT) count: 3)

To get the value:
Code: [Select]
    (FormatPrint "count = %d" ((gInv at: INV_TEST_OBJECT) count?))
Title: Re: SCI0 - Can you print an variable value in the Item's inventory description?
Post by: Doan Sephim on February 18, 2021, 09:39:29 AM
That sounds like a pretty simple way to do it to me! Thanks.