Author Topic: Crash When Loading 2P Game  (Read 6898 times)

GameDeveloper

  • Guest
Crash When Loading 2P Game
« on: 2005-12-27, 07:43:06 PM »
   I was wondering if there is any possible line that i could add to this script so that when I load a game [a 2 player game] it can stop crashing.  I can see that saving works just fine, but when I try to load a game, it crashes...  Is there anything I could add, or maybe someone else could add?  This is the new two Player script, so any help would be greatly appreciated since i am trying to create an open ended game and not being able to load a game defeats that premise.

Here it is:

Quote
Option Explicit

Dim oPlayer2
Dim oPlayer2Map

Set oPlayer2 = NewGameDevObj("Player")

Sub Player_OnPlayInit
   Dim InvIdx

   MovePlayer2ToPlayer1Map
   With ProjectObj.GamePlayer
      For InvIdx = 0 To .InventoryCount - 1
         oPlayer2.AddInventoryItem .InvQuantityDisplayType(InvIdx), .InvDisplayX(InvIdx) + 320, .InvDisplayY(InvIdx)
         oPlayer2.InventoryItemName(InvIdx) = .InventoryItemName(InvIdx)
         oPlayer2.SetInventoryTile InvIdx, .InvIconTilesetIdx(InvIdx), .InvIconTileIdx(InvIdx)
         oPlayer2.InvQuantityOwned(InvIdx) = .InvQuantityOwned(InvIdx)
         oPlayer2.InvIconCountPerRepeat(InvIdx) = .InvIconCountPerRepeat(InvIdx)
         oPlayer2.InvSetBarInfo InvIdx, .InvBarColor(InvIdx), .InvBarThickness(InvIdx), .InvBarLength(InvIdx), .InvBarBackgroundColor(InvIdx), .InvBarOutlineColor(InvIdx)
         oPlayer2.InvMaxQuantity(InvIdx) = .InvMaxQuantity(InvIdx)
      Next
      oPlayer2.ScrollMarginX = .ScrollMarginX
      oPlayer2.ScrollMarginY = .ScrollMarginY
      oPlayer2.InvBarMargin = .InvBarMargin
   End With
   with oPlayer2
      .KeyConfig(0) = 87 ' W
      .KeyConfig(1) = 65 ' A
      .KeyConfig(2) = 68 ' D
      .KeyConfig(3) = 83 ' S
      .KeyConfig(4) = 20 ' Caps Lock
      .KeyConfig(5) = 9 ' Tab
      .KeyConfig(6) = 112 ' F1
      .KeyConfig(7) = 113 ' F2
   End With
End Sub

Sub MovePlayer2ToPlayer1Map
   With ProjectObj.GamePlayer
      Set oPlayer2.rMap = .rMap
      Set oPlayer2Map = .rMap
      Set oPlayer2.Disp = .Disp
      Set oPlayer2.Touch = NewGameDevObj("MapInteract")
      Set oPlayer2.PlayerSprite = ProjectObj.GamePlayer.rMap.MapLayer(0).Sprite(1)
      Set oPlayer2.InterestingTiles = .InterestingTiles
      Set oPlayer2.FirstTouchTiles = .FirstTouchTiles
      oPlayer2.PrepareMapInventory
   End With
End Sub

Sub MovePlayer1ToPlayer2Map
   With ProjectObj.GamePlayer
      Set .rMap = oPlayer2.rMap
      .InitPlayerSprite
      Set .rMap = oPlayer2.rMap
      Set .Disp = oPlayer2.Disp
      .PrepareMapInventory
   End With
End Sub

Sub Player_OnBeforeMoveSprites()
   Dim oMap

   oPlayer2.AutoScrollMap

   If Not oPlayer2.rMap Is ProjectObj.GamePlayer.rMap Then
      MovePlayer2ToPlayer1Map
   End If

   Set oMap = oPlayer2.rMap

   oMap.ViewLeft = 320
   oMap.Draw oPlayer2.MapScrollX, oPlayer2.MapScrollY, False
   oMap.ViewLeft = 0


   oPlayer2.DrawInventory
   oPlayer2.PollJoystick 1
   if oPlayer2.JSValid Then oPlayer2.JoyMove
   oPlayer2.CtlActions = oPlayer2.KeyActions Or oPlayer2.JoyActions
   oPlayer2.PlayerSprite.ProcessAction(oPlayer2.CtlActions)
   oPlayer2.PlayerSprite.ReactToSolid()
End Sub

Sub Player_OnAfterMoveSprites()
   oPlayer2.Touch.Move oPlayer2.PlayerSprite.X, oPlayer2.PlayerSprite.Y
   oPlayer2.BtnStatesOld = oPlayer2.BtnStates
   oPlayer2.BtnStates = oPlayer2.TranslateCtlActions(oPlayer2.CtlActions)
   oPlayer2.Touch.TouchTest oPlayer2.PlayerSprite, oPlayer2.InterestingTiles, oPlayer2.FirstTouchTiles
   If Not oPlayer2.rMap Is ProjectObj.GamePlayer.rMap Then
      MovePlayer1ToPlayer2Map
      MovePlayer2ToPlayer1Map
   End If
End Sub

Sub Player2Touch_OnTouchTile(TileX, TileY,TileVal, bNewTouch)
   CurrentDisplay.Close
End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.SinkObjectEvents HostObj.AsObject(oPlayer2), "Player2"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16

Sorry for the long post...

EDIT: Didn't include the whole thing...but it's there now.
« Last Edit: 2005-12-27, 08:02:05 PM by GameDeveloper »

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Crash When Loading 2P Game
« Reply #1 on: 2005-12-27, 08:01:36 PM »
My goodness... there could be so many things going wrong...

First of all, are the two players on the same map when saving or not?  Are there sprites being deleted etc. during the game that could change which sprite you are using as player 2?  Are you switching sprites during gameplay?  I can't even begin to think of all the questions I could ask that pertain to this.  And also I have no idea how this script works :P (so maybe I'm not the best person to ask, but these are probably good questions to answer to help facilitate the solution)
Edward Dassmesser

GameDeveloper

  • Guest
Re: Crash When Loading 2P Game
« Reply #2 on: 2005-12-27, 08:11:06 PM »
Well, to answer your questions:

1.  Yes, both players are on the same map when saving.

2.  Actually, no they aren't being deleted, and player 2 is not being changed [as a result of sprites being deleted].

3.  Yes, I am switching sprites during gameplay.

Well, it sounds like I might have a problem here...  Is there any possible way to do this?  I'll be happy to answer any more questions that could possibly lead to a solution.
« Last Edit: 2005-12-27, 08:14:40 PM by GameDeveloper »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Crash When Loading 2P Game
« Reply #3 on: 2005-12-28, 09:04:38 AM »
I'm still looking into it.  Time's been tight with all the travelling and celebrating going on lately :).

GameDeveloper

  • Guest
Re: Crash When Loading 2P Game
« Reply #4 on: 2005-12-28, 09:42:49 AM »
That's no problem!  Take all the time you need, I understand.  Thanks for helping me.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Crash When Loading 2P Game
« Reply #5 on: 2005-12-28, 12:35:16 PM »
Phew, this took a little more effort than I thought, but I think I have something good.  One problem is that it relies on the Scripting.FileSystem object in order to save the data for player 2 (all of player 2's inventory etc) and some systems get nervous about script accessing the file system (especially virus scanners and the sort).  But it works fine on my system.
If it becomes a problem we can look into replacing FileSystemObject with the file-scripting object posted on the GameDev project which I hear is a little more limited/secure/safe.

Anyway, it's posted under the same name (GD2Player.zip) at: http://gamedevprj.sourceforge.net/files/.
You can now
  • Play on 2 separate maps at one
  • Have either player 1 or player 2 be the first to jump to a new map, leaving the other player on the old map
  • Save and load without errors (player 1 has Ctrl set up to save and Space set up to load on map 1)

The one oddity that I didn't bother cleaning up is that the player sprite gets left behind on the original map when switching to the new map, but that shouldn't be too hard to work out on your own.

You should be aware that I changed the basic architecture a bit.  Now instead of having the player sprites be "Initial Instance" sprites, they don't get created by default.  Instead the default player sprite on each map is set to some sprite (which should probably be off-screen or invisible in a real project) that is sitting on a special function that checks whether you are player 1 or player 2 and activates a "switch to sprite" special function accordingly.  There are 2 inventory items to help you create these kinds of special functions that behave differently based on which player is activating them.  The inventory items are set up in the project itself, but the InitialQuantity values for player 2 are set in script for these items.

That's all I can think of to say about it now.  Give it a try and let me know how it goes.

GameDeveloper

  • Guest
Re: Crash When Loading 2P Game
« Reply #6 on: 2005-12-28, 01:54:30 PM »
I just tested it, and everything works just fine.  my firewall blocked the script, but I allowed access and everything seems to be working.  Wow, I don't know how you did it, but thanks :)!  It looks like I'll be able to make my large game now.

I was wondering... when you say it has to 'check' for the player on each map...does that mean i'll have to make a function on each map named 'P1' and 'P2' and they check for each and switch accordingly?  If that's the case, then that shouldn't be a problem.  I did notice one small problem...when player 2 saved, and also when player 2 to loaded the game, it was really messed up...  It's fine, though.  i'll just make it so player1 has to make the decision to save and load, since it worked that way.

Thank you, and I'll mention if I see anything strange.

GameDeveloper

  • Guest
Re: Crash When Loading 2P Game
« Reply #7 on: 2005-12-28, 04:17:48 PM »
I believe I found a problem... when i save and then load, everything works fine.  in that same loaded game, however, I load it agian, but then Player 2 can't move at all... I was wondering, is there some way around this problem?  If not, then i could just improvise.

Thanks agian for helping me.

EDIT: Actually, everytime I load a game, the second player is frozen... if there's something wrong with the script, could you at some point help me get through the problem? Or, If you think it's my sprites, then please ask questions that would lead to solving this problem.
« Last Edit: 2005-12-28, 04:31:54 PM by GameDeveloper »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Crash When Loading 2P Game
« Reply #8 on: 2005-12-29, 09:33:37 AM »
In the sample game, the loading seems to be working OK -- do you observe the same thing?  It must be something unique to your game/sprite setup that is causing the problem, right?

There are some things I neglected to mention last time so I will try to cover some more important details about the project and maybe taking these considerations into account will make things work better for you:

  • The function that saves the game must be "handled" in the Player_OnSpecialFunction Sub using the proper function name (the name of the function that actually saves the game).  In the sample, the name of the function was "DoSave".  The script must catch this and do its extra saving of player 2 data.
  • Similarly, it must handle the load function as well in order to re-load player 2 data and re-initialize player 2 after a load.
  • In order for these to work you must check the box for "Raise an event" on all your functions that save and load the game.
  • Yes, you will need a number of things on each map to ensure that the players get connected to their proper sprites whenever they arrive at the map:
    1. A default sprite that any player arriving at the map will use.
    2. A special function touching that sprite (P1) that switches to the player 1 sprite if inventory indicates that the player is player 1 (Item index number 2 in script, number 3 in the Player Settings dialog)
    3. Another special function touching that sprite (P2) that switches to player 2 if inventory indicates that the player is player 2 (Item index number 3 in script, number 4 in the Player Settings dialog)
    4. A player 1 sprite (don't check initial instance)
    5. A player 2 sprite (don't check initial instance)

GameDeveloper

  • Guest
Re: Crash When Loading 2P Game
« Reply #9 on: 2005-12-29, 10:23:06 AM »
I seem to have everything good and set as far as your bulleted list goes.  I created the functions and named them exactly 'DoSave' and 'LoadGame'.  All of that is working, i suppose it could be a problem with my sprites.  I'll go take a look and I'll mention if I see a problem, and thanks for helping me to get this thing to work.

GameDeveloper

  • Guest
Re: Crash When Loading 2P Game
« Reply #10 on: 2005-12-29, 11:38:01 AM »
Ok, I found a few problems, i believe.  First off, when the game is loaded, player two can't move, but the default sprite that has initial instance can... I believe he switches back to that default sprite?  Maybe, maybe not.  Also, could it be that I have several initial instance sprites on the map and the default happens to be at the bottom of that list?  I had that problem in the past.  I'm not sure if it's it though.

Everything, as far as special functions go, are completely, and I mean completely duplicated from the sample.  Agian, i don't know if it's the way they are set up, such as Save comes before load, that is screwing it up.  Originally, I had the save before the load, and it was doing very strange things... now that i switched them, it seems to be okay.

If this doesn't make sense, then just say, and I'll try to explain more clearly, as for now, I'll go try and squash the bugs in the game and see if I can get this to work.

EDIT: It worked!! I got it to work finally!  I figured that since I had another sprite as the very first initial instance on the map, I needed to switch that with the default sprite!  And it worked!  It was just like my last two player game.  Here's a tip for anyone making a two player game:  make the default first, then all the other sprites.  I'm sorry, I'm just so relieved that i can finally continue making the game.  Thanks for your help, but it's working now.  I'm pretty sure I should be okay from here on out.  If another problem is to arise, then I'll ask about it.  Agian, thanks!
« Last Edit: 2005-12-29, 11:52:48 AM by GameDeveloper »