Need scripting help: Rand and OnControlDown Info

Post » Mon May 17, 2010 10:02 am

I need some serious help here or I will be unable to complete the mod I'm working on. I'm trying to set up a script that when you begin sneaking, the script kicks, giving a random chance for NPCs to appear. I have a rough draft of it and tried to test it, but I couldn't get the script to even save in the construction set. First it kept telling me there had to be a begin/end for all scripts. After putting in a Begin Gamemode/end block, it keeps telling me that Line 10 has a syntax error: Undefined function Set. Line 10 is the If OnControlDown 9 Set A to Rand 1 4.

Here's how I structured the script:

ScriptName AAAStealthWarBook
;This activates the random stealther chance

Begin Gamemode

Short Doonce
float A
float B

If OnControlDown 9 Set A to Rand 1 4
If A <= 2 Set B to Rand 1 2
Endif
If B == 1 NPC1.MoveTo Player 100,100,0
NPC1.enable
Endif
If B == 2 NPC2.MoveTo Player -100,100,0
NPC2.enable
Endif
Endif
End

I also had some errors in regards to the OnControlDown, saying something about it being wrong too. I can't remember the error message, but I switched it around to read the way the current script is now.

Thank you for helping out if you can.
User avatar
A Lo RIkIton'ton
 
Posts: 3404
Joined: Tue Aug 21, 2007 7:22 pm

Post » Mon May 17, 2010 7:17 pm

Short Dooncefloat Afloat BBegin Gamemode	If ( OnControlDown 9 #)		Set A to Rand 1 4	EndIf	If ( A <= 2 )  	    Set B to Rand 1 2                     SetA to 0	Endif	If ( B == 1  )	    NPC1.MoveTo Player 100,100,0	    NPC1.enable                    Set B to 0	ElseIf ( B == 2 )	     NPC2.MoveTo Player -100,100,0	     NPC2.enable                     Set B to 0	EndifEnd 


Basically, "IF" statements need to be defined on one line, with their action inside an "If/EndIf", "If/ElseIf/EndIf" or "If/Else/EndIf" Block. It's recommended to put the conditions in Brackets ( ) to :) You're also probably best off defining Variables outside of the Blocks, since that's standard practice :)
User avatar
Krista Belle Davis
 
Posts: 3405
Joined: Tue Aug 22, 2006 3:00 am

Post » Mon May 17, 2010 2:12 pm

Okay, I'll give it a try tonight. Thank you so much!

:bowdown:

Edit: Oh, Setting A and B back to 0 resets the value I guess so that it can be reused?
User avatar
Nicole M
 
Posts: 3501
Joined: Thu Jun 15, 2006 6:31 am

Post » Mon May 17, 2010 6:44 am

I see two problems with your script:

1. The gamemode block runs every frame or almost every frame (if you use a quest script, but you need a low questdelaytime in that case to check for the key press). I'm not 100% sure how oncontroldown works, but I think it returns 1 as long as the key is pressed. If the player presses the key for half a second and has a framerate of 40 it means the script will run 20 times in a row and statistically spawn 10 NPCs at once. You need a condition to prevent the script from running that often. Maybe a timer or checking for gamedayspassed so it is only possible every x minutes or once per day that a NPC will spawn.

2. This part will (almost) never turn true:

	If ( B == 1  )	    NPC1.MoveTo Player 100,100,0	    NPC1.enable                    Set B to 0	ElseIf ( B == 2 )	     NPC2.MoveTo Player -100,100,0	     NPC2.enable                     Set B to 0	Endif


The B variable is a float and rand generates a float value. It will almost never be exactly 1 or exactly 2. Instead it will be something like 1.3265 or a similar value. To make it work you need to use

if B <= 1.5  ;blaelse  ;blaendif


EDIT: Just noticed you have a doonce variable, but it isn't used in your script. Maybe you just didn't post that part of the script and my first point is obsolete.

And in case you use a quest script it might be a good idea to check for player.issneaking instead of oncontroldown. It is far less time sensitive and you can set your questdelaytime easily to 1 instead of the 0.01 you'd need for the key check.
User avatar
tannis
 
Posts: 3446
Joined: Sat Dec 09, 2006 11:21 pm

Post » Mon May 17, 2010 5:13 pm

I see two problems with your script:

1. The gamemode block runs every frame or almost every frame (if you use a quest script, but you need a low questdelaytime in that case to check for the key press). I'm not 100% sure how oncontroldown works, but I think it returns 1 as long as the key is pressed. If the player presses the key for half a second and has a framerate of 40 it means the script will run 20 times in a row and statistically spawn 10 NPCs at once. You need a condition to prevent the script from running that often. Maybe a timer or checking for gamedayspassed so it is only possible every x minutes or once per day that a NPC will spawn.

2. This part will (almost) never turn true:

	If ( B == 1  )	    NPC1.MoveTo Player 100,100,0	    NPC1.enable                    Set B to 0	ElseIf ( B == 2 )	     NPC2.MoveTo Player -100,100,0	     NPC2.enable                     Set B to 0	Endif


The B variable is a float and rand generates a float value. It will almost never be exactly 1 or exactly 2. Instead it will be something like 1.3265 or a similar value. To make it work you need to use

if B <= 1.5  ;blaelse  ;blaendif


EDIT: Just noticed you have a doonce variable, but it isn't used in your script. Maybe you just didn't post that part of the script and my first point is obsolete.

And in case you use a quest script it might be a good idea to check for player.issneaking instead of oncontroldown. It is far less time sensitive and you can set your questdelaytime easily to 1 instead of the 0.01 you'd need for the key check.


Hmm, I might investigate using a quest script then. I was looking for a way to have a continual check while the player is sneaking, not a constant way, but say something like every 5 seconds. However, I was really hoping not to have to use a quest script to run this script. I wanted it to be put on an misc item that you can remove if wanted to in order to stop the script from affecting you.

You said the doonce command is not being used by the script? Should I place it in the initial Rand string for variable A?

Is there a way I can use whole numbers rather than numbers with decimals? I thought Short part in Short Doonce makes it do that. Or do I need to put Short in the Rand command?
User avatar
Andres Lechuga
 
Posts: 3406
Joined: Sun Aug 12, 2007 8:47 pm

Post » Mon May 17, 2010 7:37 am

Hmm, I might investigate using a quest script then. I was looking for a way to have a continual check while the player is sneaking, not a constant way, but say something like every 5 seconds.

You said the doonce command is not being used by the script? Should I place it in the initial Rand string for variable A?

Is there a way I can use whole numbers rather than numbers with decimals? I thought Short part in Short Doonce makes it do that. Or do I need to put Short in the Rand command?


You only declared a variable called doonce, but it is not used anywhere. The question is whether you want to use the script only once or once in a while.

If you only want to use it once the best way would be a quest script and the stopquest command at the end of the script. That way the script would completely stop running after the NPC has spawned and there is no need for another variable.

If you want to use it once in a while you need a condition to specify how often it runs. Like a timer or checking for gamedayspassed.

You can use whole numbers, but rand always generates float values. A possible way to do this would be:

short B

set B to rand 1 3

if B == 1
; do stuff
else
; do stuff

This works because B is a short now and will become an unrounded integer. So if rand rolls 1.532 it will become 1, if it rolls 2.784 it will become 2.
User avatar
Lisha Boo
 
Posts: 3378
Joined: Fri Aug 18, 2006 2:56 pm

Post » Mon May 17, 2010 2:53 pm

You only declared a variable called doonce, but it is not used anywhere. The question is whether you want to use the script only once or once in a while.

If you only want to use it once the best way would be a quest script and the stopquest command at the end of the script. That way the script would completely stop running after the NPC has spawned and there is no need for another variable.

If you want to use it once in a while you need a condition to specify how often it runs. Like a timer or checking for gamedayspassed.

You can use whole numbers, but rand always generates float values. A possible way to do this would be:

short B

set B to rand 1 3

if B == 1
; do stuff
else
; do stuff

This works because B is a short now and will become an unrounded integer. So if rand rolls 1.532 it will become 1, if it rolls 2.784 it will become 2.



Okay, I think I understand now. I wouldn't want to use Float A or B if I wanted to use whole numbers. I'd use Short. So really, I wouldn't need to use Float at all then. I think I will use a quest script now. I definately want the script to run more than once. In fact, I wanted it to ultimately run every 5-10 seconds while the player is sneaking. But, I wanted it to stop running once the player has stopped sneaking. So therefore, I wouldn't need to use Doonce either.
User avatar
Amy Melissa
 
Posts: 3390
Joined: Fri Jun 23, 2006 2:35 pm

Post » Mon May 17, 2010 5:43 pm

Okay, I think I understand now. I wouldn't want to use Float A or B if I wanted to use whole numbers. I'd use Short. So really, I wouldn't need to use Float at all then. I think I will use a quest script now. I definately want the script to run more than once. In fact, I wanted it to ultimately run every 5-10 seconds while the player is sneaking. But, I wanted it to stop running once the player has stopped sneaking. So therefore, I wouldn't need to use Doonce either.


By default quest scripts run every five seconds. You can change the rate by using a local variable called fquestdelaytime. Like

float fquestdelaytime

set fquestdelaytime to 10

will make the quest script run every 10 seconds.

50% chance to spawn NPCs every 5 seconds the player is sneaking is a bit over the top if you ask me. Everytime you sneak for a while you'll end up with dozens of NPC spawning all over the place. I think you need a condition to limit the amount of NPCs that can spawn.
User avatar
Christie Mitchell
 
Posts: 3389
Joined: Mon Nov 27, 2006 10:44 pm

Post » Mon May 17, 2010 7:43 pm

By default quest scripts run every five seconds. You can change the rate by using a local variable called fquestdelaytime. Like

float fquestdelaytime

set fquestdelaytime to 10

will make the quest script run every 10 seconds.

50% chance to spawn NPCs every 5 seconds the player is sneaking is a bit over the top if you ask me. Everytime you sneak for a while you'll end up with dozens of NPC spawning all over the place. I think you need a condition to limit the amount of NPCs that can spawn.

Oh definately, this is just testing the script and making sure the NPCs will be behaving correctly, making sure their placement to the player is correct, and a couple other variables. The final script will be much much less. I think I intended 20%, but even that will probably be reduced, especially considering the nature of the NPCs: They'll be pretty rough to fight. :toughninja:
User avatar
Céline Rémy
 
Posts: 3443
Joined: Sat Apr 07, 2007 12:45 am


Return to IV - Oblivion