Author Topic: glich with Sub Display_KeyDown  (Read 4089 times)

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
glich with Sub Display_KeyDown
« on: 2006-09-28, 10:33:36 PM »
This always seemd like such a simple piece of script , but now it seems to be causeing a glich.
It is not an error , the game functions properly untill either keypress function is activated , It somehow activates another function.
 

Code: [Select]
Sub Display_Keydown(KeyCode, Shift)
   With ProjectObj.GamePlayer   
   If keycode = 83 Then
   .ActivateFunction .rMap.Specials("selecta")
   Elseif keycode = 68 Then
   .ActivateFunction .rMap.Specials("selectb")
   End If
   End With
End Sub
The code 83 = s and 68 = d , what could cause this code to trigger another function , and how can i fix / prevent it? ???

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: glich with Sub Display_KeyDown
« Reply #1 on: 2006-09-29, 06:39:36 AM »
I'm ... 87% certain that the problem is not with that script or the KeyDown event.  I suspect that something odd is happening as a result of the function being activated or something happening somewhere else in the project.  In order to confirm this, I suggest you try one of two things:
1. Try the same script in a different, much simpler project that simply displays a message for the selecta and selectb functions (and those functions would be added with the "Add" button instead of being added in the map editor).
2. Try activating your selecta and selectb functions in a different way (instead of from script) and see if you get similar odd behavior.  I suspect one of them is somehow indirectly activating the other.

If you can reproduce the same problem in a simple project, send it to me and I can investigate it.

Maybe you did something as simple as define those functions to be triggered by Buttons mapped to the S and D keys (but swapped), and then forgot to remove that when you started triggering them in script?

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
Re: glich with Sub Display_KeyDown
« Reply #2 on: 2006-09-29, 10:50:51 AM »
Ok here is the other function , It changes between two playersprites.
What happens when I use the two selection keys is , theplayer is teleported back to start position.
This wouldnt seem so bad if I could retain new position of player , but if I was on a map where player
is a cursorsprite this would cause an error ; playersprite not found.
I would try the print text thing but this code fires first to check which sprite was selected.
Still I cant understand why these two pieces of code should be reacting this way.
 :-\
   
Code: [Select]
Sub Player_OnSpecialFunction(AdPlayer)
   Dim NewSpr
   Dim Idx
   Dim MyState, SprCount
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   SprCount = .SpriteCount
   Idx = 0
   If .Sprite(Idx) Is ProjectObj.GamePlayer.PlayerSprite Then
   oLayer.RemoveSprite(Idx)
   End If
   Idx = Idx + 1
   If ProjectObj.GamePlayer.InvQuantityOwned(60) = 0 Then
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("player").MakeInstance
   Elseif ProjectObj.GamePlayer.InvQuantityOwned(60) = 1 Then
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("playera").MakeInstance
   End If
   .AddSprite HostObj.AsObject(NewSpr)
   Set ProjectObj.GamePlayer.PlayerSprite = NewSpr
   ProjectObj.GamePlayer.PlayerSprite.CurState = MyState
   End With
End Sub

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: glich with Sub Display_KeyDown
« Reply #3 on: 2006-09-29, 11:13:08 AM »
Are you saying that when you press S or D the above code runs and you don't want it to at all?

The above code would activate if the "Raise Event" box is checked in the special functions "selecta" and "selectb".  Try unchecking those boxes if they're checked to remove this problem.  If they aren't checked, and they aren't activating a list of other special functions that are raising an event, then it may be that a function activated from script automatically raises an event.  In that case, just check that the name of the special function is not "selecta" or "selectb" before executing the code. (Or check to make sure the name of the special function is the function you want to be using)
Edward Dassmesser

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
Re: glich with Sub Display_KeyDown
« Reply #4 on: 2006-09-29, 12:45:58 PM »
 I unchecked Raise an event for the select functions and fixed the glich!;D
That was to simple , So why does that work?
Is it special for the keyDown feature to activate all functions that raise an event? ???

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: glich with Sub Display_KeyDown
« Reply #5 on: 2006-09-29, 01:11:16 PM »
OnKeyDown had nothing to do with it.

The OnSpecialFunction function in your script will run whenever any special function is activated that has "Raise Event" checked.  So it was running that script when selecta and selectb were being activated.  This is why you should check the name of the function being activated before running the OnSpecialFunction script.  This becomes absolutely necessary if you want to have different special functions that do different things in the script.
Edward Dassmesser

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
Re: glich with Sub Display_KeyDown
« Reply #6 on: 2006-09-29, 06:53:17 PM »
Ok , I originaly thought that was the reason you needed to raise an event. :o
I still have a problem with the code that selects the playersprite.
It seems to work fine except if the charecter switches maps it always appears at the default location.
for example If I moved the player after startup and then reactivate the  adplayer function , the player seems to teleport back to default.
I realise that retaining state is unimpotant , somehow I must retain the predetermind start location from the previous map function. :D

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: glich with Sub Display_KeyDown
« Reply #7 on: 2006-09-30, 05:43:59 AM »
I don't see any code that is searching for the index of the player sprite.  It looks like you are always assuming it is #0.  What are the purposes of the Idx and SprCount variables?  They don't really seem to do much of consequence.  I would have expected to see a For loop around that "If" checking each sprite index (Idx) in the layer up to SprCount.

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
Re: glich with Sub Display_KeyDown
« Reply #8 on: 2006-09-30, 12:41:25 PM »
I only need to delete the player sprite , this is global inv item sets off.
So this should be enough code for that.
Ill test this later.

Code: [Select]
Sub Player_OnSpecialFunction(AdPlayer)
   Dim NewSpr
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   If .Sprite Is ProjectObj.GamePlayer.PlayerSprite Then
   oLayer.RemoveSprite
   End If
   If ProjectObj.GamePlayer.InvQuantityOwned(60) = 0 Then
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("player").MakeInstance
   Elseif ProjectObj.GamePlayer.InvQuantityOwned(60) = 1 Then
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("playera").MakeInstance
   End If
   .AddSprite HostObj.AsObject(NewSpr)
   Set ProjectObj.GamePlayer.PlayerSprite = NewSpr
   End With
End Sub

Then checks inv item #60 for which sprite to make.
This function is set off every time I switch maps so the sprite remains the same.
So far it seems to work but If I leave the shop to return to the main world instead of being outside the shop Im back at the begining.
Whats missing?

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
Re: glich with Sub Display_KeyDown
« Reply #9 on: 2006-09-30, 01:06:53 PM »
Ok i tested the code and it seems the indx variable is needed to delete the sprite.
Maybee I misunderstood you , I guess I am indexing 0. 8)

basementbiker

  • Visitor
  • *
  • Posts: 14
    • View Profile
    • Email
Re: glich with Sub Display_KeyDown
« Reply #10 on: 2006-09-30, 04:02:54 PM »
Hey! ;D
I figured it out.
Code: [Select]
Sub Player_OnSpecialFunction(AddPlayer)
   Dim NewSpr
   Dim Idx
   Dim MyState, SprCount
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   Xcoord = ProjectObj.GamePlayer.PlayerSprite.X 
   Ycoord = ProjectObj.GamePlayer.PlayerSprite.Y
   MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   SprCount = .SpriteCount
   Idx = 0
   If .Sprite(Idx) Is ProjectObj.GamePlayer.PlayerSprite Then
   oLayer.RemoveSprite(Idx)
   End If
   Idx = Idx + 1
   If ProjectObj.GamePlayer.InvQuantityOwned(60) = 0 Then
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("player").MakeInstance
   Elseif ProjectObj.GamePlayer.InvQuantityOwned(60) = 1 Then
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("playera").MakeInstance
   End If
   .AddSprite HostObj.AsObject(NewSpr)
   Set ProjectObj.GamePlayer.PlayerSprite = NewSpr
   NewSpr.X = Xcoord
   NewSpr.Y = Ycoord
   ProjectObj.GamePlayer.PlayerSprite.CurState = MyState
   End With
End Sub
This works totaly , now where ever the playersprite with initial instance appears is where the players chosen sprite is placed.
 :-*

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: glich with Sub Display_KeyDown
« Reply #11 on: 2006-10-01, 09:03:18 PM »
I think, if you want to be sure you're getting the player sprite, no matter which sprite it is on the map, you should change this:
Code: [Select]
   Dim Idx
   Dim MyState, SprCount
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   Xcoord = ProjectObj.GamePlayer.PlayerSprite.X 
   Ycoord = ProjectObj.GamePlayer.PlayerSprite.Y
   MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   SprCount = .SpriteCount
   Idx = 0
   If .Sprite(Idx) Is ProjectObj.GamePlayer.PlayerSprite Then
   oLayer.RemoveSprite(Idx)
   End If
   Idx = Idx + 1

to this:

Code: [Select]
   Dim Idx
   Dim MyState, SprCount
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   Xcoord = ProjectObj.GamePlayer.PlayerSprite.X 
   Ycoord = ProjectObj.GamePlayer.PlayerSprite.Y
   MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   SprCount = .SpriteCount - 1
   for Idx = 0 to SprCount
      If .Sprite(Idx) Is ProjectObj.GamePlayer.PlayerSprite Then
         oLayer.RemoveSprite(Idx)
         exit for
      end if
   next

Otherwise, if you only want to remove it if it's zero, just change it to:

Code: [Select]
   Dim MyState
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   Xcoord = ProjectObj.GamePlayer.PlayerSprite.X 
   Ycoord = ProjectObj.GamePlayer.PlayerSprite.Y
   MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   If .Sprite(0) Is ProjectObj.GamePlayer.PlayerSprite Then
      oLayer.RemoveSprite(0)
   End If

Or if you always want to delete it, and are sure that it is always zero:

Code: [Select]
   Dim MyState
   Dim oLayer
   Set oLayer = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   Xcoord = ProjectObj.GamePlayer.PlayerSprite.X 
   Ycoord = ProjectObj.GamePlayer.PlayerSprite.Y
   MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
   oLayer.RemoveSprite(0)

Also, you may want to stick something in at the beginning to make sure it only activates when activating the right special function:

Code: [Select]
Sub Player_OnSpecialFunction(AddPlayer)
   if AddPlayer.Name <> "SpecialFunctionName" then exit sub

or if you have different actions for different functions:

Code: [Select]
Sub Player_OnSpecialFunction(AddPlayer)
   Select Case AddPlayer.Name
      Case "SpecialOne"
         'DoStuff
      Case "SpecialTwo"
         'DoOtherStuff
      Case "SpecialThree"
         'DoEvenMoreStuff
   End Select
End Sub
« Last Edit: 2006-10-01, 09:10:23 PM by durnurd »
Edward Dassmesser