Extending Utility

Post » Tue Jun 19, 2012 6:39 pm

Hi there. I am currently trying to create a script which extends Utility to store additional useful helper functions in and be able to call the standart Utility functions from the same place.

Spoiler

Scriptname ExtendedUtility extends UtilityInt Function GetCurrentMoonphase() GlobalFloat GameTimeFloat GameDaysPassedFloat GameHoursPassedFloat GameMinutesPassedInt PhaseTestGameTime = Parent.GetCurrentGameTime()GameDaysPassed = Math.Floor(GameTime)GameHoursPassed = (GameTime - GameDaysPassed) * 24.0If (GameHoursPassed >= 12.0)  GameDaysPassed += 1EndIfPhaseTest = (GameDaysPassed As Int) % 24If (PhaseTest >= 1 && PhaseTest <= 3)  return 0EndIfIf (PhaseTest >= 4 && PhaseTest <= 6)  return 1EndIfIf (PhaseTest >= 7 && PhaseTest <= 9)  return 2EndIfIf (PhaseTest >= 10 && PhaseTest <= 12)  return 3EndIfIf (PhaseTest >= 13 && PhaseTest <= 15)  return 4EndIfIf (PhaseTest >= 16 && PhaseTest <= 18)  return 5EndIfIf (PhaseTest >= 19 && PhaseTest <= 21)  return 6EndIfIf (PhaseTest >= 22 || PhaseTest == 0)  return 7EndIfEndFunction

The above script though fails to compile because of the call to Parent.GetCurrentGameTime(), telling me the Vriable "Parent" was not declared. If I replace it with Utility.GetCurrentGameTime() it compiles but if I then in another script make a call to ExtendedUtility.GetCurrentGameTime() that other script fails to compile (calling ExtendedUtility.GetCurrentMoonphase() works like a charm though).

My question is why is this? Well quite obviously my script does not inherit the functions from Utility or the extends keyword itself seems to be not working but why?

This is really a shame. I'll do a lot more scripting in future projects I've planned and being able to extend the Utility script would be extremely helpful. Also creating a "Community Utility Script" could be a helpful resource for all of us so we don't need to copy paste or even worse reinvent the wheel all the time.

So I am thankful for any insight on this.
User avatar
Haley Cooper
 
Posts: 3490
Joined: Wed Jun 14, 2006 11:30 am

Post » Tue Jun 19, 2012 6:35 pm

Hmm not even getting a view. Should have chosen a better Title.
User avatar
phillip crookes
 
Posts: 3420
Joined: Wed Jun 27, 2007 1:39 pm

Post » Tue Jun 19, 2012 11:49 am

i'm not positive what your trying to do, but don't think you need to extend Utility

usually you just place "Import Utility" whre you declare your variables in the script and the utility functions will be available

then all you need is Utility.GetCurrentGameTime() to call time function

edit: after rereading i dont think my answer applies to your situation.
User avatar
Mr.Broom30
 
Posts: 3433
Joined: Thu Nov 08, 2007 2:05 pm

Post » Tue Jun 19, 2012 10:41 am

It's failing for Utility as they are all marked as Global functions. So you're not getting the inheritance that you get with Member functions.

http://www.creationkit.com/Script_File_Structure#Imports is probably what you want to use, but I think that's just syntactic sugar to save you typing the filename.
I'm not sure that if you import Extendedutil in another script that it will drag along all the Utility globals too.

I don't think anyone will object to the idea of a community library file though :)
User avatar
Cartoon
 
Posts: 3350
Joined: Mon Jun 25, 2007 4:31 pm

Post » Tue Jun 19, 2012 11:51 am

It's failing for Utility as they are all marked as Global functions. So you're not getting the inheritance that you get with Member functions.

tunaisafish is correct. Since the functions in the Utility script are all global, they don't exist in the "Parent" object but rather in the Utility class itself. This is why Parent.Function() fails but Utility.Function() works fine.

This is also why ExtendedUtility.GetCurrentGameTime() fails. You didn't declare GetCurrentGameTime, and it is not inherited since it is global, so it is not there. To work around this and make it work as you intend it to work, you can create wrappers for all the existing utility functions in your script like this:

Function float GetCurrentGameTime() global    return Utility.GetCurrentGameTime()EndFunction
User avatar
Lyd
 
Posts: 3335
Joined: Sat Aug 26, 2006 2:56 pm

Post » Tue Jun 19, 2012 2:09 pm

Thanks. Sadly this is what I was afraid of allready (but thought it was because the functions are native rather than because they are global).

On the other hand I was not aware of the import keyword and what it does.
User avatar
Dark Mogul
 
Posts: 3438
Joined: Tue Feb 20, 2007 11:51 am

Post » Tue Jun 19, 2012 8:22 am

Yes, http://www.creationkit.com/Script_File_Structure#Imports is your friend here, just be aware of how it handles name collisions (it doesn't) and you'll be fine.

P.S.: You can use Ex as a suffix instead of a long prefix like Extended -- it's a more or less standard way of doing it.
User avatar
Jamie Lee
 
Posts: 3415
Joined: Sun Jun 17, 2007 9:15 am


Return to V - Skyrim