Author Topic: script "merging"  (Read 25979 times)

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« on: 2005-05-07, 07:04:24 PM »
Hey, I was wondering if anyone would be able to tell me how to put these 2 pieces of script together so they'd be able to work at the same time.
This first part is the 'forced scrolling' script (for my flying level) with a 'jump button' script form the script generator combined. This bit is being used with my game right now:
Code: [Select]
Option Explicit
Dim XOff, YOff

Sub Player_OnAfterMoveSprites()
   If ProjectObj.GamePlayer.rMap.name = "2-4BonusLevel" then

   XOff = XOff + 1
   With ProjectObj.GamePlayer
      if .PlayerSprite.X <= XOff then
         if .PlayerSprite.DX < 0 then .PlayerSprite.DX = 1
         .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
         if .PlayerSprite.X < XOff - .PlayerSprite.Width / 2 then
            XOff = 1
            .bQuit = false
         End if
      end if
      .rMap.Draw XOff, YOff
   End With
   End If
End Sub

Sub Player_OnControllerMove(OldActions, NewActions)
   With ProjectObj.GamePlayer.PlayerSprite
      If (Not OldActions) And NewActions And ACTION_BUTTON2 Then
         If (.rDef.SolidTest(.X, .Y + .Height) Or .rDef.SolidTest(.X + .Width - 1, .Y + .Height)) Or (Not .pRideOnRef Is Nothing) Then
            .DY = - .rDef.Template.JumpHeight
         End If
      End If
   End With
End Sub

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


This next part is the 'split screen' script, it's for the part of my game that's 2 player:
NOTE: I'm going to need this to be in effect for 3 or 4 separate 2 player maps, so I don't know if that can be put into one line like in the 'forced scroll' part, or if the whole 'split screen' part will need to be copied over each time per map.)
Code: [Select]
' ======== INITIAL STARTUP SCRIPT (Number 0) =========
Sub Player_OnPlayInit()
   HostObj.StartScript=1
End Sub
HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16


Option Explicit

Dim P2OffsetX, P2OffsetY
Dim P2Actions

Const nSprWidth = 32 ' Could be retrieved, but would be slower
Const nSprHeight = 32

Sub Display_KeyDown(KeyCode, Shift)
   ' E=69, S=83, D=68, F=70
   If KeyCode = 69 Then P2Actions = P2Actions Or ACTION_UP
   If KeyCode = 83 Then P2Actions = P2Actions Or ACTION_LEFT
   If KeyCode = 68 Then P2Actions = P2Actions Or ACTION_DOWN
   If KeyCode = 70 Then P2Actions = P2Actions Or ACTION_RIGHT
End Sub

Sub Display_KeyUp(KeyCode, Shift)
   If KeyCode = 69 Then P2Actions = P2Actions And Not ACTION_UP
   If KeyCode = 83 Then P2Actions = P2Actions And Not ACTION_LEFT
   If KeyCode = 68 Then P2Actions = P2Actions And Not ACTION_DOWN
   If KeyCode = 70 Then P2Actions = P2Actions And Not ACTION_RIGHT
End Sub

Sub Player_OnAfterMoveSprites()
   Dim oMap, oPlayer, oLayer, nLyrWid, nLyrHgt

   Set oPlayer = ProjectObj.GamePlayer
   Set oMap = oPlayer.rMap
   Set oLayer = oMap.MapLayer(0)

   With oLayer.Sprite(1)
.ProcessAction(P2Actions)

      oMap.ViewTop = 240
      If P2OffsetX + oPlayer.ScrollMarginX > .X Then
         P2OffsetX = .X - oPlayer.ScrollMarginX
      End If
      If P2OffsetX + oMap.ViewWidth - oPlayer.ScrollMarginX < .X + nSprWidth Then
         P2OffsetX = .X - oMap.ViewWidth + oPlayer.ScrollMarginX + nSprWidth
      End If
      If P2OffsetY + oPlayer.ScrollMarginY > .Y Then
         P2OffsetY = .Y - oPlayer.ScrollMarginY
      End If
      If P2OffsetY + oMap.ViewHeight - oPlayer.ScrollMarginY < .Y + nSprHeight Then
         P2OffsetY = .Y - oMap.ViewHeight + oPlayer.ScrollMarginY + nSprHeight
      End If
      If P2OffsetX < 0 Then P2OffsetX = 0
      If P2OffsetY < 0 Then P2OffsetY = 0
      nLyrWid = oLayer.Columns * nSprWidth
      nLyrHgt = oLayer.Rows * nSprHeight
      If P2OffsetX > nLyrWid - oMap.ViewWidth Then P2OffsetX = nLyrWid - oMap.ViewWidth
      If P2OffsetY > nLyrHgt - oMap.ViewHeight Then P2OffsetY = nLyrHgt - oMap.ViewHeight
      oMap.Draw P2OffsetX, P2OffsetY, False
      oMap.ViewTop = 0

   End With

End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.SinkObjectEvents CurrentDisplay, "Display"
HostObj.ConnectEventsNow()


My deepest thanks to who ever can figure this out :? .
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
script "merging"
« Reply #1 on: 2005-05-08, 07:28:57 AM »
Try this -- copying code from the top code into the bottom code:
[list=1]
  • Paste the line "Dim XOff, YOff" to the beginning of the "Runtime Script (Number 1)" block (like right after Option Explicit)
  • Copy the from code inside Sub Player_OnAfterMoveSprites (between the Sub line and the End Sub line) and paste it into the end of the other code's OnAfterMoveSprites sub (right before End Sub)
  • Copy the while Sub Player_OnControllerMove sub (including the Sub and End Sub) and paste it after the last End Sub in the bottom code.
  • [/list:o]
    As easy as 1, 2, 3... maybe :)

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« Reply #2 on: 2005-05-08, 09:38:00 AM »
ok, heres what i used (following you the best i could):
Code: [Select]
' ======== INITIAL STARTUP SCRIPT (Number 0) =========
Sub Player_OnPlayInit()
   HostObj.StartScript=1
End Sub
HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16


Option Explicit

Dim XOff, YOff

Dim P2OffsetX, P2OffsetY
Dim P2Actions

Const nSprWidth = 32 ' Could be retrieved, but would be slower
Const nSprHeight = 32

Sub Display_KeyDown(KeyCode, Shift)
   ' E=69, S=83, D=68, F=70
   If KeyCode = 69 Then P2Actions = P2Actions Or ACTION_UP
   If KeyCode = 83 Then P2Actions = P2Actions Or ACTION_LEFT
   If KeyCode = 68 Then P2Actions = P2Actions Or ACTION_DOWN
   If KeyCode = 70 Then P2Actions = P2Actions Or ACTION_RIGHT
End Sub

Sub Display_KeyUp(KeyCode, Shift)
   If KeyCode = 69 Then P2Actions = P2Actions And Not ACTION_UP
   If KeyCode = 83 Then P2Actions = P2Actions And Not ACTION_LEFT
   If KeyCode = 68 Then P2Actions = P2Actions And Not ACTION_DOWN
   If KeyCode = 70 Then P2Actions = P2Actions And Not ACTION_RIGHT
End Sub

Sub Player_OnAfterMoveSprites()
   Dim oMap, oPlayer, oLayer, nLyrWid, nLyrHgt

   Set oPlayer = ProjectObj.GamePlayer
   Set oMap = oPlayer.rMap
   Set oLayer = oMap.MapLayer(0)

   With oLayer.Sprite(1)
.ProcessAction(P2Actions)

      oMap.ViewTop = 240
      If P2OffsetX + oPlayer.ScrollMarginX > .X Then
         P2OffsetX = .X - oPlayer.ScrollMarginX
      End If
      If P2OffsetX + oMap.ViewWidth - oPlayer.ScrollMarginX < .X + nSprWidth Then
         P2OffsetX = .X - oMap.ViewWidth + oPlayer.ScrollMarginX + nSprWidth
      End If
      If P2OffsetY + oPlayer.ScrollMarginY > .Y Then
         P2OffsetY = .Y - oPlayer.ScrollMarginY
      End If
      If P2OffsetY + oMap.ViewHeight - oPlayer.ScrollMarginY < .Y + nSprHeight Then
         P2OffsetY = .Y - oMap.ViewHeight + oPlayer.ScrollMarginY + nSprHeight
      End If
      If P2OffsetX < 0 Then P2OffsetX = 0
      If P2OffsetY < 0 Then P2OffsetY = 0
      nLyrWid = oLayer.Columns * nSprWidth
      nLyrHgt = oLayer.Rows * nSprHeight
      If P2OffsetX > nLyrWid - oMap.ViewWidth Then P2OffsetX = nLyrWid - oMap.ViewWidth
      If P2OffsetY > nLyrHgt - oMap.ViewHeight Then P2OffsetY = nLyrHgt - oMap.ViewHeight
      oMap.Draw P2OffsetX, P2OffsetY, False
      oMap.ViewTop = 0

   End With

   If ProjectObj.GamePlayer.rMap.name = "2-4BonusLevel" then

   XOff = XOff + 1
   With ProjectObj.GamePlayer
      if .PlayerSprite.X <= XOff then
         if .PlayerSprite.DX < 0 then .PlayerSprite.DX = 1
         .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
         if .PlayerSprite.X < XOff - .PlayerSprite.Width / 2 then
            XOff = 1
            .bQuit = false
         End if
      end if
      .rMap.Draw XOff, YOff
   End With
   End If


End Sub

Sub Player_OnControllerMove(OldActions, NewActions)
   With ProjectObj.GamePlayer.PlayerSprite
      If (Not OldActions) And NewActions And ACTION_BUTTON2 Then
         If (.rDef.SolidTest(.X, .Y + .Height) Or .rDef.SolidTest(.X + .Width - 1, .Y + .Height)) Or (Not .pRideOnRef Is Nothing) Then
            .DY = - .rDef.Template.JumpHeight
         End If
      End If
   End With
End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.SinkObjectEvents CurrentDisplay, "Display"
HostObj.ConnectEventsNow()


and these are the error messages i got:

Code: [Select]
CreateTileSet requires an open display
[OK]

Error playing map: Object variable or With block variable not set
[OK]

Script stopped at line 33 on character 4: Subscript out of range
[OK]


I believe line 33 is Sub Player_OnAfterMoveSprites()

Also, since the split screen will only be for 4 maps, what code do I use/where do I put it?
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
script "merging"
« Reply #3 on: 2005-05-09, 05:31:59 AM »
Line 33 is the one that says "With oLayer.Sprite(1)".  Apparently you don't have a second player and it is having an error because it can't find it.  You either need to help it figure out when to look for a second player or put a second player on the map.

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« Reply #4 on: 2005-05-09, 05:37:27 AM »
ok, but what map is it looking for the 2nd player on? because the 2-4Bonus... one isn't going to be 2 player.
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
script "merging"
« Reply #5 on: 2005-05-09, 05:47:16 AM »
It applies to whatever map you're playing when you get the error.  Script always applies to all maps unless you put some check to only run it on certain maps (like you did when you checked for 2-4Bonus).

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« Reply #6 on: 2005-05-09, 01:01:35 PM »
ok, so i need to put the same check i used for the 2-4 part in the script for split screen, so it would just be the line
If ProjectObj.GamePlayer.rMap.name = "2-4BonusLevel" then
but how do i make it specify to check and see if it's one of 4 maps? im just going out on a limb here and guessing that it should be
If ProjectObj.GamePlayer.rMap.name = "map1" or "map2" or "map3" or "map4" then
or something like that, and if that IS right, where would it go
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
script "merging"
« Reply #7 on: 2005-05-09, 04:17:54 PM »
Close.  You need to check the entire condition every time.

Code: [Select]
If ProjectObj.GamePlayer.rMap.name = "map1" or ProjectObj.GamePlayer.rMap.name = "map2" or ProjectObj.GamePlayer.rMap.name = "map3" or ProjectObj.GamePlayer.rMap.name = "map4" then

Something else that might be easier:

Code: [Select]
tmp = ProjectObj.GamePlayer.rMap.name
If tmp = "map1" or tmp = "map2" or tmp = "map3" or tmp = "map4" then


Or, if your maps are infact named map1, map2, map3, and map4, then you could use

Code: [Select]
mapNum = right(ProjectObj.GamePlayer.rMap.name,1)
If mapnum >= 1 and mapNum <= 4 then
Edward Dassmesser

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« Reply #8 on: 2005-05-09, 04:26:22 PM »
Hah, no, I just used that as a general name, I havent actually made (let alone named) those levels yet, so I'll just stick with the first one. Now, where to put them...

Quote
Line 33 is the one that says "With oLayer.Sprite(1)". Apparently you don't have a second player and it is having an error because it can't find it. You either need to help it figure out when to look for a second player or put a second player on the map.


BTW, where does it say the name used for the player2 sprite in the script? I see P2 where the keys are defined, so I'm guessing that's it.
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
script "merging"
« Reply #9 on: 2005-05-09, 08:30:36 PM »
It doesn't go by the name, it goes by the zero-based-index.  It assumes that the second sprite created is the second player sprite (the first being the first player, but this is not neccessary).  Hence, the "oLayer.Sprite(1)" takes the 2nd sprite created on that layer.  (oLayer.Sprite(0) would be the first).  You can change the number to be the one you want, or you could do a loop to find the sprite you want by checking it's name, then use that integer.  Though once you find it, its index shouldn't change unless you delete some sprites.

It should look something like this.

Code: [Select]

intSprTwo = 0
For i = 0 to ubound(oLayer.Sprite)
   If oLayer.Sprite(i).rDef.Name = "NAME OF SPRITE" then intSprTwo = i
next


Then use "with oLayer.Sprite(intSprTwo)".  The only thing I'm not sure of is ubound().  I'm not sure that would work.  Be sure to not execute this piece of script in the play loop every frame, because it's very unneccessary and would create a considerable drag on performance.
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
script "merging"
« Reply #10 on: 2005-05-10, 05:36:43 AM »
Instead of "ubound(oLayer.Sprite)" use "oLayer.SpriteCount - 1"

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« Reply #11 on: 2005-05-10, 05:58:57 AM »
k, i'll put this in and test it when i get home
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
script "merging"
« Reply #12 on: 2005-05-10, 09:23:48 AM »
Ah, I was looking for that.  So, it's a method instead of a property, eh?
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
script "merging"
« Reply #13 on: 2005-05-11, 06:15:25 PM »
Quote from: "durnurd"
So, it's a method instead of a property, eh?


Yeah, that took me by surprise too.  Don't know why I did that that way.

billybob884

  • Contributor
  • Fanatic
  • **
  • Posts: 355
    • AOL Instant Messenger - billybob884
    • View Profile
script "merging"
« Reply #14 on: 2005-05-15, 12:15:33 PM »
ok, heres the script ive managed to put together with the bits an peices youve told me to add:
Quote
' ======== INITIAL STARTUP SCRIPT (Number 0) =========
If ProjectObj.GamePlayer.rMap.name = "0-4LevelSelect" then 'will be If ProjectObj.GamePlayer.rMap.name = "map1" or ProjectObj.GamePlayer.rMap.name = "map2" or ProjectObj.GamePlayer.rMap.name = "map3" or ProjectObj.GamePlayer.rMap.name = "map4" then
Sub Player_OnPlayInit()
   HostObj.StartScript=1
End Sub
HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16


Option Explicit

Dim XOff, YOff

Dim P2OffsetX, P2OffsetY
Dim P2Actions

Const nSprWidth = 48 ' Could be retrieved, but would be slower
Const nSprHeight = 89

Sub Display_KeyDown(KeyCode, Shift)
   ' E=69, S=83, D=68, F=70
   If KeyCode = 69 Then P2Actions = P2Actions Or ACTION_UP
   If KeyCode = 83 Then P2Actions = P2Actions Or ACTION_LEFT
   If KeyCode = 68 Then P2Actions = P2Actions Or ACTION_DOWN
   If KeyCode = 70 Then P2Actions = P2Actions Or ACTION_RIGHT
End Sub

Sub Display_KeyUp(KeyCode, Shift)
   If KeyCode = 69 Then P2Actions = P2Actions And Not ACTION_UP
   If KeyCode = 83 Then P2Actions = P2Actions And Not ACTION_LEFT
   If KeyCode = 68 Then P2Actions = P2Actions And Not ACTION_DOWN
   If KeyCode = 70 Then P2Actions = P2Actions And Not ACTION_RIGHT
End Sub

Sub Player_OnAfterMoveSprites()
   Dim oMap, oPlayer, oLayer, nLyrWid, nLyrHgt

   Set oPlayer = ProjectObj.GamePlayer
   Set oMap = oPlayer.rMap
   Set oLayer = oMap.MapLayer(0)

  intSprTwo = 0
For i = 0 to oLayer.SpriteCount - 1
   If oLayer.Sprite(i).rDef.Name = "Player2" then intSprTwo = i
next

.ProcessAction(P2Actions)

      oMap.ViewTop = 240
      If P2OffsetX + oPlayer.ScrollMarginX > .X Then
         P2OffsetX = .X - oPlayer.ScrollMarginX
      End If
      If P2OffsetX + oMap.ViewWidth - oPlayer.ScrollMarginX < .X + nSprWidth Then
         P2OffsetX = .X - oMap.ViewWidth + oPlayer.ScrollMarginX + nSprWidth
      End If
      If P2OffsetY + oPlayer.ScrollMarginY > .Y Then
         P2OffsetY = .Y - oPlayer.ScrollMarginY
      End If
      If P2OffsetY + oMap.ViewHeight - oPlayer.ScrollMarginY < .Y + nSprHeight Then
         P2OffsetY = .Y - oMap.ViewHeight + oPlayer.ScrollMarginY + nSprHeight
      End If
      If P2OffsetX < 0 Then P2OffsetX = 0
      If P2OffsetY < 0 Then P2OffsetY = 0
      nLyrWid = oLayer.Columns * nSprWidth
      nLyrHgt = oLayer.Rows * nSprHeight
      If P2OffsetX > nLyrWid - oMap.ViewWidth Then P2OffsetX = nLyrWid - oMap.ViewWidth
      If P2OffsetY > nLyrHgt - oMap.ViewHeight Then P2OffsetY = nLyrHgt - oMap.ViewHeight
      oMap.Draw P2OffsetX, P2OffsetY, False
      oMap.ViewTop = 0

   End With

   If ProjectObj.GamePlayer.rMap.name = "2-4BonusLevel" then

   XOff = XOff + 1
   With ProjectObj.GamePlayer
      if .PlayerSprite.X <= XOff then
         if .PlayerSprite.DX < 0 then .PlayerSprite.DX = 1
         .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
         if .PlayerSprite.X < XOff - .PlayerSprite.Width / 2 then
            XOff = 1
            .bQuit = false
         End if
      end if
      .rMap.Draw XOff, YOff
   End With
   End If


End Sub

Sub Player_OnControllerMove(OldActions, NewActions)
   With ProjectObj.GamePlayer.PlayerSprite
      If (Not OldActions) And NewActions And ACTION_BUTTON2 Then
         If (.rDef.SolidTest(.X, .Y + .Height) Or .rDef.SolidTest(.X + .Width - 1, .Y + .Height)) Or (Not .pRideOnRef Is Nothing) Then
            .DY = - .rDef.Template.JumpHeight
         End If
      End If
   End With
End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.SinkObjectEvents CurrentDisplay, "Display"
HostObj.ConnectEventsNow()


the bold bits are the parts that differ from the last script i posted. I'm pretty sure it's wrong, because i didn't really know where each bit went.. im going to try it in a minute and see what happens, as soon as i put in a second player and define a map for the 2 player.

also, the 2nd part thats in bold/underlined replaced
Quote
With oLayer.Sprite(1)



-----added-later-----
Quote
Script Stopped at line 9 on character 1:
Expected 'End'
"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." :: Hitchhiker's Guide to the Galaxy