[REQ/WIP] ModFood: Snippets & Tips

Post » Wed Dec 07, 2011 4:07 am

I'm not sure if any of these are what you're looking for, but:


Objective: Debug Messages for Mod Testing (With OBSE and Without OBSE)
The purpose of this is to find out variables stated in the script, so you know it is running and to see what the outcome is. In the example script, I can figure out what the variables equal easily, but if you replace Variable01 with something that can be dynamic in-game, than this becomes helpful to know the outcome of the script variables. This can also be used to see if the script is even running. [Note: The two version are no different, except one message to the console and the other messages in-game.]

Options:

A. Without OBSE

B. With OBSE

Steps

Without OBSE:
A. 1. Edit a script:
Click on the pencil button, click on the folder button, and choose the script you want to edit.
Copy/type this code in the editor.

Scriptname DebugExampleScriptWithoutOBSEShort Variable01Short Variable02Short Variable03Begin GameModeSet Variable01 to 20Set Variable02 to (Variable01 * 2)Set Variable03 to ((Variable02 * 5) + Variable01)Message "Variable 1 is %.2f.  Variable 2 is %.2f.  Variable 3 is %.2f.", Variable01, Variable02, Variable03End

A. 2. Implement the edited script so it will run during the game.
Close the window and save the mod.


With OBSE:
B. 1. Edit a script:
Click on the pencil button, click on the folder button, and choose the script you want to edit.
Copy/type this code in the editor.

Scriptname DebugExampleScriptWithOBSEShort Variable01Short Variable02Short Variable03Begin GameModeSet Variable01 to 20Set Variable02 to (Variable01 * 2)Set Variable03 to ((Variable02 * 5) + Variable01)PrintToConsole "Variable 1 is %.2f.  Variable 2 is %.2f.  Variable 3 is %.2f.", Variable01, Variable02, Variable03End

B. 2. Implement the edited script so it will run during the game.
Close the window and save the mod.


With either version next time you play, you'll get a message (in the console or on screen) telling you the variables you specified in the script.
User avatar
Matt Terry
 
Posts: 3453
Joined: Sun May 13, 2007 10:58 am

Post » Wed Dec 07, 2011 6:38 am

Objective: Avoiding Message Spam (With OBSE)

To avoid message spam with OBSE, you can use these functions:

AddItemNSRemoveItemNSEquipItemNSUnequipItemNSAddSpellNSRemoveSpellNS

All the no spam functions have the same syntax as their opposite message producing functions.

Here's an example script:

Scriptname NoMessageSpamExampleScriptBegin GamemodePlayer.AddItemNS DwarvenGauntlets, 1Player.RemoveItemNS DwarvenGauntlets, 1Player.EquipItemNS DwarvenGauntletsPlayer.UnequipItemNS DwarvenGauntletsPlayer.AddSpellNS StandardShield1NovicePlayer.RemoveSpellNS StandardShield1NoviceEnd


Next time you play the game, you won't realize an item/spell has been added/removed or equipped/unequipped.
User avatar
Alessandra Botham
 
Posts: 3440
Joined: Mon Nov 13, 2006 6:27 pm

Post » Wed Dec 07, 2011 9:47 am

Great! Thanks con-tur-eh! Now we have to wait Felic or moderator and ask them to put it in the first post.
User avatar
SEXY QUEEN
 
Posts: 3417
Joined: Mon Aug 13, 2007 7:54 pm

Post » Wed Dec 07, 2011 8:45 am

Good to know that this topic is available. Many of these subjects are asked over and over.
So, here is my contribution (from http://cs.elderscrolls.com/constwiki/index.php/User:QQuix)

Objective: Working with distances and angles

1. Three angle systems
The game uses three angle 'systems':

1a. World Angle
Range ? 0-360 - 0 degrees is up, increasing clockwise up to 360. (e.g: 90 = Right, 180 = Down, 270 = Left).
This is the most common. It is the angle you find in the CS for Reference Rotation.
This type of angle is returned by: GetAngle Z, GetStartingAngle
This type of angle must be provided for: SetAngle Z

1b Heading Angle
Range ? -180 to +180 - 0 degrees is up, increasing clockwise up to 180 and decreasing counterclockwise down to -180. (e.g: 90 = Right, 180 = Down, -90 = Left, -180 = Down).
This is used to tell how a second object is positioned in relation to a first object heading
This type of angle is returned by: GetHeadingAngle

1c.Trigonometric Angle

Range ? 0 to 360 - 0 degrees is right, increasing counterclockwise up to 360. (e.g: 90 = Up, 180 = Left, 270 = Down). This is the standard trigonometric angle system.
This type of angle must be provided for OBSE trigonometric functions: Sin, Cos, Tan, etc

2.Sample code
This code uses OBSE trig functions. Non-OBSE trig code can be found in the http://cs.elderscrolls.com/constwiki/index.php/ESM_Math_Library.

2a. DeltaX & DeltaY from Angle and distance
Useful when positioning an object at a given distance and angle from another object

;=================================; Get:  DeltaX and DeltaY; From:	Angle and distance;=================================set xAngle to xxxset xDistance to xxxif  xAngle <= 90	set xxTrigAngle  to 90 - xAngleelse	set xxTrigAngle to 450 - xAngleendif set xDeltaX to xDistance * cos xxTrigAngle set xDeltaY to xDistance * sin xxTrigAngle


2b. Angle and distance from DeltaX & DeltaY
Useful to calculate the horizontal (2D) distance between two objects or in situations where GetDistance is not reliable.
Useful to calculate the heading angle in situations where GetHeadingAngle is not reliable (non-actors).

;=================================; Get:  Angle and distance; From: DeltaX and DeltaY;=================================set xAngle to atan2 xDeltaX xDeltaY   ;	returns -180 to +180if xAngle  < 0  set xAngle to xAngle + 360   	;	convert to 0-360endifset xDistance to ( xDeltaX * xDeltaX ) + ( xDeltaY * xDeltaY )set xDistance to sqrt xDistance


2c. Adding two angles
Self explanatory

;=================================; Get:  Angle ; From: Adding two angles;=================================set xAngle to xAngleA + xAngleBif xAngle > 360  set xAngle to xAngle - 360endifif xAngle < 0  set xAngle to xAngle + 360endif

User avatar
matt white
 
Posts: 3444
Joined: Fri Jul 27, 2007 2:43 pm

Post » Wed Dec 07, 2011 5:00 am

Excellent idea! I have already benefited from this thread thanks. :goodjob:
User avatar
Logan Greenwood
 
Posts: 3416
Joined: Mon Jul 30, 2007 5:41 pm

Post » Wed Dec 07, 2011 12:21 pm

Objective: Slide an object up and down

This script should be attached to an activator to move the object up or down each time the activator is activated.
The activator may be the object itself.
Change the numbers for speed and max displacement to fit your needs.
Can be easily adapted to sliding sideways.

scn SlidingObjectSCRIPTref xObjectshort xStdSpeedshort xSpeedshort xMaxDisplacementfloat xPosZfloat xPosMinfloat xPosMaxbegin onactivate	if xObject == 0		; First time only: set vars		;======================		; xStdSpeed positive: Object will initially go up 		; xStdSpeed negative: Object will initially go down		;======================		set xObject to aaqqActivator;  aaqqPlatform ; aaqqPaint		set xStdSpeed to -5 ; <<< Change to your liking  (units per second)		set xMaxDisplacement to 100   ; <<< Displacement limit		set xPosZ to xObject.getstartingpos z		if xStdSpeed > 0			set xPosMin to xPosZ			set xPosMax to xPosZ + xMaxDisplacement 		else			set xPosMax to xPosZ			set xPosMin to xPosZ - xMaxDisplacement 			set xStdSpeed to -xStdSpeed 		endif	endif	if xPosZ <= xPosMin 		set xSpeed to xStdSpeed 	else		set xSpeed to -xStdSpeed 	endifendbegin gamemode	if xSpeed == 0		return	endif	set xPosZ to xObject.getpos Z + xSpeed * getsecondspassed	if xPosZ > xPosMax		set xPosZ to xPosMax		set xSpeed to 0	endif	if xPosZ < xPosMin		set xPosZ to xPosMin		set xSpeed to 0	endif	xObject.setpos Z xPosZ	xObject.reset3Dstateend

User avatar
Tinkerbells
 
Posts: 3432
Joined: Sat Jun 24, 2006 10:22 pm

Post » Wed Dec 07, 2011 12:53 pm

Bringing this up to the front with a thanks for this thread Felic!
User avatar
Robert Jackson
 
Posts: 3385
Joined: Tue Nov 20, 2007 12:39 am

Post » Wed Dec 07, 2011 9:06 am

Detecting collision of items (objects which can be held in containers)

The construction set defines collision sounds of havoked items explicitly, which can be detected to approximate an item's collision. Though this method has a high success rate and good accuracy, it's not completely fool proof. Inherently, the game engine doesn't play the collision sounds at times. From my testing, this has something to do with the mesh of the item. It seems that the bhkCollisionObject's bhkXYZShape needs to be triggered by the collision to cause the sound; ergo the material defined by the bhkXYZShape determines the sound being played. More about that aspect in this http://www.gamesas.com/bgsforums/index.php?showtopic=990278&hl=. This method requires OBSE.

Another method to detect collisions would be to check if the distance of the item from a fixed point reduces in an subsequent frame. More clearly, if the distance in the last frame was greater the distance in the current frame, it could possibly imply a collision. The obvious flaw in this method would be that this method detects collisions only if the item bounces off a surface, causing the distance to reduce. This method does not require OBSE.

Method 1 : Using collision sounds
OBSE's http://obse.silverlock.org/obse_command_doc.html#GetSoundPlaying function can determine if the collision sounds are being played during a frame, in the vicinity of a reference or otherwise.
 if ( rItem.getSoundPlaying "C*" 100 ); collision handling endIf

The numeric parameter can be modified to increase the sound detection radius. Currently, the code checks for any collision sound being played within 100 units of the item, which seems to be a radius good enough to gauge the item's collision. The material of the item/surface can be determined by checking for the particular collision sound associated with it. The sounds are defined in the Sound tab of the Object Window.

Method 2 : Using distance checks
 float fCurrentDistance float fLastDistance   ref rItem  begin gameMode	set fCurrentDistance to rItem.getDistance rMarker; where rMaker is a persistent reference and is in the same cell as the item 	if ( fCurrentDistance != fLastDistance )	   if ( fCurrentDistance < fLastDistance ); collision handling	   endIf 	   set fLastDistance to fCurrentDistance	endIf end

This method is not reliable for items being dropped down vertically. The item has to be moving horizontal to some extent to get the system to trigger the detection.
User avatar
rheanna bruining
 
Posts: 3415
Joined: Fri Dec 22, 2006 11:00 am

Post » Wed Dec 07, 2011 3:31 pm

Bumping . . . for the benefit of those that may have missed this thread . . . very useful.
User avatar
Kayla Keizer
 
Posts: 3357
Joined: Tue Dec 12, 2006 4:31 pm

Post » Wed Dec 07, 2011 6:11 pm

A small snippet of code that is extremely useful. It allows you to check when an actor value changes.

Here is it for level

If Level != Player.GetLevel		*Do Stuffs*	Set Level to Player.GetLevelEndif


Another example showing another way it can be used
Set cHealth to Player.GetAV HealthIf Health != cHealth  Set HealthDifference to Health - cHealth  Set Health to Player.GetAV HealthEndif

User avatar
El Goose
 
Posts: 3368
Joined: Sun Dec 02, 2007 12:02 am

Post » Wed Dec 07, 2011 11:59 am

Can some1 explain that last piece of code to us noobs? How it works and why it is, the way it is?
User avatar
Lily Something
 
Posts: 3327
Joined: Thu Jun 15, 2006 12:21 pm

Post » Wed Dec 07, 2011 5:45 pm

Bump to prevent the thread from entering Oblivion.
User avatar
Austin England
 
Posts: 3528
Joined: Thu Oct 11, 2007 7:16 pm

Post » Wed Dec 07, 2011 5:15 pm

Another bump, as I've just referenced some code here on another thread.
User avatar
Madison Poo
 
Posts: 3414
Joined: Wed Oct 24, 2007 9:09 pm

Previous

Return to IV - Oblivion