Castle Paradox Forum Index Castle Paradox

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Gamelist   Review List   Song List   All Journals   Site Stats   Search Gamelist   IRC Chat Room

Problem with 8 point turning script

 
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP!
View previous topic :: View next topic  
Author Message
H-Bomb




Joined: 24 Apr 2007
Posts: 11

PostPosted: Tue Apr 24, 2007 3:04 am    Post subject: Problem with 8 point turning script Reply with quote

I think my problem is fairly simple.

I scripted a code for facing in 8 directions, it is part of a much larger movement code, but this section has some issues. This is the chunk for turning through the full 8 directions in a clockwise sequence.

Code:
 
#Clockwise--------------------------------------------------------------------


#Normal
while (key is pressed (CW), and, get hero picture (me) == 0) do (

wait (2)
if (d==north) then, begin
set hero picture (0,6)
suspend player
wait (2)
resume player
end
if (d==east) then, begin
set hero picture (0,6)
suspend player
wait (2)
resume player
end
if (d==south) then, begin
set hero picture (0,6)
suspend player
wait (2)
resume player
end
if (d==west) then, begin
set hero picture (0,6)
suspend player
wait (2)
resume player
end
#-------------
suspend player
wait (2)
resume player
)

#Diagonal
while (key is pressed (CW), and, get hero picture (me) == 6) do (

wait (2)
if (d==north) then, begin
set hero picture (0,0)
set hero direction (me,east)
suspend player
wait (2)
resume player
end
if (d==east) then, begin
set hero picture (0,0)
set hero direction (me,south)
suspend player
wait (2)
resume player
end
if (d==south) then, begin
set hero picture (0,0)
set hero direction (me,west)
suspend player
wait (2)
resume player
end
if (d==west) then, begin
set hero picture (0,0)
set hero direction (me,north)
suspend player
wait (2)
resume player
end
#-------------
suspend player
wait (2)
resume player
)


I just want the code to require the key be pressed again to turn, or some kinda buffer clearing delay, because if the button is held down, it runs continuously, and after turning 3 points, the script locks up an endless loop script that is running in the background (on a wait (1) schedule when the problem occurs). If there is a general rule of thumb for cutting down the buffer load on codes like this, that would help too. I have quite a few on keypress commands running over an endless loop (the endless loop buffer is pretty small), and anytime I turn the character, the buffer display in debug mode (F10) shoots all the way up to red. I've tried splitting the while-key-is-pressed codes into smaller scripts a few different ways, but it doesn't seem to affect the buffer load, perhaps I am just splitting it in the wrong place?

Any help would be appreciated. I am still new at this.
Back to top
View user's profile Send private message
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Tue Apr 24, 2007 1:01 pm    Post subject: Reply with quote

I'll admit I am really confused as to what exactly this script is supposed to do, however, here is a cleaned-up version of it that uses indentation and removes all the pointless excessive suspendplayer/resumeplayer stuff.

Code:
#Clockwise--------------------------------------------------------------------


#Normal
while (key is pressed (CW), and, get hero picture (me) == 0) do (

wait (2)
suspend player
if (d==north) then, begin
  set hero picture (0,6)
  wait (2)
end
if (d==east) then, begin
  set hero picture (0,6)
  wait (2)
end
if (d==south) then, begin
  set hero picture (0,6)
  wait (2)
end
if (d==west) then, begin
  set hero picture (0,6)
  wait (2)
end
#-------------
wait (2)
resume player
)

#Diagonal
while (key is pressed (CW), and, get hero picture (me) == 6) do (

wait (2)
suspend player
if (d==north) then, begin
  set hero picture (0,0)
  set hero direction (me,east)
  wait (2)
end
if (d==east) then, begin
  set hero picture (0,0)
  set hero direction (me,south)
  wait (2)
end
if (d==south) then, begin
  set hero picture (0,0)
  set hero direction (me,west)
  wait (2)
end
if (d==west) then, begin
  set hero picture (0,0)
  set hero direction (me,north)
  wait (2)
end
#-------------
wait (2)
resume player
)


hopefully this will be easier for people to read so they can help you out further

And I strongly suspect that you don't really want to be using "while" at all in this script
Back to top
View user's profile Send private message Send e-mail Visit poster's website
msw188




Joined: 02 Jul 2003
Posts: 1041

PostPosted: Tue Apr 24, 2007 2:47 pm    Post subject: Reply with quote

Yeah, my first guess would be to change all of those while's to if's. After that I think we need more. I'm guessing that 'd' is a variable that is assigned the hero's direction, but where and when is it assigned? Also, you could probably save yourself a lot of trouble with a bit of modular arithmetic. The single line:
Code:
set hero direction (me, (d+1,mod,4) )

will 'rotate' the hero sprite clockwise, as in a north-facing sprite will face east, an east-facing sprite will face south, etc.

I'm also pretty sure that the command 'suspend player' does NOT prevent the player's keypresses from being registered, it just prevents the arrow keys from initiating movement and the alt key from initiating the menu (I THINK).
_________________
My first completed OHR game, Tales of the New World:
http://castleparadox.com/gamelist-display.php?game=161

This website link is for my funk/rock band, Euphonic Brew:
www.euphonicbrew.com
Back to top
View user's profile Send private message Visit poster's website
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Tue Apr 24, 2007 6:17 pm    Post subject: Re: Problem with 8 point turning script Reply with quote

H-Bomb wrote:
I just want the code to require the key be pressed again to turn, or some kinda buffer clearing delay, because if the button is held down, it runs continuously, and after turning 3 points, the script locks up


So what is the problem here: holding down a key to turn locks up your script so you want to disallow it, or you want the player to have to press the key repeatedly to turn continously? We'll need to know that before knowing in which way to fix your script.

H-Bomb wrote:
I have quite a few on keypress commands running over an endless loop


So how exactly is this snippet of code getting called? If I understand you, it's not getting called from an onkeypress script, but from an endless loop in some other script, and you don't have an onkeypress script. Correct?

For splitting scripts up to reduce script buffer usage, see 'How do I avoid script buffer overflows?'
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
H-Bomb




Joined: 24 Apr 2007
Posts: 11

PostPosted: Tue Apr 24, 2007 7:54 pm    Post subject: Reply with quote

Sorry to be so shortwinded in the initial post. The script is called by an on keypress script, with a separate endless loop running in the background on the same map.

This script changes allows you to face 8 directions, changing the NPC Image accordingly when you are diagonal (image 6). A part of the script which isn't included here is hitting the up key to advance in the direction you are facing, and the down key to move backwards, or in this case, stop moving.

The entire script runs fine except for the heavy buffer load, and the complete lockup when the key is held down too long.

So yes, it is called on-keypress, the endless loop is a different script, and I just want the right-arrow (CW) to have to be pressed again to execute the next command, and not turn more than 1 click at a time if the button is held. I have a sample RPG file I've slapped together with the corresponding NPCs and complete plotscript code for the movement system, I can post it if need be.
Back to top
View user's profile Send private message
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Wed Apr 25, 2007 12:10 am    Post subject: Reply with quote

Ok, the first thing to realise is the danger of putting wait statements in an onkeypress script. Do this wrong and the script will be run over and over.

I wrote an article on this: 'Why does my on-keypress script freeze/produce errors when I hold down a key?'

So, assuming that the loop that you have running in the background is not doing anything important and you can pause it without a problem (if this is not the case, you need to rewrite everything), you can use wait commands in an onkeypress script, but you have to stop it from getting called repeatedly.

Your other problem is also solvable. However, in the next version, Ubersetzung, to be released soon, things will change.

Hasta-la-qb+:
-Instead of "key is pressed (key:something)" write "key is pressed (key:something) >= 2". This will be true only if the key has just been pressed this tick.

Ubersetzung/'WIP' builds:
Old games (compiled with a hasta-la-qb plotscr.hsd or earlier) will work as intended.
For new games:
-key is pressed returns 0 or 1 only. Use keyval instead: write "keyval (key:something) >= 2"
-Actually, keyval is the old key is pressed command under a new name.

So you'll have to either use a WIP, or stick to hasta-la-qb

Since you don't want the player to be able to turn continously, you don't need to use while at all.

But wait! One more thing. If you have code that looks like this:

Code:
if (a == 0) then (
  #...
  a := 6
)
if (a == 6) then (
  #...
  a := 0
)


Then you have a bug if you don't want both if blocks to be run.

Your code should now look like this:
Code:
#Clockwise--------------------------------------------------------------------
if (keyval (CW) >= 2) then (

  #Normal
  if (get hero picture (me) == 0) then (
    #...
  ) else (

    #Diagonal
    if (get hero picture (me) == 6) then (
      #...
    )
  )
)

_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
H-Bomb




Joined: 24 Apr 2007
Posts: 11

PostPosted: Wed Apr 25, 2007 2:44 pm    Post subject: Reply with quote

Hi, I changed my script to look more like the last one, it come out looking like this:
Code:

define script (2,movement,none)
define constant(KEY:UP,Go)
define constant(KEY:DOWN,Stop)
define constant(KEY:LEFT,CCW)
define constant(KEY:RIGHT,CW)
script, movement, begin
d := hero direction (me)

#On KEYPRESS==================================================================
##############################################################################
#Clockwise--------------------------------------------------------------------


#Normal
if (keyval (CW) >=2, and, get hero picture (me) == 0) then (

suspend player

if (d==north) then, begin
set hero picture (0,6)
end
if (d==east) then, begin
set hero picture (0,6)
end
if (d==south) then, begin
set hero picture (0,6)
end
if (d==west) then, begin
set hero picture (0,6)
end

resume player

#-------------
)

#Diagonal
if (keyval (CW) >=2, and, get hero picture (me) == 6) then (

suspend player

if (d==north) then, begin
set hero picture (0,0)
set hero direction (me,east)
end
if (d==east) then, begin
set hero picture (0,0)
set hero direction (me,south)
end
if (d==south) then, begin
set hero picture (0,0)
set hero direction (me,west)
end
if (d==west) then, begin
set hero picture (0,0)
set hero direction (me,north)
end

resume player
#-------------
)


This code doesn't seem to work properly because it doesn't bypass the default values of the arrow keys. Perhaps I should couple in a "while" command to handle suspending the player so he doesn't move east and cancel out my movement script?

I'm still not sure if anyone understands what I'm going for, hitting the right arrow turns the character clockwise, I can't use the standard arithmatic to turn the character clockwise, because there is no constant such as "northeast." That value is kept track of by checking for the NPC picture depicting the character as facing diagonal, and the direction. I suppose I could clean the script up and use the standard "turn clockwise" arithmatic in the parts where it applies, but the indents and "cleanness" of my code are the least of my worries, unless it clears up my buffer load. As I stated in the initial post, the original code worked perfectly, and is ready to impliment into the game, if it wasn't for the off chance that the player will HOLD the key instead of just tapping it as intended. Also, I am 100% aware that the wait and suspend player commands are "useless," that's why I came here to ask these questions in the first place. I've tried everything I can think of, and can't get the desired result. You guys were probably onto something when you said to ditch my while commands, so I gave it a try, but I'm more confused now than before.

I would've liked to check out the article written specifically about this problem, by Mr. Cacti, but the link seemed dead.

If anyone can help, I'm certainly willing to rewrite this script from the ground up if my approach is altogether incorrect, I have an 8 point turning script that rotates the character clockwise (I can simply alter the code for counter clockwise). The up key sets the player speed to 5 and turns on a tag, the tag being on meets the conditions of a walk hero script in the separate endless loop, which checks direction, and walks the character in the direction he is facing (coupling 2 directions at once for the diagonal effect when required, and setting speed to 4 instead of 5, so the hero doesn't appear to walk faster diagonally than straight). The reason I call the walk script in the endless loop is because I want the character to keep moving in that direction until the down key is tapped. I also call the movement script by name in the endless loop, because I wanted to make sure the character can still turn while in motion, and remain in motion.

This new code doesn't work at all, I've tried it without the endless loop, and it still doesn't work. Perhaps there is no way to take my original code, and add the type of delay I am looking for, so it doesn't lock up my endless loop, in which case, I will have to rethink my approach.

Sorry to be so painfully specific, I'm just hoping someone can give me a coherant answer to this question, and I'm either haven't explained this correctly or just don't understand where anyone is saying I've gone wrong. Everything I know about plotscripts I learned from the vague plotdictionary.html file, and enivitably, a painful stretch of trial and error, so I apologize if I seem a little uninformed.
Back to top
View user's profile Send private message
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Thu Apr 26, 2007 4:09 am    Post subject: Reply with quote

Opps, seems the wiki tag escapes forward slashes when it shouldn't. But I really shouldn't be using forward slashes. Here's the link:

http://gilgamesh.dnsalias.org:8080/wiki/ohrrpgce/index.php/Why_does_my_on-keypress_script_freeze/produce_errors_when_I_hold_down_a_key%3F

Quote:
This code doesn't seem to work properly because it doesn't bypass the default values of the arrow keys.


Ok, to fix this, put a wait back into your script - at least a wait(1) between suspend player and resume player. However, maybe this will interfere with your endless loop, maybe you need to have the player suspended the whole time.

Have another look at what I said after "But wait! One more thing...": with those two if blocks you have, you still have that problem.

Sorry, can't write a very full response right now.
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
Inferior Minion
Metric Ruler



Joined: 03 Jan 2003
Posts: 741
Location: Santa Barbara, CA

PostPosted: Thu Apr 26, 2007 10:39 am    Post subject: Reply with quote

The Mad Cacti wrote:
Opps, seems the wiki tag escapes forward slashes when it shouldn't. But I really shouldn't be using forward slashes. Here's the link:

http://gilgamesh.dnsalias.org:8080/wiki/ohrrpgce/index.php/Why_does_my_on-keypress_script_freeze/produce_errors_when_I_hold_down_a_key%3F


Off topic, but I've fixed this for the wiki tag:

'Why does my on-keypress script freeze/produce errors when I hold down a key?'
_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address MSN Messenger
H-Bomb




Joined: 24 Apr 2007
Posts: 11

PostPosted: Sat Apr 28, 2007 2:30 am    Post subject: Reply with quote

it took a while, but I managed to figure the script out, so if anyone was interested, here's how it's done.

Code:

#Clockwise--------------------------------------------------------------------
#Normal
if (key is pressed (CW), and, get hero picture (me) == 0) then (
if (d==north) then (
set hero picture (0,6))
if (d==east) then (
set hero picture (0,6))
if (d==south) then (
set hero picture (0,6))
if (d==west) then (
set hero picture (0,6))
#-------------
wait (2))

#Diagonal
if (key is pressed (CW), and, get hero picture (me) == 6) then (
if (d==north) then (
set hero picture (0,0)
set hero direction (me,east))
if (d==east) then (
set hero picture (0,0)
set hero direction (me,south))
if (d==south) then (
set hero picture (0,0)
set hero direction (me,west))
if (d==west) then (
set hero picture (0,0)
set hero direction (me,north))
#-------------
wait (2))


I stuck with the key is pressed command because keyval was giving me alot of weird results. The suspend player command is now running in the same script as the endless loop, but before the loop itself starts, making the player suspended the whole time. The required wait command for the endless loop depend on what is happening, during movement, it is set to wait for player, stopping is also set to wait for player so you don't get misaligned, if you are still, I set it to "wait for key." It doesn't require the player to hit the key again, but it creates a buffer clearing delay, which turns out to be a more convenient result for me. Those 3 magic words are pretty much what I've been looking for for almost 3 days now, how humiliating.

Perhaps someone can make a suggestion about how to make these scripts lighter on the buffer? I managed to cut down my endless loop by splitting it into smaller scripts and calling them by name, one after the other, but I am not sure if I know how to approach lightening to load on on all of the onkeypress scripts. Is the heavy load because the "movement" script contains so many "if (key is pressed)" commands? Or is it the complexity of the individual "if (key is pressed)" commands that leads to this? If anyone has any advice or bad news about this, I'd love to hear it, but the script is working great now, aside from the buffer, but it no longer locks up the loop. It does however kill the menu key (and other default keys I imagine), but that's what I wanted.

Thanks to all you crazy hamsters and Mr. Paige himself for your insights on this thusfar. I'll have more questions soon, I'm sure.
Back to top
View user's profile Send private message
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Sat Apr 28, 2007 8:08 am    Post subject: Reply with quote

Oh, I forgot that keyval interprets key repeat (which starts after a few ticks held down) as new keypresses. Which is probably a nuisance. Just now I couldn't understand why it wasn't working properly for me either until I remembered.

I'm not sure what you mean by a "buffer clearing delay". But I'm surprised that you have script buffer issues with just the background loop and keypress script. That must be a large keypress script. You could split it up in the same way you did the endless loop script.

Script buffer space usage is approximately proportional to the number of commands in your script (like +, if, walkhero), complexity in other words.
The next release (and current WIP builds) don't have a script buffer, so you'll be able to forget about the problem.
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
H-Bomb




Joined: 24 Apr 2007
Posts: 11

PostPosted: Wed May 02, 2007 7:27 pm    Post subject: Reply with quote

Hi TMC, what I meant by a buffer clearing delay was a split in the codes being processed, allowing the interpreter to dump what it currently has in the buffer, and load up a new batch. Similar to the science of splitting a large command into several smaller parts.

This is probably a poor way to describe what the wait for key command is doing here, but just what it seemed like at first glance. However, now that I think about it, I think this command is just eliminating any room for error in the timing of the events, I may be wrong about both theories, all I know is that it seems to work. Happy

Good news about the buffer by the way. Now if I'd live to see an end to the 1000 usable only once tag-limit, life couldn't get any sweeter.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP! All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group