Post new topic    
King Slime
Send private message
I fail at walking. 
 PostFri Oct 07, 2011 8:50 pm
Send private message Reply with quote
I'm making a game, Maze of Crates, which involves a hero and an NPC. I'm trying to make a script that enables the movement of of NPC 0.

Code:
include,plotscr.hsd
include,cratemaze.hsi
include,scancode.hsi

plotscript, master controls, begin
   master k controls
   end
   
script, master k controls, begin
   A key
   S key
   D key
   W key
   end

script, A key, begin
   if (key is pressed(key:A)) then
   walk NPC(0,left,1)
   wait for NPC(0)
   
   end

script, S key, begin
   if (key is pressed(key:S)) then
   walk NPC(0,down,1)
   wait for NPC(0)
   
   end
   
script, D key, begin
   if (key is pressed(key:D)) then
   walk NPC(0,right,1)
   wait for NPC(0)
   
   end
   
script, W key, begin
   if (key is pressed(key:W)) then
   walk NPC(0,up,1)
   wait for NPC(0)
   
   end


It compiles, so I assigned master controls to key is pressed script. However, when I try to move NPC 0, he starts to trip out and walk werever he wants, and then I get an error warning ingame.

Whats wrong with this script?
Liquid Metal King Slime
Send private message
 
 PostFri Oct 07, 2011 9:17 pm
Send private message Reply with quote
You are running "master controls" as an on-keypress script, right? Because of the "wait for NPC" commands, the script gets re-triggered many times for every keypress. Try something like this instead:

Code:

plotscript, movement keys, begin
  if (key is pressed(key:up)    && NPC is walking(0) == false) then(walk NPC (0, north, 1))
  if (key is pressed(key:right) && NPC is walking(0) == false) then(walk NPC (0, east, 1))
  if (key is pressed(key:down)  && NPC is walking(0) == false) then(walk NPC (0, south, 1))
  if (key is pressed(key:left)  && NPC is walking(0) == false) then(walk NPC (0, west, 1))
end


I haven't tested that in a game yet, but I think it is correct. Notice that there are no wait commands, so the script will always finish on the same tick that it started on, and therefore can't have more than one copy of itself interrupt itself.
King Slime
Send private message
 
 PostFri Oct 07, 2011 10:28 pm
Send private message Reply with quote
Thanks, it works. Also, is there a way to set a tag on if an NPC is at a certain spot?
Liquid Metal King Slime
Send private message
 
 PostFri Oct 07, 2011 11:54 pm
Send private message Reply with quote
You can do that by checking the NPC's X and Y position,but I think it is easier with the "NPC at spot" command

This example checks to see if NPC 0 is standing on position 24,57 and sets a tag on if so or off if not

Code:

script, check special npc spot, begin
  variable(ref)
  ref := NPC at spot(24, 57)
  if (ref && get NPC ID(ref) == 0) then(
    set tag(tag:foo, ON)
  )else(
    set tag(tag:foo, OFF)
  )
end


If you want to check a whole lot of different x,y locations, this method will get really tedious, and you should switch to using zones instead.
King Slime
Send private message
 
 PostSat Oct 08, 2011 12:37 am
Send private message Reply with quote
Well, i'm going to be checking 3 areas per map, maybe 3+ later on, so enlighten me on zones.

Be warned, i'm using Zenzizenzic, and not a nightly.
Nightlies arn't the way I fly. My computer doesn't like them.
Liquid Metal King Slime
Send private message
 
 PostMon Oct 10, 2011 3:30 pm
Send private message Reply with quote
Zones are fully functional in Zenzizenzic.

Also, Pepsi Ranger's article about zones in the latest HamsterSpeak is worth checking out.

Anyway. Unless you tell me otherwise, I am going to assume that all three of these zones are supposed to set the same tag when NPC 0 steps on them.

Lets say you are using zone 1 for these three special areas. Mark them all with zone 1 on your map. Then use a script like this to check for them:

Code:

script, check special npc spot, begin
  variable(x, y)
  x := NPC X(0)
  y := NPC Y(0)
  if(read zone(1, x, y)) then(
    set tag(tag:foo, ON)
  )else(
    set tag(tag:foo, OFF)
  )
end


And here is an even shorter version that does exactly the same thing (but don't use it if it is confusing)

Code:

script, check special npc spot, begin
  variable(x, y)
  x := NPC X(0)
  y := NPC Y(0)
  set tag(tag:foo, read zone(1, x, y))
end
Display posts from previous: