Author Topic: A few scripting questions  (Read 4739 times)

Dr Obvious

  • Regular
  • **
  • Posts: 53
    • View Profile
    • http://www.drobvious.net
A few scripting questions
« on: 2005-08-25, 04:57:25 PM »
Hello, I'm trying to make a game for SGDK but I'm having a few troubles figuring a few things out.  I'm hoping someone can help me with a few scripting problems...

1) Script for collision
I know this is probably something simple, but I can't find how to do collision with scripting.  I want to do my shooting all in scripting because of the way I'm doing damage.

2) Random Damage on collision (for enemy and player)
I want whenever an emeny or player shoots a bullet collides with the other to do some amount of damage plus a random number so that it looks something like:
'On collision
Health = 20 - diff*(rand(-5,5))

Where health is the enemy or players health, diff is the difficulty rating (0.7, 1.0, 1.3) and it adds some random number between -5 and 5.  Is doing something like this possible?

3) Player sprite switching
I tried this earlier so that when the player shoots, the script changes the player sprite for a short amount of time and then back but I couldn't really get it to work.  How do you make a script that changes the player sprite for x time and then back to the original while creating a bullet sprite that moves away from the player in a straight line?


I've tried making my shooting with special functions, but I'm just having a weird problem I can't really figure out and neither can anyone else so I want to try it this way with scripting instead.  Whenever I shoot using the special function, the sprite changes and makes a bullet where I want it to go, just the bullet doesn't really go anywhere.  It has speed, no gravity, follows path relative to player, and path vector, it just sits there for some odd reason.

Thanks for any help you can give!

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
A few scripting questions
« Reply #1 on: 2005-08-26, 07:32:32 AM »
1) If you add a sub like this to your script, it will get called whenever a collision test (defined in your project) detects that two sprites have collided.  Does that help?
Code: [Select]
Sub Player_OnSpritesCollide(Name, ClsASprIdx, ClsBSprIdx, CollDefIdx)
End Sub

2) Yes, to calculate a random number between 15 and 25, use an expression like this
Code: [Select]
Damage = 15 + Int(Rnd(1) * 11).  You could probably store a sprite's total damage / hit points in the sprite object's UserData property.
3) What did the path that the bullet sprite was associated with look like?  Did it have a second point offset from the first point (no delays)?  What did the path for your temporary player sprite look like?
To temporarily switch the player sprite, could you use the switch to sprite function to switch the player to a sprite that follows a path relative to the player sprite, and the path is just one point followed by a delay.  Then tell that sprite to activate a function at the end of the path which will switch the player back to his normal form?

Dr Obvious

  • Regular
  • **
  • Posts: 53
    • View Profile
    • http://www.drobvious.net
A few scripting questions
« Reply #2 on: 2005-08-26, 03:59:15 PM »
Thanks for all the help, those really help a lot, but one more quick question (for now): how do I access the player data?

Also, I'm making some headway on that shooting problem I had (and it was just some stupid little thing I had missed).  I forgot to turn my inertia all the way up so that seemed to be why they didn't go anywhere.  Now I just have to figure out why my left and right shot isn't working. :-p

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
A few scripting questions
« Reply #3 on: 2005-08-26, 05:37:57 PM »
To access player data, use ProjectObj.GamePlayer.  ProjectObj is all data in the project and GamePlayer is the property of the project that returns just the player data.  It returns an object of type Player, which is documented in the scripting reference.

Dr Obvious

  • Regular
  • **
  • Posts: 53
    • View Profile
    • http://www.drobvious.net
A few scripting questions
« Reply #4 on: 2005-08-30, 08:23:27 PM »
Alrighty, I have two more scripting questions:

1. How do you change levels in a script?

2. How do I say when I press a button (like "1") the script does some funcrion, like level change.  I don't want this to be one of the 4 action buttons, I just want to be able to define about 5 or 6 of these.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
A few scripting questions
« Reply #5 on: 2005-08-30, 10:12:22 PM »
For changing level, I either have a special function on a map that goes to another arbitrary map or create one, then change it in script to go to the map I want.  To add a new one to the map during runtime, it's something like this:

Code: [Select]
Set oNewSpecial = NewSpecialFunction
  oNewSpecial.DestX = 0
  oNewSpecial.DestY = 0
  oNewSpecial.Flags = 0 'You'll have to fiddle with these to find out what's good
  oNewSpecial.Flags2 = 0 'These too
  oNewSpecial.FuncType = SPECIAL_SWITCHMAP
  oNewSpecial.InvItem = 0
  oNewSpecial.InvUseCount = 0
  oNewSpecial.LayerIndex = LayerNum
  oNewSpecial.MediaName = ""
  oNewSpecial.Name = "MoveToNewMap"
  oNewSpecial.SpriteName = "SpriteName" 'Sprite you want to show up as (I think)
  oNewSpecial.TileBottom = 0
  oNewSpecial.TileLeft = 0
  oNewSpecial.TileRight = 0
  oNewSpecial.TileTop = 0
  oNewSpecial.Value = "Map" 'Name of map to go to
oMap.AddSpecial(hostObj.AsObject(oNewSpecial))


Otherwise, if you just want to have a special function on the map (or on a template map and use code to copy it to each map during runtime) and just change the map it goes to:

Code: [Select]

ProjectObj.GamePlayer.rMap.Specials("NextLevel").Value = "Map" 'Name of next level
ProjectObj.GamePlayer.ActivateFunction(ProjectObj.GamePlayer.rMap.Specials("NextLevel")


And now that I look at it, it may be possible to only use one special function, because the ActivateFunction method is part of the player, not the map.  Though it may only look within the current map's special functions, so be on the safe side.

As for trapping keypresses, it's something along the lines of:

Code: [Select]
Sub Display_KeyPress(KeyAscii)
   Select Case ucase(chr(KeyAscii))
      Case "1"
         ProjectObj.GamePlayer.ActivateFunction(F1) 'Activate Special Function predefined as F1
      Case "2"
         ProjectObj.GamePlayer.ActivateFunction(ProjectObj.GamePlayer.rMap.Specials("NextLevel"))
      Case "F"
         'You Get the idea
   End Select
End Sub
'And you'll need this down at the bottom with the other SinkObjectEvents
HostObj.SinkObjectEvents CurrentDisplay, "Display"
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
A few scripting questions
« Reply #6 on: 2005-08-31, 06:04:26 AM »
There's a little bit more to handling arbitrary keypresses since you have to do a little extra work to receive events from the Display object (which is where the keypress events come from).  There's a document describing how to handle mouse and keyboard events here, though:
http://sourceforge.net/docman/display_doc.php?docid=7777&group_id=9970

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
A few scripting questions
« Reply #7 on: 2005-08-31, 10:00:46 AM »
I took the code mostly from the Text Input sample code.  If you want to trap keys like Shift and Alt and the Arrow keys arbitrarily (without having them set as controls) then yes, you will need to use Display_KeyDown, but otherwise Display_KeyPress should work.
Edward Dassmesser