Help with a script (copy from CP)

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Help with a script (copy from CP)

Post by Meatballsub »

I hope you all dont mind me copying this post from CP. This particular script is a big deal in terms of finishing my game and I hope for it to be resolved quicker this way. Thanks for the help:
What I am trying to accomplish here is making a script that makes you press a key to start it, then it randomly picks from a couple of sequences to follow.

One of the sequences merely shows a text box and starts the script over while the other sequence is supposed to set a five second timer and make you press a key as many times as possible. When the timer is up, it is supposed to check a variable and do something depending on the results.

However, all that happens when the timer starts is a hard lock in-game. Any suggestions?

Also, if you know of a way to add a feature within this script that allows the player to exit the script that would be nice. The whole script is supposed to repeat itself over and over again.

Be warned my scripting skills are terrible, which is why this probably doesn't work in the first place.

Code:
script,Blah,begin
suspend npcs
teleport to map (76,3,5)
set hero direction (0,south)
show textbox (989)
wait for textbox
wait for scancode (46)

variable (x)
x:=2

while (x<<4>0)) do (
if (key is pressed(19== true)) then (
feet+=1
) )
if (read timer (0==0) and (feet >= 5)) then (
show textbox (992))
else ( show textbox (993))

destroy npc (1)
if (r == 1) then (
show textbox (991)
wait for textbox
destroy npc (1)
) ))
end


Back to top


Camdog




Joined: 08 Aug 2003
Posts: 491

Posted: Tue Jun 03, 2008 5:48 am Post subject:

--------------------------------------------------------------------------------

The first thing that jumps out at me is that your parentheses are placed incorrectly when you're checking the timer values. Things in parentheses are always evaluated first. For example, a statement like:

Code:
read timer(myTimer + 1)


would check the value of the timer with the id myTimer + 1, rather than checking the value of the timer myTimer and then adding 1 to it. So, your statement:

Code:
while (read timer(0>0)) do (


checks to see if the value at timer(false) evaluates to true (since 0 is not greater than 0, it evaluates to false before the read timer function is called), rather than checking to see if the value at timer 0 is greater than 0. I believe false evaluates to 0 in hamsterspeak, which means you're checking timer 0, which is fine, but you're skipping the greater than condition you want. Since the timer is incrementing, and (I believe) positive integers are always equivalent to true, reade timer(0>0) is always true, and therefore you have an infinite loop, which would explain your lock. Try:

Code:
while (read timer(0)>0) do (


to see if that fixes it. Remember to also change:

Code:
if (read timer (0==0) and (feet >= 5)) then (


to:

Code:
if ((read timer (0)==0) and (feet >= 5)) then (


Also, it looks as if you're setting the trigger value of your timer to default. It seems timers were designed to fire off a custom script when they finish executing, and the documentation doesn't make it clear what happens if you just give that argument a default value. So, that may also cause you problems, in which case you might want to create a trigger script that just increments a global variable, and have your while statement check that instead of the number of ticks elapsed on the timer.

Back to top


Meatballsub
Divine Bovine




Joined: 16 Jun 2003
Posts: 227
Location: Adairsville, GA
Posted: Tue Jun 03, 2008 6:46 pm Post subject:

--------------------------------------------------------------------------------

I changed a few things around, but i'm still getting a hard lock if it goes to sequence one. Here is the updated code:

Code:
script,blah,begin
suspend npcs
suspend player
teleport to map (76,3,5)
set hero direction (0,south)
resume player
show textbox (989)
wait for textbox
wait for scancode (46)
set hero picture ((find hero(0)),99,outside battle)
create npc (1,3,6)
wait (50)
variable (r)
r := random(0, 1)
if (r == 0) then (
set npc direction (1,left)
wait for npc (1)
show textbox (990)
wait for textbox
variable (feet)
set timer (0,5,18,timer:default)
while (read timer (0)>>(0)) do (

if (key is pressed(19== true)) then (
feet+=1

if ((read timer (0)==(0)) and (feet >= 5)) then (
show textbox (992)
wait for textbox
destroy npc (1)
else ( show textbox (993)

) ))

if (r == 1) then (
show textbox (991)
wait for textbox
destroy npc (1)
) ))
end


Back to top


Meatballsub
Divine Bovine




Joined: 16 Jun 2003
Posts: 227
Location: Adairsville, GA
Posted: Wed Jun 04, 2008 5:57 am Post subject:

--------------------------------------------------------------------------------

Camdog, if I did not do something exactly how you suggested, know it is because I suck at scripting, not that I am ignoring your suggestions
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 »

Cleaning up your indentation may help locate the problem.

Code: Select all

script,blah,begin
  suspend npcs
  suspend player
  teleport to map &#40;76,3,5&#41;
  set hero direction &#40;0,south&#41;
  resume player
  show textbox &#40;989&#41;
  wait for textbox
  wait for scancode &#40;46&#41;
  set hero picture &#40;&#40;find hero&#40;0&#41;&#41;,99,outside battle&#41;
  create npc &#40;1,3,6&#41;
  wait &#40;50&#41;
  variable &#40;r&#41;
  r &#58;= random&#40;0, 1&#41;
  if &#40;r == 0&#41; then &#40;
    set npc direction &#40;1,left&#41;
    wait for npc &#40;1&#41;
    show textbox &#40;990&#41;
    wait for textbox
    variable &#40;feet&#41;
    set timer &#40;0,5,18,timer&#58;default&#41;
    while &#40;read timer &#40;0&#41;>>&#40;0&#41;&#41; do &#40;

      if &#40;key is pressed&#40;19== true&#41;&#41; then &#40;
        feet+=1

        if &#40;&#40;read timer &#40;0&#41;==&#40;0&#41;&#41; and &#40;feet >= 5&#41;&#41; then &#40;
          show textbox &#40;992&#41;
          wait for textbox
          destroy npc &#40;1&#41;
          else &#40; show textbox &#40;993&#41;&#41;
        &#41;
      &#41;

      if &#40;r == 1&#41; then &#40;
        show textbox &#40;991&#41;
        wait for textbox
        destroy npc &#40;1&#41;
      &#41;
    &#41;
  &#41;
end 
Some things I notice:

Code: Select all

  r &#58;= random&#40;0, 1&#41;
  if &#40;r == 0&#41; then &#40;
This part means that this script only does something half the time. Was that intended?

Code: Select all

      if &#40;key is pressed&#40;19== true&#41;&#41; then &#40;
This is definitely not what you mean. "19==true" is a comparison, It is always false, and the numeric code for false is "0", so what you have actually written is:

Code: Select all

      if &#40;key is pressed&#40;0&#41;&#41; then &#40;
But what I assume you actually meant was

Code: Select all

      if &#40;key is pressed&#40;19&#41;== true&#41; then &#40;
Which is actually redundant, since "if" automatically checks for "true", so all you really need is:

Code: Select all

      if &#40;key is pressed&#40;19&#41;&#41; then &#40;

And finally, there is this "else"

Code: Select all

          else &#40; show textbox &#40;993&#41;&#41;
which is definitely not what you want. I actually have no idea where you want this else to go, but I know it doesn't go there.

Here is the structure of an if/then/else

Code: Select all

if &#40;condition&#41; then &#40;
  # something
&#41; else &#40;
  # something else
&#41;
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

Thanks for the help. I finally resolved the issue of it locking up and it seemed to stem from the while command. The script is starting to shape up how I planned it except for a few things.

#1 - Is there a way that I can set the script to repeat infinitely unless a key is pressed?

#2 - The (key is pressed(19)) portion is not working as intended. I think it is only counting it one time. Is there a way to make it so it reads that the key is being press multiple times and if so, how? (Ham is a global variable set to count the amount of times 19 is press during the timer).

#3 - How do I set a global variable value to a string? I have already attempted to do it in the latest code which can be seen below.

Again, thanks for the help.

Here is the latest main code:

Code: Select all

script,blah,begin
	suspend npcs
	suspend player
	teleport to map &#40;76,3,5&#41;
	set hero direction &#40;0,south&#41;
	resume player
	show textbox &#40;989&#41;
	wait for textbox
	wait for scancode &#40;46&#41;
	set hero picture &#40;&#40;find hero&#40;0&#41;&#41;,99,outside battle&#41;
	create npc &#40;1,3,6&#41;
	wait &#40;50&#41;
	variable &#40;r&#41;
	r &#58;= random&#40;0, 1&#41; 
	if &#40;r == 0&#41; then 
	
	&#40; 
		set npc direction &#40;1,left&#41;
		wait for npc &#40;1&#41;
		show textbox &#40;990&#41; 
		wait for textbox 
		set timer &#40;0,5,timer&#58;default,@endfeet,1,timer flag&#58;battle&#41;
$2="&#40;Ham&#41;"
show string at &#40;&#40;Ham&#41;,30,190&#41;
show string at &#40;1,280,190&#41;

		
if &#40;key is pressed&#40;19&#41;&#41; then 
				&#40;
				Ham+=1
				&#41;&#41;

if &#40;r == 1&#41; then 
&#40; 
show textbox &#40;991&#41; 
wait for textbox 
destroy npc &#40;1&#41;
set hero picture &#40;0,11,outside battle&#41;
wait for hero &#40;0&#41;
&#41;
end
...and the end-of-timer script:

Code: Select all

script,endfeet,begin
				if &#40;&#40;read timer &#40;0&#41;==&#40;0&#41;&#41; and &#40;Ham >= 5&#41;&#41; then 
				&#40; 
				show textbox &#40;992&#41;
				wait for textbox
				destroy npc &#40;1&#41;
				&#41;
else &#40; show textbox &#40;993&#41;
wait for textbox &#41;
clear string &#40;1&#41;
clear string &#40;2&#41;
destroy npc &#40;1&#41;
set hero picture &#40;0,11,outside battle&#41;
wait for hero &#40;0&#41;
end
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 »

Meatballsub wrote:Thanks for the help. I finally resolved the issue of it locking up and it seemed to stem from the while command. The script is starting to shape up how I planned it except for a few things.

#1 - Is there a way that I can set the script to repeat infinitely unless a key is pressed?
You can do something like

Code: Select all

  while&#40;keyispressed&#40;key&#58;ESC&#41;==false&#41;&#40;
    #stuff that happens over and over
    wait&#40;1&#41;
  &#41;
Note that if you don't have some kind of "wait" command in the while loop, the script will seem to freeze up.

Also, remember that an endlessly looping script pauses all scripts that are already running, and if any new scripts start, the endless loop will be paused until they finish.
Meatballsub wrote:#2 - The (key is pressed(19)) portion is not working as intended. I think it is only counting it one time. Is there a way to make it so it reads that the key is being press multiple times and if so, how? (Ham is a global variable set to count the amount of times 19 is press during the timer).
I suggest that you move this to a completely separate script that you will set as the "on keypress" script.

Code: Select all

plotscript, keypress handler &#40;
  if &#40;key is pressed&#40;19&#41;&#41; then &#40;
    Ham+=1
  &#41;
&#41;
Meatballsub wrote:#3 - How do I set a global variable value to a string? I have already attempted to do it in the latest code which can be seen below.
You want to display a global variable in a string? Try this:

Code: Select all

clear string&#40;2&#41;
append number&#40;2, Ham&#41;
show string at &#40;2,30,190&#41; 
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

Thanks for that. Everything is working as intended except for the keyispressed thing still. It does not count up at all. I have tried altering it several times and just cannot figure it out. Maybe I am not using the command right? I will keep trying different things but if you have any suggestions, please let me know:

Code: Select all

script,blah,begin
	suspend npcs
	suspend player
	teleport to map &#40;76,3,5&#41;
	set hero direction &#40;0,south&#41;
	resume player
	show textbox &#40;989&#41;
set on keypress script &#40;78&#41;
while&#40;keyispressed&#40;01&#41;==false&#41; do &#40; 


	wait for textbox
	wait for scancode &#40;46&#41;
	set hero picture &#40;&#40;find hero&#40;0&#41;&#41;,99,outside battle&#41;
	create npc &#40;1,3,6&#41;
	wait &#40;50&#41;
	variable &#40;r&#41;
	r &#58;= random&#40;0, 1&#41; 
	if &#40;r == 0&#41; then 
	
	&#40; 
		set npc direction &#40;1,left&#41;
		wait for npc &#40;1&#41;
		show textbox &#40;990&#41; 
		wait for textbox 
		set timer &#40;0,5,timer&#58;default,@endfish,1,timer flag&#58;battle&#41;
clear string&#40;2&#41; 
append number&#40;2, Reel&#41; 
show string at &#40;2,30,190&#41; 
show string at &#40;1,280,190&#41;
get on keypress script
&#41;

if &#40;r == 1&#41; then 
&#40; 
show textbox &#40;991&#41; 
wait for textbox 
destroy npc &#40;1&#41;
set hero picture &#40;0,11,outside battle&#41;
wait for hero &#40;0&#41;
&#41;
wait &#40;1&#41;
Ham&#58;=0
&#41;
end
the new, seperate on keypress script:

Code: Select all

script,wodod,begin
if &#40;key is pressed&#40;19&#41;&#41; then
				&#40;
				Ham+=1
				&#41;
end
User avatar
Mogri
Super Slime
Posts: 4598
Joined: Mon Oct 15, 2007 6:38 pm
Location: Austin, TX
Contact:

Post by Mogri »

Can you show us the script in context?
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 »

Code: Select all

set on keypress script &#40;78&#41;
I assume that 78 is the ID number of the script. This does technically work, but the recommended way is:

Code: Select all

set on keypress script &#40;@wodod&#41;
the @ sign automatically gets the ID number of a script. You are already using it in your "set timer" command.

...oops! No wonder you did it that way! I just realized that I never updated the docs for that command to reccomend the @scriptname method. I'll fix that right away.

Code: Select all

get on keypress script
I am not sure what you are trying to do here. This command does nothing unless you store the result in a variable. (and I can't think of any reason why you would want to do that anyway) Did you mean to disable the on keypress script you set earlier? If so, you want:

Code: Select all

set on keypress script &#40;&#41;
Next, I see this code:

Code: Select all

clear string&#40;2&#41;
append number&#40;2, Reel&#41;
show string at &#40;2,30,190&#41;
show string at &#40;1,280,190&#41; 
Which displays the "Reel" variable... but I don't know where this variable is set. I thought you were going to use the "Ham" variable here.
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

Well, I was trying to avoid giving out much information and keeping it as a surprise in-game, but I suppose if it will help find the solution I will do it:

In its entirety it is supposed to act as a fishing script:

Code: Select all

script,blah,begin 
   suspend npcs 
   suspend player 
   teleport to map &#40;76,3,5&#41; 
   set hero direction &#40;0,south&#41; 
   resume player 
   show textbox &#40;989&#41;
set on keypress script &#40;78&#41; 
while&#40;keyispressed&#40;01&#41;==false&#41; do &#40; 
wait for textbox 
wait for scancode &#40;46&#41; 
Obviously, this teleports the player from the map to the fishing area and shows a textbox indicating the particular rules and regulations for fishing in that area. After the textbox is clear, it waits for you to "cast", which is what the wait for scancode portion is about.

This part also sets up the on keypress script and starts the loop for the bulk of the script so that you can constantly fish until you press escape.


Code: Select all

set hero picture &#40;&#40;find hero&#40;0&#41;&#41;,99,outside battle&#41; 
   create npc &#40;1,3,6&#41; 
   wait &#40;50&#41; 
   variable &#40;r&#41; 
create npc &#40;1,3,6&#41; 
   wait &#40;50&#41; 
   variable &#40;r&#41; 
   r &#58;= random&#40;0, 1&#41; 
Sets the hero's "casting" graphic, creates the "bait" npc and waits for 50 ticks. Sets up the random variable that will choose whether or not you get a bite.

Code: Select all

if &#40;r == 0&#41; then 
    
   &#40; 
      set npc direction &#40;1,left&#41; 
      wait for npc &#40;1&#41; 
      show textbox &#40;990&#41; 
      wait for textbox 
      set timer &#40;0,5,timer&#58;default,@endfish,1,timer flag&#58;battle&#41; 
clear string&#40;2&#41; 
append number&#40;2, Reel&#41; 
show string at &#40;2,30,190&#41; 
show string at &#40;1,280,190&#41; 
get on keypress script 
&#41; 
OMG you got a bite! A textbox tells you that you must "reel" it in (aka the keypress script. Depending on the type of fish caught will determine how many times you need to press a certain button to catch it. If you don't press it enough times before the timer runs out, you lose the fish.

Code: Select all

if &#40;r == 1&#41; then 
&#40; 
show textbox &#40;991&#41; 
wait for textbox 
destroy npc &#40;1&#41; 
set hero picture &#40;0,11,outside battle&#41; 
wait for hero &#40;0&#41; 
&#41; 
wait &#40;1&#41; 
Ham&#58;=0 
&#41; 
end
....Or if you didn't get a bite, this happens.

I hope that helps. Let me know if you need something else.

As far as Ham and the other names go, I was just altering the original names to keep it under wraps some. But basically what I need to figure out is how to count each instance that the "r" key is pressed.
Next, I see this code:

Code:
clear string(2)
append number(2, Reel)
show string at (2,30,190)
show string at (1,280,190)


Which displays the "Reel" variable... but I don't know where this variable is set. I thought you were going to use the "Ham" variable here.
Reel is a global variable and it is correct. Ham was just a cover up.
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 »

In that case, it looks to me as if you were doing the right thing. Can you verify that the on keypress script is really getting run? Maybe something like:

script, key handler, begin
if (key is pressed(19)) then
(
Reel+=1
$5="Reel:"
append number(5, Reel)
show string at(5, 0, 0)
)
end

That will tell you if the problem is in your on keypress script or in your main script.
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

Okay I did that and it seemed to show the numbers up just fine.

I finally got it to work. I took away the if (read timer(0) and reel => etc) and set it just to if (reel) => etc) and it works great.

The only other question I have is if it is possible to to use the key press command and it not work if you hold down the button? I was hoping I could design the script around specific key presses, not just by holding the button down.

If it isn't possible, then is there a way to set the script so if the 19 key is NOT pressed during the time you can, that it decreases the value of Reel at the same speed as it can increase?

Cleaned up script for reference:

Code: Select all

script,Fishing1,begin
	suspend npcs
	suspend player
	teleport to map &#40;76,3,5&#41;
	set hero direction &#40;0,south&#41;
	resume player
	show textbox &#40;989&#41;
	while&#40;keyispressed&#40;01&#41;==false&#41; do &#40;
		set on keypress script &#40;&#41; 
		wait for textbox
		wait for scancode &#40;46&#41;
		set hero picture &#40;&#40;find hero&#40;0&#41;&#41;,99,outside battle&#41;
		create npc &#40;1,3,6&#41;
		wait &#40;300&#41;
		variable &#40;r&#41;
		r &#58;= random&#40;0, 1&#41; 
	if &#40;r == 0&#41; then &#40; 
		set npc direction &#40;1,left&#41;
		wait for npc &#40;1&#41;
		show textbox &#40;990&#41; 
		wait for textbox 
		set timer &#40;0,5,timer&#58;default,@endfish,1,timer flag&#58;battle&#41;
		set on keypress script &#40;@reelin&#41;
		&#41;
	if &#40;r == 1&#41; then &#40; 
		show textbox &#40;991&#41; 
		wait for textbox 
		destroy npc &#40;1&#41;
		set hero picture &#40;0,11,outside battle&#41;
		wait for hero &#40;0&#41;
		&#41;
		wait &#40;1&#41;
	&#41;
end



script,endfish,begin
	if &#40;&#40;Reel&#41; >= 25 and <=50 &#41; then &#40; 
		show textbox &#40;992&#41;
		wait for textbox
		&#41;
	else &#40; show textbox &#40;993&#41;
		wait for textbox
		&#41;
	destroy npc &#40;1&#41;
	clear string &#40;5&#41;
	set hero picture &#40;0,11,outside battle&#41;
	wait for hero &#40;0&#41;
end




script,reelin,begin
	if &#40;key is pressed&#40;19&#41;&#41; then &#40;
		Reel+=1
		&#41;
	else &#40;
		Reel-=1
		&#41;
	$5="Reel&#58;" 
	append number&#40;5, Reel&#41; 
	show string at&#40;5, 0, 0&#41; 
end
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 »

Meatballsub wrote:Okay I did that and it seemed to show the numbers up just fine.

I finally got it to work. I took away the if (read timer(0) and reel => etc) and set it just to if (reel) => etc) and it works great. The only other question I have is if it is possible to to use the key press command and it not work if you hold down the button? I was hoping I could design the script around specific key presses, not just by holding the button down.
instead of:

Code: Select all

if &#40;key is pressed&#40;19&#41;&#41; then
try this:

Code: Select all

if &#40;keyval&#40;19&#41; == 2&#41; then
For details on how "keyval" works, check out http://hamsterrepublic.com/ohrrpgce/doc ... out-keyval
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

Keyval doesn't seem to work right. I tried 2 and 3 scancodes and neither did anything. They won't even put up the string.

Any suggestions?
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

If it isn't possible, then is there a way to set the script so if the 19 key is NOT pressed during the time you can, that it decreases the value of Reel at the same speed as it can increase?
So is this a possibility? Basically, use the keypress script and it adds to the variable while it is pressed down but if you let up on it, it decreases the time at a slightly slower speed than when you gain it.
User avatar
Mogri
Super Slime
Posts: 4598
Joined: Mon Oct 15, 2007 6:38 pm
Location: Austin, TX
Contact:

Post by Mogri »

Yes, that's possible, although man if you read your question it is so confusing. It took me like three reads to figure out what you were asking.
User avatar
Meatballsub
Liquid Metal Slime
Posts: 996
Joined: Mon Oct 15, 2007 6:39 pm
Location: Northwest Georgia
Contact:

Post by Meatballsub »

I am the world's WORST at explaining things. If you asked anybody that knows me they would say that. :gonk:
Post Reply