maxarturo Posted August 15, 2019 Share Posted August 15, 2019 (edited) Hi everybody. I'm in the process of optimizing - simplifying all my scripts ( for better performance ), and in this particular script i want the " Function " ( Function OneTimeFUNC() ) to run only ONCE. * There are around 100 objects in the Display room, so that's why i want a more simpler version. The current script is working fine, but i'm browsing for a better and simplier version of it from a more expirienced scripter ( if there is one, i mean script version ). I'm on vacations and i'm doing this in my tablet, so i can't test them and experiment for a better altenative. This is one of my " Display Item Script " : Scriptname aXMDvaultDisplayMISC01 extends ObjectReference {Misc Items Script for the Display with an Increment Counter function} ObjectReference Property StaticMisc Auto {Link REF the static display object to enable/disable} MiscObject Property MiscToPlace Auto {The Misc Item to remove from inventory and place in the display} Message Property NoItemMSG auto {The message to show if the player doesn't have the ITEM in their inventory} Message Property OnPlacedItemMSG auto {Messsage to show when item is placed} Bool Property isPlaced = false Auto Hidden int count Event OnActivate(ObjectReference akActivator) If akActivator == Game.GetPlayer() if (isPlaced == FALSE) if akActivator.getItemCount(MiscToPlace) >= 1 blockActivation() count = count + 1 isPlaced = TRUE StaticMisc.enable() akActivator.RemoveItem(MiscToPlace, 1) OneTimeFUNC() blockActivation(FALSE) else NoItemMSG.show() endif else isPlaced = FALSE blockActivation() StaticMisc.disable() akActivator.AddItem(MiscToPlace, 1) blockActivation(FALSE) endif EndIf EndEvent Function OneTimeFUNC() If (count == 1) OnPlacedItemMSG.show() (Self.GetLinkedRef() As defaultCounter).Increment() Else Return EndIf ENDFUNCTION Many thanks in advance and i wish this summer to be the best you ever had !. Edited August 15, 2019 by maxarturo Link to comment Share on other sites More sharing options...
IsharaMeradin Posted August 15, 2019 Share Posted August 15, 2019 Calling the function from within a block that will always executes means that the function will always be called. You could move the check for count to equal 1 outside of the function. At least that way, when the block gets to that condition it will skip it since count will no longer equal 1 after the first time. But if you do that, might as well skip the function all together and put the other lines inside the condition check for count to equal 1. Link to comment Share on other sites More sharing options...
maxarturo Posted August 15, 2019 Author Share Posted August 15, 2019 (edited) Thanks IsharaMeradin for the response. You mean : if akActivator.getItemCount(MiscToPlace) >= 1 blockActivation() count = count + 1 isPlaced = TRUE StaticMisc.enable() akActivator.RemoveItem(MiscToPlace, 1) If (count == 1) OnPlacedItemMSG.show() (Self.GetLinkedRef() As defaultCounter).Increment() EndIf blockActivation(FALSE) else NoItemMSG.show() endif EDIT :Now that i thought of it a little bit more, your suggestion is completely logical !.Stupid of me for not thinking of it to begin with !... Thanks a lot !. Edited August 15, 2019 by maxarturo Link to comment Share on other sites More sharing options...
ReDragon2013 Posted August 16, 2019 Share Posted August 16, 2019 (edited) There are different ways to make things more simple, next is my proposal. aXMDvaultDisplayMISC01 Scriptname aXMDvaultDisplayMISC01 extends ObjectReference {Misc Items Script for the Display with an Increment Counter function} ; https://forums.nexusmods.com/index.php?/topic/7904573-script-alternative-simpler-script-version/ Message PROPERTY NoItemMSG auto ; {The message to show if the player does not have the ITEM in his inventory} Message PROPERTY OnPlacedItemMSG auto ; {Messsage to show when item has been placed} MiscObject PROPERTY MiscToPlace auto ; {The Misc Item to remove from inventory and will be placed in display} ObjectReference PROPERTY StaticMisc auto ; {LinkedREF (the static display object) to enable/disable} Bool PROPERTY isPlaced auto Hidden ; [default=False] Int iCount ; [default=0] ; -- EVENTs -- 2 EVENT OnActivate(ObjectReference akActionRef) IF (akActionRef == Game.GetPlayer() as ObjectReference) IF ( isPlaced ) myF_Action(akActionRef, bAdd=TRUE) ELSE myF_Action(akActionRef, False) ENDIF ENDIF ENDEVENT EVENT OnUpdateGameTime() OnPlacedItemMSG.show() defaultCounter ps = self.GetLinkedRef() as defaultCounter ; ps -> pointer to script IF ( ps ) ps.Increment() ENDIF ENDEVENT ; -- FUNCTION -- ; https://www.creationkit.com/index.php?title=RemoveItem_-_ObjectReference ;----------------------------------------------------- FUNCTION myF_Action(ObjectReference player, Bool bAdd) ;----------------------------------------------------- IF ( bAdd ) self.BlockActivation(TRUE) StaticMisc.Disable() ; disable linkedRef player.AddItem(MiscToPlace as Form, 1) ; give player back an item from display self.BlockActivation(False) isPlaced = False ; *** RETURN ; - STOP - /1 ENDIF ;--------------------- IF (player.GetItemCount(MiscToPlace as Form) < 1) NoItemMSG.show() RETURN ; - STOP - /2 player does not have the right item inside the inventory ENDIF ;--------------------- self.BlockActivation(TRUE) StaticMisc.Enable() player.RemoveItem(MiscToPlace as Form, 1) ; remove item from players inventory iCount = iCount + 1 IF (iCount == 1) RegisterForSingleUpdateGameTime(0.0) ; open another thread immediately to increment the counter ENDIF self.BlockActivation(False) isPlaced = TRUE ; *T* ENDFUNCTION Edited August 16, 2019 by ReDragon2013 Link to comment Share on other sites More sharing options...
maxarturo Posted August 16, 2019 Author Share Posted August 16, 2019 (edited) ReDragon your scripting style is so interesting to me that even if i don't end up using them, i study the hell out of them !. * I've completely abandon the use of OnUpdateGameTime() or RegisterForSingleUpdate() and equivalents ( only for the Display scripts ). Thanks for your post, i'll for sure study this one too !. Edited August 16, 2019 by maxarturo Link to comment Share on other sites More sharing options...
PeterMartyr Posted August 17, 2019 Share Posted August 17, 2019 @ maxarturo A good script and a elegant script are two completely different things, a script doesn't need to be elegant to be good. Swapping a Function (CPU usage) for a Variable (RAM usage) is potato - potata, whether it is good or bad totally depends on the environment it is running in, which is an unknown factor? So NO-ONE can say it is better, cos it might worst too. If your code is working as you intended, it is fine. There are courses to teach you how to code. You will learn about stress & load testing.... and learn how to make the correct choices. Which must be assess on individual basis in an Generic Shittest Possible Environment. Example1 If the System because of Skyrims Mods is under heavy RAM usage the Function is better... Example2 If the System because of Skyrim Mods is under heavy CPU usage the Variable is better..... My question is.... What is the System? Link to comment Share on other sites More sharing options...
PeterMartyr Posted August 17, 2019 Share Posted August 17, 2019 What came first? The chicken or the egg? The Rooster Link to comment Share on other sites More sharing options...
maxarturo Posted August 17, 2019 Author Share Posted August 17, 2019 Thanks PeterMartyr for your response. To begin with, i should mention that i'm a self teach scripter and i'm too old to go to school again, plus it doesn't interest me to attend code courses, at the end of the day for me it's just a game, and since it doesn't bring home the bacon, that's where it ends !. Making this occasionally posts in this Forum is the way for me to advance a little bit more my scripting and maybe learn something new from someone more experienced or someone that it's his actual job or studied this particular subject. Although i'm at a very good level, at least i can script whatever i have in mind with no problems or help. For me all of this is just a hobby... Thanks you very much for your input !. Link to comment Share on other sites More sharing options...
ReDragon2013 Posted August 18, 2019 Share Posted August 18, 2019 I had a look into the script "defaultCounter.psc" and maybe next script is more useful. Keep in mind I renamed the script ! aXMDvaultDisplayMISC01a Scriptname aXMDvaultDisplayMISC01a extends ObjectReference {Misc Items Script for the Display with an Increment Counter function} ; https://forums.nexusmods.com/index.php?/topic/7904573-script-alternative-simpler-script-version/ Message PROPERTY NoItemMSG auto ; {The message to show if the player does not have the ITEM in his inventory} Message PROPERTY OnPlacedItemMSG auto ; {Messsage to show when item has been placed} MiscObject PROPERTY MiscToPlace auto ; {The Misc Item to remove from inventory and will be placed in display} ;ObjectReference PROPERTY StaticMisc auto ; {LinkedREF (the static display object) to enable/disable} Bool PROPERTY isPlaced auto Hidden ; [default=False] ; -- EVENTs -- EVENT OnActivate(ObjectReference akActionRef) IF (akActionRef == Game.GetPlayer() as ObjectReference) IF ( isPlaced ) myF_Action(akActionRef, False) ELSE myF_Action(akActionRef, bRemove=TRUE) ENDIF ENDIF ENDEVENT ;======================== state Busy ;========= EVENT OnActivate(ObjectReference akActionRef) ; nothing here, I am busy with counter ENDEVENT EVENT OnUpdateGameTime() self.BlockActivation(TRUE) defaultCounter ps = self.GetLinkedRef() as defaultCounter ; ps -> pointer to script IF ( ps ) IF ( isPlaced ) ps.Decrement() isPlaced = False ; *** ELSE OnPlacedItemMSG.show() ps.Increment() isPlaced = TRUE ; *T* ENDIF ENDIF self.BlockActivation(False) gotoState("") ; ### STATE ### ENDEVENT ;======= endState ; -- FUNCTION -- ; https://www.creationkit.com/index.php?title=RemoveItem_-_ObjectReference ;-------------------------------------------------------- FUNCTION myF_Action(ObjectReference player, Bool bRemove) ;-------------------------------------------------------- IF (bRemove) && (player.GetItemCount(MiscToPlace as Form) < 1) NoItemMSG.show() RETURN ; - STOP - /1 player does not have the right display item (within inventory) ENDIF ;----------------------- objectReference oRef = self.GetLinkedRef() ; StaticMisc IF ( oRef ) ELSE Debug.Notification("Missing linkedRef..") Debug.Trace("OnActivate() - missing linkedRef detected for " +self) RETURN ; - STOP - /2 ENDIF ;--------------------- gotoState("Busy") ; ### STATE ### RegisterForSingleUpdateGameTime(0.0) ; open another thread immediately to run the counter script IF ( bRemove ) oRef.Enable() ; enable player.RemoveItem(MiscToPlace as Form, 1) ; remove item from players inventory ELSE oRef.Disable() ; disable player.AddItem(MiscToPlace as Form, 1) ; give player back an item from display ENDIF ENDFUNCTION Link to comment Share on other sites More sharing options...
maxarturo Posted August 19, 2019 Author Share Posted August 19, 2019 Thanks for this one too ReDragon !, i'll study it. But i'm not going to use the " defaultCounter ", it doesn't serves my purpose, i'll have to make a new " Counter Script " when the times comes, it'll be the last script i'll make and the last addition to the mod ( i still haven't deside what the final reward will be and how will be implemented ). Link to comment Share on other sites More sharing options...
Recommended Posts