Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: this keyword  (Read 2944 times)

0 Members and 1 Guest are viewing this topic.

rice

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 12
    • View Profile
this keyword
« on: December 28, 2013, 11:41:53 AM »

I've tried searching the forums, but "this" and "this keyword" and "this." yields a lot of results, as you can imagine.

You can attach a script to an object either using the SceneEdit app or by calling the AttachScript method in your code.

What I don't understand is why when using, for instance, the SetCursor method on an object, I have to call that method using the this keyword.  I've tried referencing the object by its name in both scene_init.script and oldguy.script.

For example, my first attempt was to call OldGuy.SetCursor("sprites\system\cur_wait.sprite"); in scene_init.script, but that did not work.  Saving the return value and printing it using Game.Msg(setcursor_result); produced a null type.

Moving it to oldguy.script didn't work either.  But calling this.SetCursor("sprites\system\cur_wait.sprite"); in that script created the behavior I was looking for.  Hovering the mouse over that object changed the cursor to the hourglass sprite.

I'd simply like to know why.  The script reference makes it unclear which methods only work this way.  Otherwise, it's not a problem.
Logged

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5683
    • View Profile
    • Dead:Code Site
Re: this keyword
« Reply #1 on: December 28, 2013, 01:22:21 PM »

Ok, so a WME game contains a lot of "objects". The scene is an object, scene entities are objects, actors, windows, buttons etc. Everything is an object.
Those objects provide properties and methods (such as the SetCursor() method you mentioned).

Now, to be able to call the methods, you need to obtain a reference to the object somehow. There are several ways how to do it. Most commonly, to reference a scene entity, you need to ask the scene to give it to you:

Code: WME Script
  1. var myObject = Scene.GetNode("some_name");
  2.  

Here you're saying, "hey, scene, give me a reference to a scene entity called 'some_name'". Once you have the reference, you can call methods on it.

Code: WME Script
  1. myObject.SetCursor("some_image.png");
  2.  

Now, the "this" keyword: It's a shortcut that will return a reference to the object which owns the current script.
For example, you create a scene entity called "door" in SceneEdit and you attach a new script to the entity. Since the script is attached to the door entity, the "this" keyword in this particular script will return a reference to the door entity.

Code: WME Script
  1. Game.Msg(this.Name); // this will print "door"
  2.  
  3. // you can still get the reference the other way, though
  4. var door = Scene.GetNode("door");
  5.  
  6. // now 'this' and 'door' reference the same object
  7. if (this == door) Game.Msg("they are the same object");
  8.  


The main advantages of 'this' are that
1) It's less typing :)
2) Multiple objects can share the same script. For example, if your scene contains 10 doors, you can use the same script for all of them, and the 'this' value in each of them will reference one particular door.
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

rice

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 12
    • View Profile
Re: this keyword
« Reply #2 on: December 28, 2013, 04:20:58 PM »

Thank you for the excellent explanation.  That clears up a lot.

I made a new copy of wme_demo and changed the following code in screen_init.script:

Code: WME Script
  1. global OldGuy = Scene.LoadEntity("entities\oldguy\oldguy.entity");
  2. OldGuy.SkipTo(505, 330);
  3.  

...to this:

Code: WME Script
  1. global OldGuy = Scene.LoadEntity("entities\oldguy\oldguy.entity");
  2. OldGuy.SetCursor("sprites\system\cur_wait.sprite");
  3. OldGuy.SkipTo(505, 330);
  4.  

It works, of course, but for reason it wasn't working in my other project.  Even stranger, it seems to have now gone away in both project folders.  I'll never know what I was doing wrong.

Code: WME Script
  1. global OldGuy;
  2. OldGuy.SetCursor("sprites\system\cur_wait.sprite");
  3.  

This lets me change it in other scripts.  Maybe not having the global variable declaration in my script was the problem.  Regardless, I have a better understanding of how WME works now and for that I am grateful.

Quote
Multiple objects can share the same script. For example, if your scene contains 10 doors, you can use the same script for all of them, and the 'this' value in each of them will reference one particular door.

That is absolutely brilliant and not something I'd considered.  Every door in the game can be rigged by creating a new door object and attaching a script to it.  Each door could be assigned its own open/close sprites and you'd just have to insert an OpenClose event to change its state when the player tries to open or close it.  Does that sound right?

Thanks again.

Edit:  Fixed formatting.
« Last Edit: December 28, 2013, 05:26:24 PM by rice »
Logged
 

Page created in 0.072 seconds with 25 queries.