PlayerRef = game.getplayer()... or do it in properties?

Post » Wed Jun 20, 2012 12:54 pm

Which is better (or does it make no difference) for speeding up a script?

playerref = game.getplayer()
(in the script)

or

Actor properties PlayerRef auto
(thus setting it is the proprieties tab)
User avatar
Phillip Hamilton
 
Posts: 3457
Joined: Wed Oct 10, 2007 3:07 pm

Post » Wed Jun 20, 2012 2:27 am

Good question. Though I would use Game.GetPlayer (), I think I prefer it as a Property for ease of change of actors.
Other than that, I'm not sure if using Game.GetPlayer () has much of an overhead compared to it being a Property.
User avatar
Melung Chan
 
Posts: 3340
Joined: Sun Jun 24, 2007 4:15 am

Post » Wed Jun 20, 2012 3:11 pm

Here's a quote from SmkViper, a developer, on this topic:
Game.GetPlayer() is slower then accessing a variable (whether in the object or in a function) because it’s a function call.

A property will depend on a few things. If the property is an auto property and you’re calling it from the same script you defined it in then the compiler optimizes it and makes it just as fast as a variable. If the property isn’t an auto property, or you are accessing the property from a different script, then a function call is involved and the speed will depend on how “busy” the script that contains the property is (because the caller may have to wait).

Also, in the example posted by Rocket, playerRef will not have any value unless you give it one in the CK, or assign it a value somewhere else (like in your OnInit event).

Cipscis
User avatar
Lalla Vu
 
Posts: 3411
Joined: Wed Jul 19, 2006 9:40 am

Post » Wed Jun 20, 2012 12:30 pm

Thanks, Cipscis! And that makes sense, actually - even if that function call simply returns the Player reference. So it's best to save it in a variable or property in your code block and use that variable instead of continuously using Game.GetPlayer ().
User avatar
Imy Davies
 
Posts: 3479
Joined: Fri Jul 14, 2006 6:42 pm

Post » Wed Jun 20, 2012 4:36 pm

um...huh? so which one is better? choice A or B?

playerref = game.getplayer() ; choice A
(in the script)

or

Actor properties PlayerRef auto ; choice B This one is my "guess" based on the fraction of what I understand has been said so far.
(thus setting it is the proprieties tab) ; But I want to be sure that is what you guys are saying.


Speaking of "functions"

in my Quest script called "aadpMainQuest":

Function DamOrKill(Float Damage, Actor Target, Actor Attacker) Global ; I am unsure if Global means it is used globally in other scrips or some other meaning. Wiki is not clear TO ME on this.	if damage >= Target.getav("health")	 attacker.pushactoraway(Target, 7)	 target.Kill(attacker)	   else	   Target.DamageAV("health", damage)	endifendFunction


And then I call it in a magic effect script:

quest property aadpMainQuest autoaadpMainQuest.DamOrKill(Damage, Target, Attacker)

HOWEVER I get this error : DamOrKill is not a function or does not exist when I try to compile my magic effect script.




Thanks, Cipscis! And that makes sense, actually - even if that function call simply returns the Player reference. So it's best to save it in a variable or property in your code block and use that variable instead of continuously using Game.GetPlayer ().
User avatar
vicki kitterman
 
Posts: 3494
Joined: Mon Aug 07, 2006 11:58 am

Post » Wed Jun 20, 2012 9:42 am

Use a local variable -- so choice A.

And nice to see that the compiler is actually an optimizing compiler. Now I have to wonder if it does inline any function calls, like DamageAV and similar.
Edit: Just checked by looking a compiled script, it doesn't -- so its better to use GetActorValue instead of GetAV.

Edit: A function marked global is callable outside the current scope (current file), other languages call this static. But it can't directly access any fields/propertiers from the file scope.
User avatar
Dalley hussain
 
Posts: 3480
Joined: Sun Jun 18, 2006 2:45 am

Post » Wed Jun 20, 2012 1:29 am

Is the aadpMainQuest compiled.
Also, I think once you extend the Quest Script into aadpMainQuest, it is typed as aadpMainQuest.
E.g.:
aadpMainQuest Property mainQuest AutomainQuest.DamOrKill (Damage, Target, Attacker)
DamOrKill () is not found by the compiler because it doesn't exist in Quest but it does in aadpMainQuest.
User avatar
Jessie Butterfield
 
Posts: 3453
Joined: Wed Jun 21, 2006 5:59 pm

Post » Wed Jun 20, 2012 11:03 am

Use a local variable -- so choice A.

And nice to see that the compiler is actually an optimizing compiler. Now I have to wonder if it does inline any function calls, like DamageAV and similar.
Edit: Just checked by looking a compiled script, it doesn't -- so its better to use GetActorValue instead of GetAV.

Edit: A function marked global is callable outside the current scope (current file), other languages call this static. But it can't directly access any fields/propertiers from the file scope.

No, the way I read that quote option B would be faster, because option A is slower than accesing a variable, and using option B it works just like a variable.

Should update this in my .pdf about script runtime.. and should also update this in my own scripts I guess :P
User avatar
Antony Holdsworth
 
Posts: 3387
Joined: Tue May 29, 2007 4:50 am

Post » Wed Jun 20, 2012 12:55 pm

No, what I said is a variable, a local one only accessible inside a specific function/event block and it can be discarded as soon as that block exists.
It doesn't waste any memory, and CPU cycles as far as papyrus is concerned.

And besides, nothing can ever be faster than a local variable which sits on the stack -- not gonna happen ever.
User avatar
james kite
 
Posts: 3460
Joined: Sun Jul 22, 2007 8:52 am

Post » Wed Jun 20, 2012 1:48 am

No, what I said is a variable, a local one only accessible inside a specific function/event block and it can be discarded as soon as that block exists.
It doesn't waste any memory, and CPU cycles as far as papyrus is concerned.

And besides, nothing can ever be faster than a local variable which sits on the stack -- not gonna happen ever.

Don't think you read this quote well :P

Game.GetPlayer() is slower then accessing a variable (whether in the object or in a function) because it’s a function call.

A property will depend on a few things. If the property is an auto property and you’re calling it from the same script you defined it in then the compiler optimizes it and makes it just as fast as a variable. If the property isn’t an auto property, or you are accessing the property from a different script, then a function call is involved and the speed will depend on how “busy” the script that contains the property is (because the caller may have to wait).

Also, in the example posted by Rocket, playerRef will not have any value unless you give it one in the CK, or assign it a value somewhere else (like in your OnInit event).

Even if you use option A, a local variable like this:

Actor playerRef = Game.GetPlayer()

you will have called Game.GetPlayer() once. However, using option B, an auto property, it will be just as fast as a variable, but then Game.GetPlayer() has never been called, so it's faster.
User avatar
Dina Boudreau
 
Posts: 3410
Joined: Thu Jan 04, 2007 10:59 pm

Post » Wed Jun 20, 2012 1:33 pm

Yes, and the same is true for what you are proposing, just that the assignment is hidden, done by the engine implicitly.
Or not at all if the user forgets to bind the property ...
User avatar
Racheal Robertson
 
Posts: 3370
Joined: Thu Aug 16, 2007 6:03 pm

Post » Wed Jun 20, 2012 5:21 am

If the user forgets to bind the properly then the script just won't work, so that's not particularly relevant. Assigning a property value via the Creation Kit will also pretty much certainly be at least as fast as assigning a variable in a script, so I'd also go with an auto property for this as the fastest method.

@spookyfx.com:
Your function hasn't been defined as part of the type Quest, so the compiler can't find it on your aadpMainQuest property as it's of type Quest. You need to either declare it as type aadpMainQuest or cast it to that type before attempting to call that function:
aadpMainQuest property aadpMainQuest autoaadpMainQuest.DamOrKill(Damage, Target, Attacker)
Also, I'd advise being wary of using the same name for an object/property and a script, as this can make it ambiguous as to whether you're calling a global or a non-global variable. I'm not sure how the compiler will handle that, but it will at least make a script more difficult to read.

Cipscis
User avatar
Chloe Mayo
 
Posts: 3404
Joined: Wed Jun 21, 2006 11:59 pm

Post » Wed Jun 20, 2012 2:45 am

Just wondering -- isn't there a page on the wiki that discusses what makes scripts persist in a save, and that one of the causes is properties assigned to references in the CK (but which don't cause a problem if assigned in the script itself). I may be mis-remembering.
User avatar
Matthew Barrows
 
Posts: 3388
Joined: Thu Jun 28, 2007 11:24 pm

Post » Wed Jun 20, 2012 4:08 am

That wouldn't be an issue in this particular case, if true or not. Because the player is essentially a constant anyway.
Which makes the whole idea of binding it via a property even stranger, IMO.

If you insist, name the property player and make it an actor, then the CK can auto-fill it.

If the user forgets to bind the properly then the script just won't work, so that's not particularly relevant.
That's a good one.
User avatar
Deon Knight
 
Posts: 3363
Joined: Thu Sep 13, 2007 1:44 am

Post » Wed Jun 20, 2012 7:01 am

Martigen, I think you're talking about this page? http://www.creationkit.com/Persistence_(Papyrus)

Cipscis
User avatar
Khamaji Taylor
 
Posts: 3437
Joined: Sun Jul 29, 2007 6:15 am

Post » Wed Jun 20, 2012 7:29 am

That wouldn't be an issue in this particular case, if true or not. Because the player is essentially a constant anyway. Which makes the whole idea of binding it via a property even stranger, IMO....
Agreed. I would have thought that Game.GetPlayer () would simply return the player reference.
However, we would never know that or if it does anything else.
And we would not know if accessing the Property we set it to called Game.GetPlayer () anyway. Code in getters and setters sometimes do more than simply return or set their value.
Keep in mind that there are suggestions in the Wiki that Game.GetPlayer () could be a totally different actor.
I.e.: You can set another actor to be your avatar. I just can't remember the Papyrus commands right now.
User avatar
Jani Eayon
 
Posts: 3435
Joined: Sun Mar 25, 2007 12:19 pm

Post » Wed Jun 20, 2012 8:21 am

Thanks to everyone as I leaned a little form each of your posts.

But, well... if you smart proggramer types get a little confused on this "simple" stuff (as demonstrated by the posts on this thread) what chance do people like myself have?

I feel (and I dare say I am not the ONLY one to think this) that the Wiki is written by programmers in programmer "speak" for programmers, making it very difficult for me to follow it with confidence.

for exsample;

This wiki page: http://www.creationkit.com/Function_Reference

this entry:
Function Block

::= *

I read this 5 times (and the entire page) from beginning to end and the pronblem is that the language may mean something to a programmer but it is just random uppercase to me.

What does "::=" suppose to mean? Why is that * following the > ?

In comparison the Oblivion's wiki about functions:


A parameter list consisting of up to ten local variables used to hold arguments passed to the function must follow this keyword enclosed in curly braces; if the function takes no arguments the braces should be empty.

Begin Function {arg1, arg2, ... arg10}
; function body
End

All in "English" and comprehension accessible to us "quasi artist" types that only know enough about programming to be dangerous.

Ironically, Skyrims Wiki is in a language for people that really do not NEED the wiki.

I am not saying this to debase the Wiki nor the Bethesda wiki writers, or somehow magically try to force the Wiki to change overnight (or ever if that be the case) I am only saying this to assure you that when I (and perhaps many others) post a question it is normally going to only be after I have struggled with it for a long long time on my own. I may have actulay READ the answer in the Wiki but did not REALIZE I read the answer.
User avatar
Red Sauce
 
Posts: 3431
Joined: Fri Aug 04, 2006 1:35 pm

Post » Wed Jun 20, 2012 2:38 am

Most of the reference pages, like that one, use notation that's described briefly on this page - http://www.creationkit.com/Language_Reference_Notation. I agree that the wiki is often unclear, though. Good thing it's a wiki :)

Don't worry about the fact that the debate here is about a simple thing. Programming languages always involve some degree of abstraction, and that makes more low-level things like this sometimes subject to discussion. All of the methods discussed here work, which is the most important thing. Any difference between them in terms of efficiency is likely to be tiny, but a lot of us still think it's worth knowing.

Cipscis

EDIT:

@kuertee:
It's possibly worth mentioning again that properties defined with the "auto" keyword are optimised such that they don't use get/set functions like other properties would, like SmkViper mentioned in the quote that I posted.

Cipscis
User avatar
Veronica Flores
 
Posts: 3308
Joined: Mon Sep 11, 2006 5:26 pm

Post » Wed Jun 20, 2012 3:13 pm

but I DO in fact need to write GLOBAL like this right? (see the red text below)


Function DamOrKill(Float Damage, Actor Target, Actor Attacker) Global


Otherwise the other scripts will not be able to work with it correct?
User avatar
Farrah Barry
 
Posts: 3523
Joined: Mon Dec 04, 2006 4:00 pm

Post » Wed Jun 20, 2012 5:42 am

1- OH! THANK YOU ! yes that will be helpful, I will read that right now!

2- I know what you are hinting at but people like me are going to find it difficult to translate to English because we do not know the "other language". And (Concerning Wiki Edits) the Wiki Nazis that blast you for trying to help without being an expert in the subject first makes it an unpleasant experience to "learn by doing" (or even just posting here for discussion about a possible edit to find out if an edit is appropriated).

I know I am not the sharpest tool in the shed for programming, but I do have something a lot of the "smart" programmers do not have, A HUGE amount of time to spend in trial and error on the game (I am freelance ....kind of). I was the first to discover (or at least to say so in the forums) that Oblivion was recording and still playing some scripts in the save file for at least a short time after the mod was removed. Everyone thought I was nuts when I posted that.

I did many edits in the Oblivion Wiki with some very esoteric information I picked up after thousands of hours of trial and error
but only AFTER a few years moding Oblvion and (after) I got to be an authority in my area of expertise of Oblivion combat moding.



1- Most of the reference pages, like that one, use notation that's described briefly on this page - http://www.creationkit.com/Language_Reference_Notation.

2- I agree that the wiki is often unclear, though. Good thing it's a wiki :smile:

Cipscis
User avatar
SEXY QUEEN
 
Posts: 3417
Joined: Mon Aug 13, 2007 7:54 pm

Post » Wed Jun 20, 2012 5:17 am

Thanks agian it did help somewhat, but even this reference is in "greek":

" * Denotes that there should be zero or more of the preceding item "

Zero or more of an Item? :shrug:


Maybe after working with it some more it will all click for me more or less. I certainly know more about programming today than I did when I first stated hard core moding. But it (this new stuff) is a little different than the "Color Basic" language I taught myself 30 years ago. :tongue:

Most of the reference pages, like that one, use notation that's described briefly on this page - http://www.creationkit.com/Language_Reference_Notation. I agree that the wiki is often unclear, though. Good thing it's a wiki
Cipscis
User avatar
sally R
 
Posts: 3503
Joined: Mon Sep 25, 2006 10:34 pm

Post » Wed Jun 20, 2012 6:10 am

If any of the reference pages should use language that's easy to understand, it's that one. I don't think it's been edited yet, though.

That "zero or more" basically means it's the same as the one before it - "one or more" - but it's also optional.

Cipscis

EDIT:

I've made a small edit to that page in the hope of clarifying that definition. Do you think that's clear enough now, or does it still need work?

Cipscis

EDIT:

Just saw your post about the global keyword above. That is the correct way to use it, yes.

Cipscis
User avatar
CArlos BArrera
 
Posts: 3470
Joined: Wed Nov 21, 2007 3:26 am

Post » Wed Jun 20, 2012 1:23 am

but I DO in fact need to write GLOBAL like this right?

No, you don't need to, and actually it's faster to use a global function than a member function.
The calling of either will be the same.

A global function has no connection to the current object, whereas a member function has access to the current instance data (all the variables/properties and 'self')
So as your DamOrKill() function works solely on its' parameters, and uses no functions where self would be implied, you may have made a small speed optimization.

[I say may have, as we've really no way of knowing whether the assembler will notice this and convert a member function to a global one when it can]

On balance, it's probably better off avoiding Global altogether until just before final release - ie when your code is debugged and you do some final performance tweaks.
User avatar
Genocidal Cry
 
Posts: 3357
Joined: Fri Jun 22, 2007 10:02 pm

Post » Wed Jun 20, 2012 3:18 pm

Just to add slightly to tunaisafish's post, global variables are called on types, whereas non-global variables are called on objects.

For example, if I have a type called MyType, and in the MyType script I define a function called MyFunction, then I would call it like this if it were global:
MyType.MyFunction(parameters)
And if it were not global I would call it like this:
MyType Property MyProperty auto...MyProperty.MyFunction(parameters)

Cipscis
User avatar
Daddy Cool!
 
Posts: 3381
Joined: Tue Aug 21, 2007 5:34 pm

Post » Wed Jun 20, 2012 11:26 am

I ate an apple.*

this symbol * means: I could add "And It tasted good." to make this "do more" but I do not NEED to add anything more for the first part to work.

If my simplistic allegory is mostly correct then yes your edit did the trick!


I've made a small edit to that page in the hope of clarifying that definition. Do you think that's clear enough now, or does it still need work?
Cipscis
User avatar
Yama Pi
 
Posts: 3384
Joined: Wed Apr 18, 2007 3:51 am

Next

Return to V - Skyrim