Author Topic: Combining 2 scripts?  (Read 4334 times)

Verm

  • Guest
Combining 2 scripts?
« on: 2005-09-01, 11:07:48 AM »
I have to combine two different scripts and I am having an issue where the shooting script loads and the other does not. How should I re enter the values? Here are the scripts btw.

Script #1


Dim nExpectCount
Dim nShot0Count
Dim arBtn0Shots(4)
Dim nShot1Count
Dim arBtn1Shots(11)
Sub Player_OnControllerMove(OldActions, NewActions)
   If (Not OldActions) And NewActions And ACTION_BUTTON1 Then DoFireButton0
   If (Not OldActions) And NewActions And ACTION_BUTTON2 Then DoFireButton1
End Sub

Sub RecountShots()
   Dim nSIdx, nLIdx
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      nSIdx = 0
      Do While nSIdx < nShot0Count
         For nLIdx = 0 to .SpriteCount - 1
            If arBtn0Shots(nSIdx) Is .Sprite(nLIdx) Then
               Exit For
            End If
         Next
         If nLIdx >= .SpriteCount Then
            Set arBtn0Shots(nSIdx) = arBtn0Shots(nShot0Count - 1)
            nShot0Count = nShot0Count - 1
         Else
            nSIdx = nSIdx + 1
         End If
      Loop

      nSIdx = 0
      Do While nSIdx < nShot1Count
         For nLIdx = 0 to .SpriteCount - 1
            If arBtn1Shots(nSIdx) Is .Sprite(nLIdx) Then
               Exit For
            End If
         Next
         If nLIdx >= .SpriteCount Then
            Set arBtn1Shots(nSIdx) = arBtn1Shots(nShot1Count - 1)
            nShot1Count = nShot1Count - 1
         Else
            nSIdx = nSIdx + 1
         End If
      Loop

      nExpectCount = .SpriteCount
   End With
End Sub

Sub RemoveShot(Spr)
   Dim nIdx
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For nIdx = 0 to .SpriteCount - 1
         If .Sprite(nIdx) Is Spr Then .RemoveSprite(nIdx) : Exit Sub
      Next
   End With
End Sub

Function GetStateDeltas(DX, DY)
   With ProjectObj.GamePlayer.PlayerSprite
      Select Case .rDef.Template.StateType
      Case STATE_SINGLE
         If Abs(DX) + Abs(DY) > 1 Then
            DX = .DX / (Abs(.DX) + Abs(.DY))
            DY = .DY / (Abs(.DX) + Abs(.DY))
         Else
            If Abs(DX) + Abs(DY) < 1 Then DX = 0 : DY = -1
         End If
      Case STATE_LEFT_RIGHT
         DY = 0
         If (.CurState Mod 2) = 0 Then DX = -1 Else DX = 1
      Case STATE_8_DIRECTION
         Select Case (.CurState Mod 8)
         Case 0 : DX = 0 : DY = -1
         Case 1 : DX = 1 : DY = -1
         Case 2 : DX = 1 : DY = 0
         Case 3 : DX = 1 : DY = 1
         Case 4 : DX = 0 : DY = 1
         Case 5 : DX = -1 : DY = 1
         Case 6 : DX = -1 : DY = 0
         Case 7 : DX = -1 : DY = -1
         End Select
      Case Else
         DX = Cos((.CurState Mod 36) * 3.14159 / 18)
         DY = -Sin((.CurState Mod 36) * 3.14159 / 18)
      End Select
   End With
End Function

Sub Player_OnAfterMoveSprites
   Dim nIdx, VLeft, VTop, VRight, VBottom, VDat
   With ProjectObj.GamePlayer
      VLeft = .MapScrollX
      VTop = .MapScrollY
      VRight = VLeft + .rMap.ViewWidth
      VBottom = VTop + .rMap.ViewHeight
   End With

   If nExpectCount <> ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount Then RecountShots
   nIdx = 0
   Do While nIdx < nShot0Count
      With arBtn0Shots(nIdx)
         If .X < VLeft Or .X > VRight Or .Y < VTop Or .Y > VBottom Then
            RemoveShot arBtn0Shots(nIdx)
            Set arBtn0Shots(nIdx) = arBtn0Shots(nShot0Count - 1)
            nShot0Count = nShot0Count - 1
            nExpectCount = nExpectCount - 1
         Else
            nIdx = nIdx + 1
         End if
      End With
   Loop

   If nExpectCount <> ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount Then RecountShots
   nIdx = 0
   Do While nIdx < nShot1Count
      With arBtn1Shots(nIdx)
         If .X < VLeft Or .X > VRight Or .Y < VTop Or .Y > VBottom Then
            RemoveShot arBtn1Shots(nIdx)
            Set arBtn1Shots(nIdx) = arBtn1Shots(nShot1Count - 1)
            nShot1Count = nShot1Count - 1
            nExpectCount = nExpectCount - 1
         Else
            nIdx = nIdx + 1
         End if
      End With
   Loop

End Sub

Sub DoFireButton0()
   Dim NewSpr, DX, DY
   If nShot0Count >= 5 Then Exit Sub
   If ProjectObj.GamePlayer.InvQuantityOwned(1) < 1 Then Exit sub
   ProjectObj.GamePlayer.InvQuantityOwned(1) = ProjectObj.GamePlayer.InvQuantityOwned(1) - 1
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("Player_Mine").MakeInstance
   Set arBtn0Shots(nShot0Count) = NewSpr
   nShot0Count = nShot0Count + 1
   With ProjectObj.GamePlayer.PlayerSprite
      .rDef.rLayer.AddSprite HostObj.AsObject(NewSpr)
      nExpectCount = nExpectCount + 1
      NewSpr.X = .X + (.Width - NewSpr.Width) / 2
      NewSpr.Y = .Y + (.Height - NewSpr.Height) / 2
      ProjectObj.MediaMgr.Clip("Mine_Drop").Play
   End With
End Sub

Sub DoFireButton1()
   Dim NewSpr, DX, DY
   If nShot1Count >= 12 Then Exit Sub
   If ProjectObj.GamePlayer.InvQuantityOwned(2) < 1 Then Exit sub
   ProjectObj.GamePlayer.InvQuantityOwned(2) = ProjectObj.GamePlayer.InvQuantityOwned(2) - 1
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("P_Bullet").MakeInstance
   Set arBtn1Shots(nShot1Count) = NewSpr
   nShot1Count = nShot1Count + 1
   With ProjectObj.GamePlayer.PlayerSprite
      .rDef.rLayer.AddSprite HostObj.AsObject(NewSpr)
      nExpectCount = nExpectCount + 1
      NewSpr.X = .X + (.Width - NewSpr.Width) / 2
      NewSpr.Y = .Y + (.Height - NewSpr.Height) / 2
      ProjectObj.MediaMgr.Clip("Player_Boat_Shoot").Play
   End With
End Sub

Function RectToPolarState(X, Y)
   Dim Angle, Pi
   Pi = 3.14159
   If X <> 0 Then
       Angle = Atn(-Y / X)
   Else
       Angle = -(Pi / 2) * Sgn(Y)
   End If
   If X < 0 Then
       Angle = Pi + Angle
   ElseIf Y > 0 Then
       Angle = Pi * 2 + Angle
   End If
   RectToPolarState = ((Angle * 18) / Pi) Mod 36
End Function

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




Script #2

Option Explicit

Dim ScrollerSprite
ScrollerSprite = -1

Function FindScrollerSprite()
   Dim I
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For I = 0 To .SpriteCount - 1
         If Left(.Sprite(I).rDef.Name, 8) = "Scroller" Then
            FindScrollerSprite = I
            Exit Function
         End If
      Next
   End With
   FindScrollerSprite = -1
End Function

Sub Player_OnAfterMoveSprites()

   If ScrollerSprite < 0 Then
      ScrollerSprite = FindScrollerSprite
   End If

   If ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount <= ScrollerSprite Then
      ScrollerSprite = FindScrollerSprite
   End If

   If ScrollerSprite >= 0 Then
      If Left(ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.Sprite(ScrollerSprite).rDef.Name, 8) <> "Scroller" Then
         ScrollerSprite = FindScrollerSprite
      End If
   End If

   If ScrollerSprite >= 0 Then
      With ProjectObj.GamePlayer
         .MapScrollX = .PlayerSprite.rDef.rLayer.Sprite(ScrollerSprite).X
         .MapScrollY = .PlayerSprite.rDef.rLayer.Sprite(ScrollerSprite).Y
         If .PlayerSprite.Y > .MapScrollY + .rMap.ViewHeight - .ScrollMarginY - .PlayerSprite.Height Then
            .PlayerSprite.DY = -.PlayerSprite.rDef.Template.MoveSpeed
         ElseIf .PlayerSprite.Y < .MapScrollY + .ScrollMarginY Then
            .PlayerSprite.DY = .PlayerSprite.rDef.Template.MoveSpeed
         End If

         If .PlayerSprite.X > .MapScrollX + .rMap.ViewWidth - .ScrollMarginX - .PlayerSprite.Width Then
            .PlayerSprite.DX = -.PlayerSprite.rDef.Template.MoveSpeed
         ElseIf .PlayerSprite.X < .MapScrollX + .ScrollMarginX Then
            .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
         End If
      End With
   End If

End Sub

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

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Combining 2 scripts?
« Reply #1 on: 2005-09-01, 04:23:12 PM »
Try this -- hook the shooting script (#1) up to your project and copy the following code into it from the #2 script:

1. Copy this code and paste it below "Dim arBtn1Shots(11)"
Code: [Select]
Dim ScrollerSprite
ScrollerSprite = -1

2. Copy the whole FindScrollerSprite function (beginning with "Function FindScrollerSprite()" and ending with "End Function") and paste it below the code you just pasted above.
3. Copy the contents of the OnAfterMoveSprites handler (beginning with "If ScrollerSprite < 0 Then" and ending with the "End If" right above the "End Sub" -- don't include the End Sub) and paste it in the other OnAfterMoveSprites function, right after "Dim nIdx, VLeft, VTop, VRight, VBottom, VDat"

Verm

  • Guest
Combining 2 scripts?
« Reply #2 on: 2005-09-01, 08:25:24 PM »
This worked somewhat, but it created more problems after combining. Now the players Shooting sprites dissapear even before they are off the screen, as soon as the player shoots, the sprites disappear. Any suggestions as to how to fix this ?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Combining 2 scripts?
« Reply #3 on: 2005-09-02, 06:00:14 AM »
This might go faster if you were able to do some debugging of your own; I don't suppose you have any interest in learning a bit of script programming / debugging skills? :)  But I'll do what I can -- I might need your project if this doesn't work out.

First step is to find out what happens if you skip the script that removes the sprites from the screen in order to see if the script is responsible for removing the sprites or if it's the project itself removing the sprites.  In order to do this, add an "Exit Sub" after the code you pasted into Player_OnAfterMoveSprites (right before "If nExpectCount <> ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount").  If the problem remains, then the script is not responsible for the sprites disappearing, you need to change your sprite definitions in the project.  If the problem goes away, then some more script debugging will be required.  (Although you could probably just delete most of that script and turn on the correct options in the project to delete the sprites when they go off the screen instead.  I think that feature was added to GameDev after the scripting wizard was written, so it didn't used to be an option, but now it is.  Before I tell you exactly how to do that, though, I should find out what the results are.)

Anonymous

  • Guest
Combining 2 scripts?
« Reply #4 on: 2005-09-02, 11:32:41 AM »
I have added that code and it fixed the problem, but now I recieve an Overflow error, It says something about drawing text. What email address should I send this too?

Anonymous

  • Guest
Combining 2 scripts?
« Reply #5 on: 2005-09-02, 11:34:27 AM »
Also  I noticed its while im using my shooting styles this issue occurs, when it goes to affect my inventory.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Combining 2 scripts?
« Reply #6 on: 2005-09-02, 04:28:51 PM »
Just click on the email button at the bottom of any forum message of mine if you want to send me an email.

Wiph

  • Guest
I am having trouble as well
« Reply #7 on: 2005-09-09, 02:24:49 PM »
OK, i have followed the directions to combine the scripts but it's not working for me.  so if anyone can help that would be great.  here's the two that i need to combine for my game.

Dim nExpectCount
Dim nShot0Count
Dim arBtn0Shots(1)
Sub Player_OnControllerMove(OldActions, NewActions)
   If (Not OldActions) And NewActions And ACTION_BUTTON1 Then DoFireButton0
End Sub

Sub RecountShots()
   Dim nSIdx, nLIdx
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      nSIdx = 0
      Do While nSIdx < nShot0Count
         For nLIdx = 0 to .SpriteCount - 1
            If arBtn0Shots(nSIdx) Is .Sprite(nLIdx) Then
               Exit For
            End If
         Next
         If nLIdx >= .SpriteCount Then
            Set arBtn0Shots(nSIdx) = arBtn0Shots(nShot0Count - 1)
            nShot0Count = nShot0Count - 1
         Else
            nSIdx = nSIdx + 1
         End If
      Loop

      nExpectCount = .SpriteCount
   End With
End Sub

Sub RemoveShot(Spr)
   Dim nIdx
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For nIdx = 0 to .SpriteCount - 1
         If .Sprite(nIdx) Is Spr Then .RemoveSprite(nIdx) : Exit Sub
      Next
   End With
End Sub

Function GetStateDeltas(DX, DY)
   With ProjectObj.GamePlayer.PlayerSprite
      Select Case .rDef.Template.StateType
      Case STATE_SINGLE
         If Abs(DX) + Abs(DY) > 1 Then
            DX = .DX / (Abs(.DX) + Abs(.DY))
            DY = .DY / (Abs(.DX) + Abs(.DY))
         Else
            If Abs(DX) + Abs(DY) < 1 Then DX = 0 : DY = -1
         End If
      Case STATE_LEFT_RIGHT
         DY = 0
         If (.CurState Mod 2) = 0 Then DX = -1 Else DX = 1
      Case STATE_8_DIRECTION
         Select Case (.CurState Mod 8)
         Case 0 : DX = 0 : DY = -1
         Case 1 : DX = 1 : DY = -1
         Case 2 : DX = 1 : DY = 0
         Case 3 : DX = 1 : DY = 1
         Case 4 : DX = 0 : DY = 1
         Case 5 : DX = -1 : DY = 1
         Case 6 : DX = -1 : DY = 0
         Case 7 : DX = -1 : DY = -1
         End Select
      Case Else
         DX = Cos((.CurState Mod 36) * 3.14159 / 18)
         DY = -Sin((.CurState Mod 36) * 3.14159 / 18)
      End Select
   End With
End Function

Sub Player_OnAfterMoveSprites
   Dim nIdx, VLeft, VTop, VRight, VBottom, VDat
   With ProjectObj.GamePlayer
      VLeft = .MapScrollX
      VTop = .MapScrollY
      VRight = VLeft + .rMap.ViewWidth
      VBottom = VTop + .rMap.ViewHeight
   End With

   If nExpectCount <> ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount Then RecountShots
   nIdx = 0
   Do While nIdx < nShot0Count
      With arBtn0Shots(nIdx)
         If .X < VLeft Or .X > VRight Or .Y < VTop Or .Y > VBottom Then
            RemoveShot arBtn0Shots(nIdx)
            Set arBtn0Shots(nIdx) = arBtn0Shots(nShot0Count - 1)
            nShot0Count = nShot0Count - 1
            nExpectCount = nExpectCount - 1
         Else
            nIdx = nIdx + 1
         End if
      End With
   Loop

End Sub

Sub DoFireButton0()
   Dim NewSpr, DX, DY
   If nShot0Count >= 2 Then Exit Sub
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("Mine").MakeInstance
   Set arBtn0Shots(nShot0Count) = NewSpr
   nShot0Count = nShot0Count + 1
   With ProjectObj.GamePlayer.PlayerSprite
      .rDef.rLayer.AddSprite HostObj.AsObject(NewSpr)
      nExpectCount = nExpectCount + 1
      NewSpr.X = .X + (.Width - NewSpr.Width) / 2
      NewSpr.Y = .Y + (.Height - NewSpr.Height) / 2
      ProjectObj.MediaMgr.Clip("Mine Splash").Play
   End With
End Sub

Function RectToPolarState(X, Y)
   Dim Angle, Pi
   Pi = 3.14159
   If X <> 0 Then
       Angle = Atn(-Y / X)
   Else
       Angle = -(Pi / 2) * Sgn(Y)
   End If
   If X < 0 Then
       Angle = Pi + Angle
   ElseIf Y > 0 Then
       Angle = Pi * 2 + Angle
   End If
   RectToPolarState = ((Angle * 18) / Pi) Mod 36
End Function

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

The one above is a script made by the scripting wizard to fire a mine.

Here's the next one.

Dim ScrollerSprite

Function FindScrollerSprite()
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For I = 0 To .SpriteCount - 1
         If Left(.Sprite(I).rDef.Name, 8) = "Scroller" Then
            Set FindScrollerSprite = .Sprite(I)
            Exit Function
         End If
      Next
   End With
End Function

Sub Player_OnAfterMoveSprites()

   If IsEmpty(ScrollerSprite) Then
      Set ScrollerSprite = FindScrollerSprite
   End If

   If Not ScrollerSprite.rDef.rLayer.pMap Is ProjectObj.GamePlayer.rMap Then
      Set ScrollerSprite = FindScrollerSprite
   End If


   With ProjectObj.GamePlayer
      .MapScrollX = ScrollerSprite.X
      .MapScrollY = ScrollerSprite.Y
      If .PlayerSprite.Y > .MapScrollY + .rMap.ViewHeight - .ScrollMarginY - .PlayerSprite.Height Then
         .PlayerSprite.DY = -.PlayerSprite.rDef.Template.MoveSpeed
      ElseIf .PlayerSprite.Y < .MapScrollY + .ScrollMarginY Then
         .PlayerSprite.DY = .PlayerSprite.rDef.Template.MoveSpeed
      End If

      If .PlayerSprite.X > .MapScrollX + .rMap.ViewWidth - .ScrollMarginX - .PlayerSprite.Width Then
         .PlayerSprite.DX = -.PlayerSprite.rDef.Template.MoveSpeed
      ElseIf .PlayerSprite.X < .MapScrollX + .ScrollMarginX Then
         .PlayerSprite.DX = .PlayerSprite.rDef.Template.MoveSpeed
      End If
   End With

End Sub

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


This is an autoscroller script.  both work by themseleves but i can't get them to work together.  If anyone can help that would be great.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Combining 2 scripts?
« Reply #8 on: 2005-09-09, 04:52:58 PM »
I thought you were going to send me the project.