HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: [Resolved] My First HSL Script - Timer excercise  (Read 862 times)

carterza

  • General Accounts
  • *
  • Posts: 1
    • View Profile
[Resolved] My First HSL Script - Timer excercise
« on: Jun 19, 12, 07:57:25 AM »

Hi all,

This is my first post on these boards so I suppose I should start off with a hello! Hopefully I'll be able to contribute by answering others questions down the road, but I'm going to start off by asking my own as I'm completely new to HSL.

So I was going through the 'My First HSL Script' tutorial and I got to the exercises portion of the tutorial. The section I'm really struggling with is the timer section.

The section reads - "Instead of changing the color each time the mouse enters or with a timeline, add a timer field to your class and change the color at some time interval. You will need to start the timer the first time the mouse enters... "

There's also a link to the TIMER wiki page - http://wiki.heroengine.com/wiki/Timers which I consulted.

Okay so from the sound of it, I need to add a timer field to my class. The sample code on the TIMER wiki page refers to a field named myTimer, so I searched for that field name in the list of available client fields and found it. Sweet! I found my class in the DOM Editor under client classes and I added a Field reference and set it to reference the myTimer field.

Now the first thing I notice when I go to edit my class script (which I've now added a myTimer field to) is that I can't access the field's member properties in my script.

If I try to for instance do -

myTimer.realTime = true

At compile time I receive an error regarding an undefined identifier. Is this because I haven't initialized the myTimer field? How would I go about doing that? Where should I handle initialization? I know there are no constructors in HSL so I'm a bit confused regarding control flow.

Nonetheless I figured out a workaround which simply involved creating a new node reference (which I guess in traditional programming would be the equivalent of instantiating an object) like so -

n as NodeRef of Class genericTimer = CreateNodeFromClass("GenericTimer")

As I noticed the myTimer field was referenced by GenericTimer I was now able to go ahead and create an instance of this class.

Now I can do -

n.myTimer.realTime = true

Awesome! However I still have questions... And this is where I really start scratching my head. In the TIMER example code it looks like the author is overriding the tick function of the timer class so I went ahead and copied that code into my custom classes script -

Code: [Select]
function myTimer_tick()
  where me is kindof GenericTimer
    if (me.myTimer.elapsedTime.inMilliseconds > 100)
      println( me.myTimer.lastFired.hour )
      println( me.myTimer.startTime.monthString )
    .
    me.myTimer.stop()
  .
.

There's some sort of contextual switch that goes on here obviously, and I'm guessing the statement - where me is kindof GenericTimer - switches that context? Also I read something about context being different in the case of a method vs a function, regardless I'm confused as hell here.

My main problem is the fact that I don't know how to override this method / define a new signature. The exercise requires me to change the color of a node / object which I grabbed reference to in a previous function -

Code: [Select]
shared function InputMouseEvent(args references Class _MouseEvent)
...
 
  // Get the Node For which the Event was Generated
  target as NodeRef = args.MouseTarget
  n as NodeRef of Class genericTimer = CreateNodeFromClass("GenericTimer")
 
  // Type Cast the node so we can access its fields/properties.
  where target is kindof HBNode
    when args.MouseEventType
      is Enter
...
      .
    .
  .
.

How can I get reference to target again in the tick function? myTimer_Tick() doesn't take in any arguments and I have no idea how to override this function. I know I'm just not getting the whole picture here but any help is greatly appreciated (if you can provide me with other code to look at that would be most excellent!)

Thanks,

-Zach
« Last Edit: Nov 02, 12, 09:36:24 PM by HE-Cooper »
Logged

HE-JAY

  • HeroEngine
  • *****
  • Posts: 122
    • View Profile
Re: My First HSL Script - Timer excercise
« Reply #1 on: Jun 19, 12, 10:07:58 AM »

Two things:
  • In order to access a field on the current node from within a method, you need to reference it with the 'me' keyword (http://hewiki.heroengine.com/wiki/ME#The_me_Node_2). In your case, 'me.myTimer.timerData'
  • When a timer ticks, it will either call the 'method' on the node to which it belongs (in your case, that would be the genericTimer) or it will call the 'function' in the script specified by the 'script' field. Before starting the timer, you'll need to set that script field to be the scriptref of the script in which your callback function resides

An example:
Code: [Select]
method myMethod()
  me.myTimer.script = SYSTEM.EXEC.THISSCRIPT
  me.myTimer.fireRate = 00:00:01
  me.myTimer.start()
.

function myTimer_tick()
  println("Tick!")
.

Also, there are several ways to reference the owner of the timer; one is to store a reference to the node in the timerData field; another is to use associations to link the node which contains the timer to another node (http://hewiki.heroengine.com/wiki/Associations); a third  is to use 'me' (which - in a timer callback - will refer to the node which contains the timer) to find your source node:
Code: [Select]
function myTimer_tick()
  where me is kindof MyNodeClass
    me.myOtherMethod("Tick!")
  .
.
« Last Edit: Jun 19, 12, 10:10:50 AM by HE-Jay »
Logged