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

Author Topic: Data from a spec oracle?  (Read 1835 times)

speedtek_99

  • General Accounts
  • *
  • Posts: 43
    • View Profile
    • CrazyWeasel Game Studios
Data from a spec oracle?
« on: Mar 05, 13, 05:39:59 PM »

I'm a bit confused to say the least.  I've created a spec oracle to create and store quest data.  The spec oracle itself is working as expected, I can open it and populate it.

I understand that there are a number of different ways to pull down the data, but I cannot seem to find a simple concrete example, I've scoured the wiki and the forums.

What I want to do is call up the oracle by speckey, pull a chunk of data and populate a GUI element with it.  The GUI is created, I can populate a label through script, but I'm just not sure how to access field values from the spec.

Any help with an example would be great.  Thanks!
Logged
Michael Gough
Lead Developer
CrazyWeasel Game Studios
http://www.cwgstudios.com

FI-ScottZ

  • General Accounts
  • *
  • Posts: 1407
    • View Profile
    • Forever Interactive, Inc.
Re: Data from a spec oracle?
« Reply #1 on: Mar 05, 13, 07:03:12 PM »

First, you'd use the $SPECORACLEUTILS system node to get the oracle by name.  Then use the oracle to get the spec.  For the client side, as would be needed for GUI work, it is recommended to use the SpecOracle method RequestSpecByKey() rather than GetSpecByKey(), as GetSpecByKey() might not return a node, depending on whether it is cached on the client and up-to-date.

The catch is that RequestSpecByKey() is asynchronous, so it does not return a value immediately. Instead you use the oracle to create a listener and then pass that listener to the Request call along with the desired key.  The callback is either to a method or function, depending on the type of listener you used.

For instance,
Code: [Select]
  var listener = oracle.CreateScriptListener( SYSTEM.EXEC.THISSCRIPT, false )
  oracle.RequestSpecByKey(specKey, listener)

will callback to a shared function called EventRaisedNotify in the executing script.  It has this as form:
Code: [Select]
shared function EventRaisedNotify(obsSubject as NodeRef of Class ObsSubject, listener as NodeRef of Class ObsListener, data as NodeRef )
For spec requests, data will be the noderef of the spec.  Technically, the obsSubject is, as well, since the spec is the node you are listening to, so in this case you could cast either one to your spec class.

Important: You will want to destroy the listener in this function when done, else you may continue to receive callbacks.
Code: [Select]
listener.deleteListener()
You would continue whatever functionality you were going to do with the spec from within this function, either directly or by calling other functions.  If you wish to pass data to this callback from the place of the request, it is common to glom a class onto the listener node to add fields and put data in that before the request call which can be later read inside of the callback function.

See
http://hewiki.heroengine.com/wiki/Observer_Pattern
and
http://hewiki.heroengine.com/wiki/Spec_System_-_Basic_Usage
for more details.
« Last Edit: Mar 05, 13, 07:10:58 PM by ScottZarnke »
Logged
Scott Zarnke
Lead Programmer, Visions of Zosimos
CTO, Forever Interactive, Inc.

speedtek_99

  • General Accounts
  • *
  • Posts: 43
    • View Profile
    • CrazyWeasel Game Studios
Re: Data from a spec oracle?
« Reply #2 on: Mar 05, 13, 07:38:28 PM »

Ah ok, it makes much more sense now.  I was ignoring the listener side of things. 

Going to give it a go and see how it works out now!

Thanks!
Logged
Michael Gough
Lead Developer
CrazyWeasel Game Studios
http://www.cwgstudios.com

speedtek_99

  • General Accounts
  • *
  • Posts: 43
    • View Profile
    • CrazyWeasel Game Studios
Re: Data from a spec oracle?
« Reply #3 on: Mar 06, 13, 09:11:39 PM »

That did the trick, still adjusting from the c++/java world to HSL, but with every victory, things are more understandable.

Thanks again!
Logged
Michael Gough
Lead Developer
CrazyWeasel Game Studios
http://www.cwgstudios.com