Some questions about functions

Post » Sun Nov 18, 2012 11:36 pm

I'm not that experienced with object-oriented programming, and the structure is sometimes confusing for me, so I have some questions for my mod. I have read some stuff previously and am still unsure about it.

1) I'd really like to have some functions that I can just call on an actor, from any script, as if they were in Actor.psc. I do not want to edit actor.psc for obvious reasons, though. As an example, I have these functions in an activemagiceffect script right now:
Spoiler

float Function getAttribute(string attribute)	if		( attribute = "Strength" )		return thisActor.getAV("FavorActive")	elseif ( attribute = "Endurance" )		return thisActor.getAV("FavorPointsBonus")	elseif ( attribute = "Speed" )		return thisActor.getAV("FavorsPerDay")	elseif ( attribute = "Agility" )		return thisActor.getAV("FavorsPerDayTimer")	elseif ( attribute = "Intelligence" )		return thisActor.getAV("BypassVendorStolenCheck")	elseif ( attribute = "Willpower" )		return thisActor.getAV("BypassVendorKeywordCheck")	elseif ( attribute = "Personality" )		return thisActor.getAV("Infamy")	else		return none	endifEndfunctionFunction setAttribute(string attribute, float value)	if		( attribute == "Strength" )		thisActor.setAV("FavorActive", value)	elseif ( attribute == "Endurance" )		thisActor.setAV("FavorPointsBonus", value)	elseif ( attribute == "Speed" )		thisActor.setAV("FavorsPerDay", value)	elseif ( attribute == "Agility" )		thisActor.setAV("FavorsPerDayTimer", value)	elseif ( attribute == "Intelligence" )		thisActor.setAV("BypassVendorStolenCheck", value)	elseif ( attribute == "Willpower" )		thisActor.setAV("BypassVendorKeywordCheck", value)	elseif ( attribute == "Personality" )		thisActor.setAV("Infamy", value)	endifEndfunction

Instead of copying this to every script and setting "thisActor" to the actor, I'd rather have it accessible from any script without pasting it in. So my question is: if I add these functions to a script that extends actor (deleting "thisActor" of course), and attach it to every actor in my mod, will I be able to just do something like:
someActor.getAttribute("Strength")
Without importing or pasting the functions in every activemagiceffect or other script?

I'd like to just move all of my actor-related functions from my other scripts into a single one, and call them like this, but before I make that change I want to know if it would work or not, and if I would need to write it any differently.

Lastly, my mod is basically a total conversion, so I'm not worried about compatiblity, but I also want to know if, in this case, there are problems with attaching a script directly to an actor? I mean, I could imagine this working if I attach the script directly to the actor, but so far I've been attaching scripts to actors with magic effects because I've read that it's bad to attach them directly, and have experienced some issues myself like that if I save a game, change the script, and open it again for example. But I'm just wondering if there are problems with attaching a script directly for a total conversion mod, with finalized scripts?

(P.S. if you have some optimization suggestion for those attribute functions, go ahead.)
User avatar
victoria johnstone
 
Posts: 3424
Joined: Sat Oct 14, 2006 9:56 am

Post » Sun Nov 18, 2012 7:00 pm

I'd recommend using http://www.creationkit.com/Function_Reference#Function_Header to accomplish this. Global functions aren't called on a specific object, but you could simply add an extra parameter through which you can pass the actor you want it to use:
float Function getAttribute(Actor thisActor, string attribute) global
A script containing global functions doesn't need to be attached to an object, so you could place them in an external "library" script that sets up all these functions for you to use.

This should allow you to effectively call your functions on any actor, albeit with a slight semantic difference, without having to alter the actors or the Actor script.

Cipscis

EDIT:

Just remember that, when calling global functions from other scripts, you need to prefix their name with the containing script. So, for example, if your library script is named Library74, you'd call it like this:
Library74.getAttribute(myActor, "Strength")

Alternately you can import the script at the top in order to omit this syntax later in the script:
Import Library74

Cipscis
User avatar
Doniesha World
 
Posts: 3437
Joined: Sun Jan 07, 2007 5:12 pm

Post » Sun Nov 18, 2012 5:09 pm

Thanks for the quick reply. It seems like your suggestion is what I'm looking for.
User avatar
Naomi Lastname
 
Posts: 3390
Joined: Mon Sep 25, 2006 9:21 am


Return to V - Skyrim