Aller au contenu

Photo

Random float or delay with an int?


  • Veuillez vous connecter pour répondre
21 réponses à ce sujet

#1
RetpircsWols

RetpircsWols
  • Members
  • 8 messages
// UNSPAWN TIMER SET
//    int rnd;
//    rnd = (Random(10)+10); // Calculate a random number	
    object oTarget = (OBJECT_SELF);	
	float fDelay = (Random(10f));
DelayCommand(fDelay, DestroyObject (oTarget));

Scripting Champions,
 
I have a question that, once again, shows my lack of understanding for this language.  I am trying to write a script that kills a creature after a random time.  It is an appendage to the standard gb_comp_spawn
 
I believe I have two ways to solve this... I cannot make either work.  I started with a random, which is an int.  Is there a way to make and int into a float for the delay command?  Elseif, is there a way to make a random float?

 

I tried to make, for testing sake, a short timer.  The above attempt is for 0 - 20 seconds, I believe, if Random (x) is able to produce 0. 
 
Attached is my current nonfunctioning keyboard hammerings.  You can see how I went through the above... then gave up.  As I said, this is just after the last line but before the closing bracket of gb_comp_spawn

Retpircs Wols
not the fastest way, but I get there eventually. usually.



#2
Tchos

Tchos
  • Members
  • 5 030 messages

First, DestroyObject has a delay parameter, so there's no need to wrap it in a DelayCommand. 

 

Yes, you can convert an int into a float with IntToFloat().

 

Random does produce 0, but your code was not formatted for a total of 20 seconds.  It should be Random(20) for 0-19, or Random(20)+1 for 1-20.

float fDelay = IntToFloat(Random(20)+1);
DestroyObject(OBJECT_SELF, fDelay);


#3
Tchos

Tchos
  • Members
  • 5 030 messages

However, if you're planning to make a longer timer, I would recommend putting it in the heartbeat script, not a delayed command in the spawning script.



#4
RetpircsWols

RetpircsWols
  • Members
  • 8 messages
    int rnd = (Random(10)); // Calculate a random number	
	if (rnd < 9)
	{
		DestroyObject (OBJECT_SELF);
	}

Tchos, I put that into both the OnSpawn and Heartbeat; it compiles in both, but does not kill the critter.  I tried to find how the original game makes animal summons only last a short time, but cannot find the script to do it.  I even tried adding the attached script to heartbeat.  I thought it would give it a 9/10 chance to die every heartbeat (again, for testing purposes).  Nothing.



#5
Tchos

Tchos
  • Members
  • 5 030 messages

Is your creature set to Plot or Immortal? 



#6
rjshae

rjshae
  • Members
  • 4 478 messages
I tried to find how the original game makes animal summons only last a short time, but cannot find the script to do it.

 

The summon creature script uses EffectSummonCreature and applies it as a temporary effect. Have a look at nw_s0_summon5.



#7
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

The reason that script does not work is because OBJECT_SELF, when used on the OnHearbeat, OnSpawn or whatever other OnScript of the module properties, it refers to the entire module, not to the creature, that's why it doesn't kill it. With that script you're trying to kill the module instead =D

 

EDIT: Apologies, I made a wrong assumption, my bad.



#8
Tchos

Tchos
  • Members
  • 5 030 messages

Where did he say it was in the module heartbeat?  He previously said he was modifying companion spawn scripts, so I would expect when he tried the heartbeat he was using the companion heartbeat, not the module heartbeat.  Module properties don't even have an On Spawn slot.

 

In any case, I would also add a feedback test to see if the changes are being executed at all.  The companion scripts are overridden by several mods, so these changes may not appear.  It would be better to use a script with a unique name than to modify the default scripts.



#9
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

Where did he say it was in the module heartbeat?  He previously said he was modifying companion spawn scripts, so I would expect when he tried the heartbeat he was using the companion heartbeat, not the module heartbeat.  Module properties don't even have an On Spawn slot.

 

In any case, I would also add a feedback test to see if the changes are being executed at all.  The companion scripts are overridden by several mods, so these changes may not appear.  It would be better to use a script with a unique name than to modify the default scripts.

 

You are right, I misread, I apologize.

Perhaps the destroy object function is not working properly? As a way of feedback testing, this could be tried instead: (in the hearbeat of the creature)

void main()
{
	object oPC = OBJECT_SELF;
	int rnd = (Random(10)); // Calculate a random number	
	if (rnd < 9)
	{
		SetImmortal(oPC, FALSE);
		effect eDMG = EffectDamage(9999, DAMAGE_TYPE_DIVINE, DAMAGE_POWER_NORMAL, TRUE);
		DelayCommand(0.1, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDMG, oPC));
	}
}

If this doesn't work then the script isn't being called at all, otherwise, the problem may lie within the DestroyObject function.



#10
RetpircsWols

RetpircsWols
  • Members
  • 8 messages

void KillIt()
{
	object oPC = OBJECT_SELF;
	int rnd = (Random(10)); // Calculate a random number 0 - 10	
	if (rnd < 3) //30% chance to kill
	{
		SetImmortal(oPC, FALSE);
		effect eDMG = EffectDamage(9999, DAMAGE_TYPE_DIVINE, DAMAGE_POWER_NORMAL, TRUE);
		DelayCommand(0.1, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDMG, oPC));
	}
	
		DelayCommand(30.0f,KillIt()); // live for another 30 seconds, then take another spin at Russian Roulette 
}

void main()
{

float fDelay = IntToFloat(Random(300)+240); //gives a time frame between 4 and 9 minutes
//float fDelay = IntToFloat(Random(15)+10); //test time frame

	DelayCommand(fDelay,KillIt()); //go to sub routine
}

Clangeddin,

That did it.  I am not sure why, buy destroy object is not working.  I put this in as a standalone heartbeat script and appended it to the other (note: I mean a copy of the original scripts, then edited and given unique names).  Both work, but I do not think there needs to be anything else in a heartbeat for a creature that follows you around as a henchman.  This works really well, and there is no doubt that the henchman went down when big red "9999" flashes over it.  Thanks for all the work, guys.  I think I am going to put these together...

 

This combined script worked well.  Thank you, you guys are awesome.  Mark this one solved.



#11
Tchos

Tchos
  • Members
  • 5 030 messages

I'd still like to know whether the creature was set to plot or immortal.  Clangeddin's script turns off the immortal flag just in case, but if that was the cause, then DestroyObject may work fine after you turn it off on the creature's properties.

 

Also, if you want to kill something without showing damage numbers, just apply EffectDeath to it.



#12
RetpircsWols

RetpircsWols
  • Members
  • 8 messages

Tchos,

 

It was not set to immortal or plot.  They died just fine in combat, but not from Destroy Object.  I'm not sure what was going on there.



#13
Tchos

Tchos
  • Members
  • 5 030 messages

Okay, thanks.



#14
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

Well, glad to hear it worked out in the end. =)

Maybe DestroyObject is just not supposed to work on creatures, but only on items and placeables.



#15
Tchos

Tchos
  • Members
  • 5 030 messages

Maybe DestroyObject is just not supposed to work on creatures, but only on items and placeables.

 

I can tell you for certain it does work on creatures.  I use it in numerous scripts for that purpose -- for instance for my spawning merchant creature, which is created and destroyed on demand.



#16
Claudius33

Claudius33
  • Members
  • 256 messages

DestroyObject should work on anything except modules and areas. I have successfully used it on creatures and placed effects.



#17
Tchos

Tchos
  • Members
  • 5 030 messages

Yes, and the creatures just fade away when you use it.  They do not go through a death animation.



#18
Claudius33

Claudius33
  • Members
  • 256 messages

Right. For creatures I do it in the background, never in front of the party.

 

For instance when an important NPC met in one town (instance #1) is met again in another town (#instance 2) later on. I delete instance #1 from Instance #2 convo if both towns are in same module, or in a clean-up script when (re)-entering module 1.

 

In front of the party I use ga_force_exit or a kill script if appropriate.



#19
ColorsFade

ColorsFade
  • Members
  • 1 267 messages

If you get a stock function that stops working, look in your campaign and module folders for standard scripts with a 0kb size. Sometimes when writing and compiling scripts, the toolset will create these spurious files. And since they're in your campaign folder, they override the game's default scripts. Delete them and everything goes back to normal. 

 

I've had this happen quite a few times. No idea what it is in the toolset that makes it create those files. 


  • GCoyote aime ceci

#20
rjshae

rjshae
  • Members
  • 4 478 messages

Possibly the DestroyObject call gets aborted when the engine detects it is being fired while still running the object's heartbeat script? Otherwise it may end up in a bad state. The delay causes it to fork off an independent executable that doesn't depend on the creature being called.



#21
andysks

andysks
  • Members
  • 1 645 messages

I can also say that destroy object never caused me problems. As a general advise, it never hurts to add some debug messages to see where the script fails. Many of us use SendMessageToPC and get reports of the script stages and behavior in your chat window. In any case, the solution you have there works, and that's fine. But a function all of us say it works and it doesn't work for you is a matter of research. And a very important function nonetheless. 


  • ColorsFade aime ceci

#22
RetpircsWols

RetpircsWols
  • Members
  • 8 messages

Andysks,

 

Here is the most frustrating part: I played through to test the function in another area, and it worked.  It is called from a conversation, taking creatures of a certain tag, then the last node of the conversation destroys the speaker, on a two second delay.  It all goes as it should.  I know this is something I did wrong, but I absolutely cannot figure it out!