Post new topic    
Liquid Metal Slime
Send private message
I need some help finding the issue with this script 
 PostThu Sep 26, 2013 9:56 pm
Send private message Reply with quote
I'm attempting to make a line of sight script for my game such that if you enter the 'sight' of an enemy NPC they will be alerted and chase your character.

So far I have made a script that shoots out a sequence of 'invisible sight NPCs' the the west of the 'guard NPC.' For the sake of making my script easier to debug I have actually made these 'sight NPCs' visible in the game testing so I could see just exactly what's happening.

The problem I'm having:

I specifically made my script as a 'while' loop so that while the 'guard NPC' is facing in the west direction the Line of Sight will be carried out. You can see in the video provided that the 'guard NPC' starts out facing west and all goes well. But if the NPC is turned another direction and then back to the westward direction that the script does not continue to work. Can anyone tell me why this is? It may be important to note that this script is being run as an Autorun Script for the map.


Here's the script thus far:

Code:


   global variable (0, x)
   global variable (1, y)

#--------------------LINE OF SIGHT--------------------------

plotscript, Line Of Sight, begin

x := hero x (find hero (0))

y := hero y (find hero (0))



variable (xNPC, yNPC, z)

xNPC := NPC X (0)

yNPC := NPC Y (0)

z := 1

#Line of sight sweep for west-facing NPC
While (NPC direction (0) == west) do(
   
   #Line of sight point west
      #####         
      If (read zone (10, xNPC--1, yNPC) == true) then(
         break
         )
      else (
      create NPC (1, xNPC--1, yNPC, west)
      wait (1)
      destroy NPC (1)
      wait (1)
      )
      #####
      If (read zone (10, xNPC--2, yNPC) == true) then(
         break
         )
      else (
      create NPC (1, xNPC--2, yNPC, west)
      wait (1)
      destroy NPC (1)
      wait (1)
      )
      #####
      If (read zone (10, xNPC--3, yNPC) == true) then(
         break
         )
      else (
      create NPC (1, xNPC--3, yNPC, west)
      wait (1)
      destroy NPC (1)
      wait (1)
      )
      #####
      If (read zone (10, xNPC--4, yNPC) == true) then(
         break
         )
      else (
      create NPC (1, xNPC--4, yNPC, west)
      wait (1)
      destroy NPC (1)
      wait (1)
      )
      #####
      If (read zone (10, xNPC--5, yNPC) == true) then(
         break
         )
      else (
      create NPC (1, xNPC--5, yNPC, west)
      wait (1)
      destroy NPC (1)
      wait (1)
      )
      #####
      
   #May need to add an additional break and/or wait here...
         
   
#wait (30)
#destroy NPC (1)
#wait (30)
)
end



And the video of this issue can be seen here:


http://www.youtube.com/watch?v=A4DvThX7ui8&list=SP-ywmUILu1tgN7OJ_KDFM2wZOqoSoxvid
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Super Slime
Send private message
 
 PostThu Sep 26, 2013 10:40 pm
Send private message Reply with quote
The script only executes once. Think of your script as if it were this:

Code:
plotscript, Line of Sight, begin
  do some things
  as long as NPC is facing west, do other things
end


Once the NPC faces another direction, the script is over.
Mega Tact v1.1
Super Penguin Chef
Wizard Blocks
Liquid Metal Slime
Send private message
 
 PostThu Sep 26, 2013 10:44 pm
Send private message Reply with quote
I see. So how could I implement the script so it re-triggers frequently. Like every time there is a change in the NPC's direction?

Possibly by containing this script within some other condition that's always true? Or would that just cause problems with an endless loop?
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Metal Slime
Send private message
 
 PostThu Sep 26, 2013 11:20 pm
Send private message Reply with quote
A classic version is to use:
[pseudocode]
Code:
while(currentmap== whatever map this happens on),do
 begin
  if(npc is facing west),then
   begin
    all your stuff
   end
 end


However, if the looking NPC is stationary (and all that matters is seeing the hero), then this may be easier to handle in an on-step script, since the only time that checking any of this is important is when the hero takes a step.
I am Srime
Super Slime
Send private message
 
 PostThu Sep 26, 2013 11:53 pm
Send private message Reply with quote
Also, remember that there should always be a wait command in any while loop that you want to keep running while the game plays.
Mega Tact v1.1
Super Penguin Chef
Wizard Blocks
Metal Slime
Send private message
 
 PostFri Sep 27, 2013 1:24 am
Send private message Reply with quote
msw188 wrote:
then this may be easier to handle in an on-step script, since the only time that checking any of this is important is when the hero takes a step.


If your enemy NPCs don't move around in some way, that is.
Metal Slime
Send private message
 
 PostFri Sep 27, 2013 3:25 am
Send private message Reply with quote
This is why I mentioned that the looking NPCs should be stationary. Although if there are other NPCs walking around, able to block paths of sight, then this thing really does need the 'line of sight NPC' that travels out (this is what I did for a part of Tales 1).
I am Srime
Liquid Metal Slime
Send private message
 
 PostFri Sep 27, 2013 6:35 am
Send private message Reply with quote
Thanks everyone for all the help. I'll give some of this a try and I'll definitely want to make this script work with a moving and turning NPC once I overcome this issue. Thanks for the stationary (on-step) idea too, I'll use something like that for certain guard NPC's in certain cases, especially if I want to minimize the amount of NPC's on the screen.

Quote:

Also, remember that there should always be a wait command in any while loop that you want to keep running while the game plays.


Could you explain why it's necessary to have a wait in a while loop if I want the script to keep running while the game plays? If it's too complexed or tedious to explain don't worry about it. I'm curious and knowing the answer will help me remember to add in a wait function for cases like this.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Metal King Slime
Send private message
Metal King Slime
Send private message
 
 PostMon Sep 30, 2013 7:35 am
Send private message Reply with quote
sheamkennedy wrote:
Could you explain why it's necessary to have a wait in a while loop if I want the script to keep running while the game plays? If it's too complexed or tedious to explain don't worry about it. I'm curious and knowing the answer will help me remember to add in a wait function for cases like this.


Scripts don't run at the same time as the engine. First of all, realise that a game runs one tick at a time. There are 18.3 ticks in a second. Each tick the engine checks for new input, runs scripts, advances animations and movement (eg camera), and redraws the screen, in that order. Crucially it runs scripts until it either waits or finishes. If a script just keeps running in an infinite loop without waiting, then it's not automatically interrupted (unless a couple seconds go by and the player impatiently hits a key, triggering the script interrupt). So you must include a wait (any kind of wait command) in any script that needs to keep running from one tick to the next.

You can also read this artcle for more details: How does plotscripting work?
Display posts from previous: