Help making 2 player game (moving player 2 NPC)

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Help making 2 player game (moving player 2 NPC)

Post by sheamkennedy »

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: Select all

#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.
Last edited by sheamkennedy on Sun Jan 25, 2015 11:40 pm, edited 1 time in total.
⊕ 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
User avatar
Urkelbot666
Slime Knight
Posts: 188
Joined: Sat Oct 18, 2014 5:29 pm

Post by Urkelbot666 »

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!
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

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
User avatar
msw188
Metal Slime
Posts: 783
Joined: Tue Oct 16, 2007 1:43 am
Location: Los Angeles, CA

Post by msw188 »

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
User avatar
Urkelbot666
Slime Knight
Posts: 188
Joined: Sat Oct 18, 2014 5:29 pm

Post by Urkelbot666 »

I took the code you posted, and threw it into a while loop in my FLY game with this result

Image

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: Select all

plotscript, ending, begin
set tag &#40;17,on&#41;

set tag &#40;28,on&#41;

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

	if &#40;key is pressed&#40;key&#58;Esc&#41;&#41; then &#40;
	
	&#40;suspend player&#41;&#40;exit&#41;&#41;
else 
	if &#40;key is pressed&#40;key&#58;r&#41;&#41; then &#40;
	
	&#40;suspend player&#41;&#40;reset&#41;&#41;
&#41;wait &#40;1&#41;
Last edited by Urkelbot666 on Mon Jan 26, 2015 2:06 pm, edited 3 times in total.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

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.
Last edited by TMC on Mon Jan 26, 2015 9:10 am, edited 1 time in total.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

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.
Last edited by sheamkennedy on Mon Jan 26, 2015 3:44 pm, edited 2 times in total.
⊕ 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
Post Reply