SetTimerScript help needed
duggold
09 Jun 2012, 23:00I am trying to use something like this:
SetTimerScript(mytimer) {
monster_attack(y.name)
}
Y is an object passed to my function. So, at the time I set the script, y.name is the name of the object. But, when the timer gets called, y I guess is out of scope and it tries to evaluate y.name and can't.
How can I pass non static parameters as part of the script I setup in SetTimerScript?
Also, does a timer kill itself once its interval is hit and its script is fired or does it reset? In other words, if I set the interval to 30 seconds, does it fire every 30 seconds, or just once, 30 seconds later?
SetTimerScript(mytimer) {
monster_attack(y.name)
}
Y is an object passed to my function. So, at the time I set the script, y.name is the name of the object. But, when the timer gets called, y I guess is out of scope and it tries to evaluate y.name and can't.
How can I pass non static parameters as part of the script I setup in SetTimerScript?
Also, does a timer kill itself once its interval is hit and its script is fired or does it reset? In other words, if I set the interval to 30 seconds, does it fire every 30 seconds, or just once, 30 seconds later?

Pertex
10 Jun 2012, 08:07duggold wrote:I am trying to use something like this:
SetTimerScript(mytimer) {
monster_attack(y.name)
}
Y is an object passed to my function. So, at the time I set the script, y.name is the name of the object. But, when the timer gets called, y I guess is out of scope and it tries to evaluate y.name and can't.
How can I pass non static parameters as part of the script I setup in SetTimerScript?
Could you post you gamefile, plz?
duggold wrote:
Also, does a timer kill itself once its interval is hit and its script is fired or does it reset? In other words, if I set the interval to 30 seconds, does it fire every 30 seconds, or just once, 30 seconds later?
You can have both. Normal timer fires every interval, but there is also a "run script after a number of seconds" which only fires once.
duggold
10 Jun 2012, 12:40Ok, below is the file. Just go south from the first room. If no ogre spawns, go north then back south until it tells you the ogre shows up. Then wait about 30 seconds for its action script to fire.
<!--Saved by Quest 5.2.4515.34846-->
<asl version="520">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="tester">
<gameid>33b72982-78b8-4684-8ddd-fbe3c93c0934</gameid>
<version>1.0</version>
<author>Doug Goldner</author>
<category>Fantasy</category>
</game>
<object name="Starting Room">
<inherit name="editor_room" />
<description>You are in the starting room. Shock...</description>
<object name="player">
<inherit name="defaultplayer" />
</object>
<exit alias="north" to="Second Room">
<inherit name="northdirection" />
</exit>
</object>
<object name="Second Room">
<inherit name="editor_room" />
<description>You are now in the second room. Woop!</description>
<enter type="script">
random_monster (50, ogre)
</enter>
<exit alias="south" to="Starting Room">
<inherit name="southdirection" />
</exit>
<object name="Bow">
<inherit name="editor_object" />
<worn type="int">0</worn>
<look>Bow 1</look>
<take />
<inventoryverbs>Look at; Use; Drop; Equip</inventoryverbs>
<alias>Bow</alias>
<equip type="script">
do_equip ("Bow")
</equip>
</object>
<object name="Hammer">
<inherit name="editor_object" />
<alias>Hammer</alias>
<worn type="int">0</worn>
<inventoryverbs>Look at; Use; Drop; Equip</inventoryverbs>
<equip type="script">
do_equip ("Hammer")
</equip>
<take />
</object>
</object>
<object name="ogre">
<inherit name="editor_object" />
<visible type="boolean">false</visible>
<speed type="int">20</speed>
<last_seen type="int">0</last_seen>
<chance_to_hit type="int">30</chance_to_hit>
<damage_base type="int">4</damage_base>
<damage_random type="int">6</damage_random>
<look>The ogre is mean looking!</look>
</object>
<verb>
<property>equip</property>
<pattern>equip</pattern>
<defaultexpression>"You can't equip " + object.article + "."</defaultexpression>
</verb>
<function name="monster_attack" parameters="monster_to_attack">
msg ("You see a " + monster_to_attack + " attack!")
</function>
<function name="do_equip" parameters="obj_name">
us = GetObject(obj_name)
if (us.worn = 0) {
inven = GetDirectChildren(GetObject("player"))
for (myItem, 0, ListCount(inven) - 1) {
the_item = ObjectListItem(inven,myItem)
if (the_item.worn = 1) {
the_item.worn = 0
the_item.alias = Left(the_item.alias,LengthOf(the_item.alias) - 6)
}
}
}
if (us.worn = 1) {
us.alias = Left(us.alias,LengthOf(us.alias) - 6)
msg ("You no longer have your " + us.name + " equipped")
us.worn = 0
}
else {
us.alias = us.alias + "(worn)"
msg ("You equip yout " + us.name)
us.worn = 1
}
</function>
<function name="random_monster" parameters="chance, monster_name">
msg ("Checking for random monster with chance " + ToString(chance))
if (RandomChance(chance)) {
y = CloneObject(monster_name)
y.parent = player.parent
MakeObjectVisible (y)
msg ("You see a " + y.alias + " enter the room!")
create timer ("mtimer")
our_time = GetTimer("mtimer")
SetTimerInterval (our_time, y.speed)
SetTimerScript (our_time) {
monster_attack (y.name)
}
EnableTimer (our_time)
}
</function>
<function name="change_name">
</function>
</asl>

Pertex
10 Jun 2012, 14:23Ah yes, you are right.When the timer function is called y isn't set any more. So when cloning the monster save it in a global variable
and best is to set an alias, too
Then you need no parameter for monster_attack () and the function looks like this:
game.monster=y
and best is to set an alias, too
y.alias=monster_name.name
Then you need no parameter for monster_attack () and the function looks like this:
<function name="monster_attack" >
msg ("You see a " + game.monster.alias + " attack!")
</function>
duggold
10 Jun 2012, 16:43The issue I have is there can be a timer for each "Active" monster. So, I can't just store 1 monster object name. I need a way to know the name of the timer that fired (then I can make a list to store the timer->object connection info). Or, I need a way to pass a parameter to the timer firing script (to hold the timer name, name of the monster, or some other identifying id).
I basically need a way to know which of my 10 timers fired, as then I know which monster to perform an action with.
I basically need a way to know which of my 10 timers fired, as then I know which monster to perform an action with.

Pertex
10 Jun 2012, 20:29I did not try that but you could add an additional attribute to your timer when creating it to save the monstername in it
duggold
10 Jun 2012, 22:07The problem is, all timers call the same script (do_attack for example). How can I tell, from within the script that the timer fires, WHICH timer called the script? I don't have 20 timers with 20 scripts so I need to tell from within the single script, which timer fired. What I am doing now, is I have a single list of events that need to happen and 1 timer firing every 5 seconds. It checks to see which events need to fire (based on game.elapsedtime) and runs them. The list also contains the monster name so when I process the event, I know which monster is causing the action. This solution may actually be better as it keeps me from needing 1 script for each active monster.

Pertex
11 Jun 2012, 08:58OK, now I got it. Try "this.name" in the timerscript