Post new topic    
Liquid Metal Slime
Send private message
Help making 2 player game (moving player 2 NPC) 
 PostSun Jan 25, 2015 11:26 pm
Send private message Reply with quote
I'm making a 2 player game where Player 1 (the hero) competes with Player 2 (the NPC) to see who is the last man standing in a gun battle.

Player 1 simply presses up, down, left, right to move around nicely.

Player 2 on the other hand is coded to be controlled by A, S, D, W. I used this script I found on the wiki to do so:

Code:

#Move NPC 0 (Player 2)
variable (npc cursor)
  npc cursor := 0   # first NPC with ID 0
  if (npc is walking (npc cursor) == false) then (
    # Only move the NPC after they've finished the previous movement,
    # also use elseif to prevent moving in two directions at once.
    if      (key is pressed (key:A))  then (walk npc (npc cursor, left, 1))
    else if (key is pressed (key:D)) then (walk npc (npc cursor, right, 1))
    else if (key is pressed (key:W))    then (walk npc (npc cursor, up, 1))
    else if (key is pressed (key:S))  then (walk npc (npc cursor, down, 1))
  )


Unfortunately this code does not 100% mimic the regular hero control scheme. I notice while moving the NPC in a new direction they will take 1 step, wait a few ticks, then continue walking in that direction. This repeats every time I change direction with the NPC. Is this a problem with the code snippet or is something else in my game perhaps interfering?

EDIT: Also I have more questions regarding why player input while game goes from title to map screen (or through doors) can cause unexpected errors in game, but I figure I'll solve this first and see if it makes a difference.
⊕ 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
Slime Knight
Send private message
 
 PostMon Jan 26, 2015 12:40 am
Send private message Reply with quote
From a quick look, it seems to me that the code is only going to do anything if (NPC is walking==false) So the NPC would have to stop walking to meet the condition of walking == false, hence it would pause after each step.
...But if it's only pausing with each new direction, I'm not sure...
...That's just me thinking out loud right now, and I haven't attempted anything like this... I hope you get the bugs worked out though!
Liquid Metal Slime
Send private message
 
 PostMon Jan 26, 2015 3:20 am
Send private message Reply with quote
Urkelbot666 wrote:
From a quick look, it seems to me that the code is only going to do anything if (NPC is walking==false) So the NPC would have to stop walking to meet the condition of walking == false, hence it would pause after each step.
...But if it's only pausing with each new direction, I'm not sure...
...That's just me thinking out loud right now, and I haven't attempted anything like this... I hope you get the bugs worked out though!


That condition is just to prevent movement while in mid-step. Though it may be the source of the problem. Maybe I can change the condition to whether the x,y is divisible by 20... Either way I'll wait for more opinions and then give a few things a try.
⊕ 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
 
 PostMon Jan 26, 2015 3:27 am
Send private message Reply with quote
Two potentially important questions:
Are there any other scripts running during this time? Is this script being called by the map and then looping, or is this script being called onkeypress?
I am Srime
Slime Knight
Send private message
 
 PostMon Jan 26, 2015 3:58 am
Send private message Reply with quote
I took the code you posted, and threw it into a while loop in my FLY game with this result



I don't seem to be getting any pauses while moving. Is this what is going on for you?

In case you couldn't tell, player 2 is the flyswatter... The NPC is set to stand still, but has a move speed or 4.

EDIT: just more info.... I;ve copied my mess of a code below in case it is of any help. As you know the game deals with hitting levers, so I've added your script (Player2) into my while (lever<6>>0) The script below is a Map Autorun.

Code:
plotscript, ending, begin
set tag (17,on)

set tag (28,on)

if  (inventory (item:shell) >>0)
then (delete item (item:shell, 3))
   stop sound (18),
while (lever<6)
do( (player2)
   if ((inventory (item:shell))
) then (if
   (keyval (key:space)>>0)
then(
   (shootabullet2) decrement (gun,1))
else if(item count in slot (0)<<1) then
 (play sound (2,false,false), show map)
) wait (1)

   if (key is pressed(key:Esc)) then (
   
   (suspend player)(exit))
else
   if (key is pressed(key:r)) then (
   
   (suspend player)(reset))
)wait (1)
Metal King Slime
Send private message
 
 PostMon Jan 26, 2015 9:09 am
Send private message Reply with quote
I looked into this. It turns out that that script used as an on-key-press script works correctly for arrow keys, but not A/S/D/W, because holding down an arrow key (or use or cancel) causes the on-keypress script to be continually triggered each tick, while holding down other keys causes the on-keypress script only to trigger at the rate of key repeat. Which usually means that there's a pause after the first step, and t's smooth after that.

The simple solution is to put this in a while loop rather than on-keypress.

However, that inconsistent on-keypress triggering doesn't make sense, so I've changed it to always trigger when keys are held down.

Urkelbot666, you need to select "Disable HTML in this post" when pasting scripts, or the < and >'s mess things up.
Liquid Metal Slime
Send private message
 
 PostMon Jan 26, 2015 3:26 pm
Send private message Reply with quote
TMC wrote:
The simple solution is to put this in a while loop rather than on-keypress.

However, that inconsistent on-keypress triggering doesn't make sense, so I've changed it to always trigger when keys are held down.


So are you saying I should change it to a while loop? Or will downloading the latest nightly just make what I already have work without any changes?

EDIT: Shouldn't matter either way. I changed to the while loop and it seems to work smoothly. Thank you.
⊕ 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
Display posts from previous: