mauricimoo 2 Posted July 30, 2016 hello, i have 2 questions. the first question is: for example if i want to cook salmon, i use the raw salmon on a fire and then in the messagebox an image appears, where you can select to "cook all" or "cook 5" etc. where can i find the part of the api about this / what is the code to put in the script to "cook all"? second question: if i have a piece of code like this: if (getCurrentState().equals(-1) for longer than 1,5-2,5 seconds) {.....} what code can i use to replace the "for longer than 1,5-2,5 seconds" part? and if that is not possible, could you think of an alternative? thanks in advance
Hopewelljnj 46 Posted July 30, 2016 hello, i have 2 questions. the first question is: for example if i want to cook salmon, i use the raw salmon on a fire and then in the messagebox an image appears, where you can select to "cook all" or "cook 5" etc. where can i find the part of the api about this / what is the code to put in the script to "cook all"? second question: if i have a piece of code like this: if (getCurrentState().equals(-1) for longer than 1,5-2,5 seconds) {.....} what code can i use to replace the "for longer than 1,5-2,5 seconds" part? and if that is not possible, could you think of an alternative? thanks in advance A. Widgets B. Timer stateTimer = new Timer(); public boolean checkState() { if(stateTimer.elapsed() > (the time in ms)) { return true; } else { if(getCurrentState() != 1) { stateTimer = new Timer(); } } return false; }
Cardozz 46 Posted July 31, 2016 A. Widgets B. Timer stateTimer = new Timer(); public boolean checkState() { if(stateTimer.elapsed() > (the time in ms)) { return true; } else { if(getCurrentState() != 1) { stateTimer = new Timer(); } } return false; } Creating a new timer each time you call the method will overload your memory. Create the timer on startup or as a global variable and reset the timer each time you call the method so you don't create 100 timers
Hopewelljnj 46 Posted July 31, 2016 Creating a new timer each time you call the method will overload your memory. Create the timer on startup or as a global variable and reset the timer each time you call the method so you don't create 100 timers The new Timer() call actually replaces the old one with a new one effectively resetting the time. I don't know of another way to do this with db's Timer class.
TechTaco 23 Posted July 31, 2016 Creating a new timer each time you call the method will overload your memory. Create the timer on startup or as a global variable and reset the timer each time you call the method so you don't create 100 timers Doesn't java have a garbage collector that gets rid of objects that aren't being used by the program anymore? So if you have 100 timers and are only using 1, it should get rid of the other 99. Edit: The new Timer() call actually replaces the old one with a new one effectively resetting the time. I don't know of another way to do this with db's Timer class. Didn't know that.
Hopewelljnj 46 Posted July 31, 2016 Doesn't java have a garbage collector that gets rid of objects that aren't being used by the program anymore? So if you have 100 timers and are only using 1, it should get rid of the other 99. Edit: Didn't know that. Well think about it logically. We only told java to give it memory once --- Timer stateTimer new Timer() just fills that spot therefore multiple of them cannot exist at the same time in memory as there is only space for 1
TechTaco 23 Posted July 31, 2016 Well think about it logically. We only told java to give it memory once --- Timer stateTimer new Timer() just fills that spot therefore multiple of them cannot exist at the same time in memory as there is only space for 1 That actually makes perfect sense, thank you for the clarification! I had always thought that when you created a new object it would be given it's own allocated memory until disposed by the garbage collector when not being used anymore.
raptor 10 Posted July 31, 2016 @Hopewelljnj, @TechTaco, and @Cardozz, you are all partially correct (but also partially incorrect ). I'll walk through relevant lines and explain what is happening, with references. Timer stateTimer = new Timer(); Line 1 accomplishes two things: it declares a local variable, and instantiates a class. We can break it up and examine each part: Timer stateTimer; stateTimer = new Timer(); Line 2 isolates the local variable declaration, and line 3 isolates the instantiation of the Timer class.Line 2 allocates some memory. For reference types (e.g. not primitives), this is usually 32 or 64 bits. This memory is used to store a reference to an object. In the example @Hopewelljnj gave, this is a one-time allocation.Line 3 also allocates memory. "The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory." This tells us that constantly creating new Timers will use more memory, even if they are being assigned to an existing variable. However, this is where Java's garbage collection comes into play. "Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects." We don't have to worry about unused objects in Java - we trust that the garbage collector will properly deallocate their memory. That doesn't mean we should needlessly use memory though! Java garbage collection is not continuous, so an unused object might exist in memory for a relatively long time. Good practice is to not create unnecessary objects. Just use stateTimer.reset() instead of creating a new Timer.
Hopewelljnj 46 Posted July 31, 2016 @Hopewelljnj, @TechTaco, and @Cardozz, you are all partially correct (but also partially incorrect ). I'll walk through relevant lines and explain what is happening, with references. Timer stateTimer = new Timer();Line 1 accomplishes two things: it declares a local variable, and instantiates a class. We can break it up and examine each part: Timer stateTimer; stateTimer = new Timer(); Line 2 isolates the local variable declaration, and line 3 isolates the instantiation of the Timer class.Line 2 allocates some memory. For reference types (e.g. not primitives), this is usually 32 or 64 bits. This memory is used to store a reference to an object. In the example @Hopewelljnj gave, this is a one-time allocation. Line 3 also allocates memory. "The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory." This tells us that constantly creating new Timers will use more memory, even if they are being assigned to an existing variable. However, this is where Java's garbage collection comes into play. "Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects." We don't have to worry about unused objects in Java - we trust that the garbage collector will properly deallocate their memory. That doesn't mean we should needlessly use memory though! Java garbage collection is not continuous, so an unused object might exist in memory for a relatively long time. Good practice is to not create unnecessary objects. Just use stateTimer.reset() instead of creating a new Timer. Theres the function I was referring to that I didnt know existed . Anyhow declaring new ones works just as well
Mad 86 Posted July 31, 2016 A. Widgets B. Timer stateTimer = new Timer(); public boolean checkState() { if(stateTimer.elapsed() > (the time in ms)) { return true; } else { if(getCurrentState() != 1) { stateTimer = new Timer(); } } return false; } Nice else statement on a return
Recommended Posts
Archived
This topic is now archived and is closed to further replies.