Begging for cash
senglyyy
08 Jul 2015, 22:56Alrighty, total noob here, but I've been playing around with the Quest interface and I can't seem to make this work:
I want to have the player be able to use a "beg" command in a certain area. When they do this, I want to randomize the amount of cash given to the player (somewhere between $0.50 and $5.00) and print a message like: "You got x amount of money from begging."
Should the cash be a variable or attribute? And if there's any way to show the current amount in the inventory, maybe by making it an object, that would be sweet.
Anyway I honestly have no idea how complicated this is. Can it be done?
I want to have the player be able to use a "beg" command in a certain area. When they do this, I want to randomize the amount of cash given to the player (somewhere between $0.50 and $5.00) and print a message like: "You got x amount of money from begging."
Should the cash be a variable or attribute? And if there's any way to show the current amount in the inventory, maybe by making it an object, that would be sweet.
Anyway I honestly have no idea how complicated this is. Can it be done?
The Pixie
09 Jul 2015, 07:33Cash needs to be an attribute. Variables exist only for a moment while a script or function runs, then disappear, so cannot be used to hold long-term values.
You could make it an item that starts in the player attribute and cannot be dropped, and have the amount as an attribute on that, then change the alias to reflect how much the player has. It would be easier to do it as a status attribute though (which is described in the tutorial).
Have you considered what will happen if the player begs for money a hundred times in one place? Will she get more and more money?
You could make it an item that starts in the player attribute and cannot be dropped, and have the amount as an attribute on that, then change the alias to reflect how much the player has. It would be easier to do it as a status attribute though (which is described in the tutorial).
Have you considered what will happen if the player begs for money a hundred times in one place? Will she get more and more money?
HegemonKhan
09 Jul 2015, 15:48not sure if this will work or if it's what you want (as I've never tried working with decimal numbers), but here you go:
http://docs.textadventures.co.uk/quest/elements/ (these are your main things in quest)
http://docs.textadventures.co.uk/quest/types/ (Attributes)
http://docs.textadventures.co.uk/quest/ ... omint.html
http://docs.textadventures.co.uk/quest/ ... ouble.html
http://docs.textadventures.co.uk/quest/ ... toint.html
http://docs.textadventures.co.uk/quest/ ... ouble.html
http://docs.textadventures.co.uk/quest/ ... tring.html
first, create~add your 'cash_double_attribute' Double Attribute on your 'player' Player Object:
'player' Player Object -> 'Attributes' Tab -> Attributes -> Add -> (see below)
(Object Name: player)
Attribute Name: cash_double_attribute
Attribute Type: double
Attribute Value: 0.00
then for your 'beg' Scripting Element ( 'whatever' Object's Verb, Command, Function, Turnscript, Timer, etc):
run as~a script -> add a~new script -> variables -> 'set a variable or attribute' Script -> (see below)
set variable random_cash_double_variable = [expression] ToDouble (ToString (GetRandomInt (0,4))) + GetRandomDouble ()
add a~new script -> variables -> 'set a variable or attribute' Script -> (see below)
set variable player.cash_double_attribute = [expression] player.cash_double_attribute + random_cash_double_variable
add a~new script -> output -> 'print a message' Script -> (see below)
print [expression] "You got " + random_cash_double_variable + " cash from begging."
to display your cash (via the 'Status' pane, it'll shows up when you create~add the Status Attributes for your Player Objects or the special 'game' Game Object):
'player' Player Object -> 'Attributes' Tab -> Status Attributes -> Add -> (see below)
Attribute Name: cash_double_attribute
Format String (Value): Cash: !
-----------
as for the 'area' where the 'beg' command works... are you going to make the 'beg' be a Verb of some Object, or is the 'beg' going to be an actual Command Element ??? Then, I'll need to know what you want done, as I need more info about it, as just saying in a certain area, isn't enough info.
once I know exactly what you want, then I can help with how to do it.
http://docs.textadventures.co.uk/quest/elements/ (these are your main things in quest)
http://docs.textadventures.co.uk/quest/types/ (Attributes)
http://docs.textadventures.co.uk/quest/ ... omint.html
http://docs.textadventures.co.uk/quest/ ... ouble.html
http://docs.textadventures.co.uk/quest/ ... toint.html
http://docs.textadventures.co.uk/quest/ ... ouble.html
http://docs.textadventures.co.uk/quest/ ... tring.html
first, create~add your 'cash_double_attribute' Double Attribute on your 'player' Player Object:
'player' Player Object -> 'Attributes' Tab -> Attributes -> Add -> (see below)
(Object Name: player)
Attribute Name: cash_double_attribute
Attribute Type: double
Attribute Value: 0.00
then for your 'beg' Scripting Element ( 'whatever' Object's Verb, Command, Function, Turnscript, Timer, etc):
run as~a script -> add a~new script -> variables -> 'set a variable or attribute' Script -> (see below)
set variable random_cash_double_variable = [expression] ToDouble (ToString (GetRandomInt (0,4))) + GetRandomDouble ()
add a~new script -> variables -> 'set a variable or attribute' Script -> (see below)
set variable player.cash_double_attribute = [expression] player.cash_double_attribute + random_cash_double_variable
add a~new script -> output -> 'print a message' Script -> (see below)
print [expression] "You got " + random_cash_double_variable + " cash from begging."
to display your cash (via the 'Status' pane, it'll shows up when you create~add the Status Attributes for your Player Objects or the special 'game' Game Object):
'player' Player Object -> 'Attributes' Tab -> Status Attributes -> Add -> (see below)
Attribute Name: cash_double_attribute
Format String (Value): Cash: !
-----------
as for the 'area' where the 'beg' command works... are you going to make the 'beg' be a Verb of some Object, or is the 'beg' going to be an actual Command Element ??? Then, I'll need to know what you want done, as I need more info about it, as just saying in a certain area, isn't enough info.
once I know exactly what you want, then I can help with how to do it.
senglyyy
09 Jul 2015, 19:48Your script is working for me, Hegemon, thank you! A couple things though: I followed your instructions and even after begging, the cash number in the inventory pane remains at zero. Also, is there any way to limit the number of decimals to the hundredth place?
Right now, "beg" works as a command that shows up in the area.
Right now, "beg" works as a command that shows up in the area.

jaynabonne
09 Jul 2015, 20:02Also, is there any way to limit the number of decimals to the hundredth place?
The only way I can see to do that is to use a string for the status attribute and format it yourself with two (fixed) decimal places. That means you'll need two attributes - one to accumulate the actual numeric value and one to be the formatted string that is shown in the attribute pane - but it should be straightforward enough to manage them both. (Update the numeric value with your addition, and then format that value into your string.)
I would get it working first, though, with the value changing as desired.

The other alternative is to not bother with cents (keep it whole dollars, which would just be an integer), but that might not be what you want.

jaynabonne
09 Jul 2015, 20:26Just for grins, here is a sample program that has a "beg" command and prints out how much you received and how much you now have. It has a "FormatMoney" function that makes it look dollar-y. (It's used for both the amount you received and how much you have.) Note that internally, the money value is kept as an integer representing cents. Floating point numbers are no good for currency. 
I didn't bother with the status attribute. That could be added.
Selecting a random value between $0.05 and $5.00 ended up with odd amounts like $3.44, so I took a punt and made it a multiple of 5 cents. You can easily change that if you wish.
The code is here and attached.
Note: the "if" in that function is handling the case where there aren't enough digits to form at least "X.XX". If so, it prepends 0's to make it at least 3 digits long. Then it just splits the string to insert a decimal point.

I didn't bother with the status attribute. That could be added.
Selecting a random value between $0.05 and $5.00 ended up with odd amounts like $3.44, so I took a punt and made it a multiple of 5 cents. You can easily change that if you wish.
The code is here and attached.
<!--Saved by Quest 5.6.5621.18142-->
<asl version="550">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="FormatCurrency">
<gameid>273c3146-02eb-4c1c-ae6b-971def5458c8</gameid>
<version>1.0</version>
<firstpublished>2015</firstpublished>
</game>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
<money type="int">0</money>
</object>
</object>
<command>
<pattern>beg</pattern>
<script>
amount = GetRandomInt(1, 100) * 5
player.money = player.money + amount
msg("You received " + FormatMoney(amount) + ". You now have " + FormatMoney(player.money))
</script>
</command>
<function name="FormatMoney" parameters="cents" type="string">
<![CDATA[
s = ToString(cents)
if (LengthOf(s) < 3) {
s = Right("000" + s, 3)
}
return("$" + Left(s, LengthOf(s)-2) + "." + Right(s, 2))
]]>
</function>
</asl>
Note: the "if" in that function is handling the case where there aren't enough digits to form at least "X.XX". If so, it prepends 0's to make it at least 3 digits long. Then it just splits the string to insert a decimal point.