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
Help with a script (copy from CP)
Moderators: Bob the Hamster, marionline, SDHawk
- 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)
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:
- Bob the Hamster
- Liquid Metal King Slime
- Posts: 7460
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
Cleaning up your indentation may help locate the problem.
Some things I notice:
This part means that this script only does something half the time. Was that intended?
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:
But what I assume you actually meant was
Which is actually redundant, since "if" automatically checks for "true", so all you really need is:
And finally, there is this "else"
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
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
Code: Select all
r := random(0, 1)
if (r == 0) then (
Code: Select all
if (key is pressed(19== true)) then (
Code: Select all
if (key is pressed(0)) then (
Code: Select all
if (key is pressed(19)== true) then (
Code: Select all
if (key is pressed(19)) then (
And finally, there is this "else"
Code: Select all
else ( show textbox (993))
Here is the structure of an if/then/else
Code: Select all
if (condition) then (
# something
) else (
# something else
)
- Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact:
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:
...and the end-of-timer script:
#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 (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
set timer (0,5,timer:default,@endfeet,1,timer flag:battle)
$2="(Ham)"
show string at ((Ham),30,190)
show string at (1,280,190)
if (key is pressed(19)) then
(
Ham+=1
))
if (r == 1) then
(
show textbox (991)
wait for textbox
destroy npc (1)
set hero picture (0,11,outside battle)
wait for hero (0)
)
end
Code: Select all
script,endfeet,begin
if ((read timer (0)==(0)) and (Ham >= 5)) then
(
show textbox (992)
wait for textbox
destroy npc (1)
)
else ( show textbox (993)
wait for textbox )
clear string (1)
clear string (2)
destroy npc (1)
set hero picture (0,11,outside battle)
wait for hero (0)
end- Bob the Hamster
- Liquid Metal King Slime
- Posts: 7460
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
You can do something likeMeatballsub 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?
Code: Select all
while(keyispressed(key:ESC)==false)(
#stuff that happens over and over
wait(1)
)
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.
I suggest that you move this to a completely separate script that you will set as the "on keypress" script.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).
Code: Select all
plotscript, keypress handler (
if (key is pressed(19)) then (
Ham+=1
)
)
You want to display a global variable in a string? Try this: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.
Code: Select all
clear string(2)
append number(2, Ham)
show string at (2,30,190)
- Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact:
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:
the new, seperate on keypress script:
Code: Select all
script,blah,begin
suspend npcs
suspend player
teleport to map (76,3,5)
set hero direction (0,south)
resume player
show textbox (989)
set on keypress script (78)
while(keyispressed(01)==false) do (
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
set timer (0,5,timer:default,@endfish,1,timer flag:battle)
clear string(2)
append number(2, Reel)
show string at (2,30,190)
show string at (1,280,190)
get on keypress script
)
if (r == 1) then
(
show textbox (991)
wait for textbox
destroy npc (1)
set hero picture (0,11,outside battle)
wait for hero (0)
)
wait (1)
Ham:=0
)
endCode: Select all
script,wodod,begin
if (key is pressed(19)) then
(
Ham+=1
)
end- Bob the Hamster
- Liquid Metal King Slime
- Posts: 7460
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
Code: Select all
set on keypress script (78)Code: Select all
set on keypress script (@wodod)...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 scriptCode: Select all
set on keypress script ()Code: Select all
clear string(2)
append number(2, Reel)
show string at (2,30,190)
show string at (1,280,190) - Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact:
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:
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.
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.
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.
....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.
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 (76,3,5)
set hero direction (0,south)
resume player
show textbox (989)
set on keypress script (78)
while(keyispressed(01)==false) do (
wait for textbox
wait for scancode (46)
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 ((find hero(0)),99,outside battle)
create npc (1,3,6)
wait (50)
variable (r)
create npc (1,3,6)
wait (50)
variable (r)
r := random(0, 1)
Code: Select all
if (r == 0) then
(
set npc direction (1,left)
wait for npc (1)
show textbox (990)
wait for textbox
set timer (0,5,timer:default,@endfish,1,timer flag:battle)
clear string(2)
append number(2, Reel)
show string at (2,30,190)
show string at (1,280,190)
get on keypress script
)
Code: Select all
if (r == 1) then
(
show textbox (991)
wait for textbox
destroy npc (1)
set hero picture (0,11,outside battle)
wait for hero (0)
)
wait (1)
Ham:=0
)
endI 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.
Reel is a global variable and it is correct. Ham was just a cover up.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.
- Bob the Hamster
- Liquid Metal King Slime
- Posts: 7460
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
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.
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.
- Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact:
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:
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 (76,3,5)
set hero direction (0,south)
resume player
show textbox (989)
while(keyispressed(01)==false) do (
set on keypress script ()
wait for textbox
wait for scancode (46)
set hero picture ((find hero(0)),99,outside battle)
create npc (1,3,6)
wait (300)
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
set timer (0,5,timer:default,@endfish,1,timer flag:battle)
set on keypress script (@reelin)
)
if (r == 1) then (
show textbox (991)
wait for textbox
destroy npc (1)
set hero picture (0,11,outside battle)
wait for hero (0)
)
wait (1)
)
end
script,endfish,begin
if ((Reel) >= 25 and <=50 ) then (
show textbox (992)
wait for textbox
)
else ( show textbox (993)
wait for textbox
)
destroy npc (1)
clear string (5)
set hero picture (0,11,outside battle)
wait for hero (0)
end
script,reelin,begin
if (key is pressed(19)) then (
Reel+=1
)
else (
Reel-=1
)
$5="Reel:"
append number(5, Reel)
show string at(5, 0, 0)
end- Bob the Hamster
- Liquid Metal King Slime
- Posts: 7460
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
instead of: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.
Code: Select all
if (key is pressed(19)) thenCode: Select all
if (keyval(19) == 2) then- Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact:
- Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact:
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.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?
- Meatballsub
- Liquid Metal Slime
- Posts: 996
- Joined: Mon Oct 15, 2007 6:39 pm
- Location: Northwest Georgia
- Contact: