More efficient way of coding this..
3absh
Member Posts: 601
Hey guys,
I'm trying to make my code as efficient as possible for better performance.
My code is basically this
Every 100 days, change self image of actor
This is my approach to it:
Is there a better way to it? I have around 20 images, I don't want the game to go through the process of checking all 20 images for every execution.
Comments
Should I use the otherwise here? to make my code more efficient.
Here is what we know. When you use a change image behavior like that all those images are loaded on start of the scene, even if preload is unchecked. It is best to use a change attribute and call the image by name. This will cause the images not to preload and take up memory. Also using otherwise is more efficient as the scan will skip anything buried in an otherwise. With seperate rule they will all be scanned for their state.
I see your naming conventions for images are long. I tend to use abbreviations for my file names. Aka, sgwg this allows me to call images et.. Without typing out all this language. This weeks guru broadcast I'm going to show and talk about building solid code. I'm going to talk about the importance of code order et.., these things are all incremental. All the small things compound when you have lots of code. A nickel isn't heavy but if I fill a bag with 2000 nickels it get's really heavy. We need to see our code building this way.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
If there's a 100 days between each band/rule how about doing something like this:
Rule: self.Activated = false
..Constrain Attribute: self.Image = "ghost_image_"..floor(scene.Backg/100)..".png"
I don't know what your scene.Backg... was referring too in your screenshots so I cut it short in the example above. Replace floor(scene.Backg/100) with whatever you're using in your rule in your screenshot.
Then create an image for each set of 100 i.e.
ghost_image_1.png (for the first 100)
ghost_image_2.png (for the next 100)
ghost_image_3.png (etc... etc..)
Then it's just the one rule which works for as many 100 day bands that you need.
I also noticed in your screenshot that the images in each rule was the same.
Exactly! You took it a big step forward. I didn't really look at the conditions. Kevin showed exactly what we have been saying merging techniques to slim down the code.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Maybe say it a little nicer! :P
I just wanted to add that floor(day number/100) will give the value 0 if the day number is less than 100 so either create an image with 0 in the file name for when the day counter is less than 100 or use ceil instead which would return a value of 1.
That's why I do my live stream text doesn't always show proper emotion.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Thanks, guys, I went with the otherwise method
@KevinCross
I'm not sure if I understand correctly, is it something like this:
Constrain Attribute: self.Image = "ghost_image_"
and then after "ghost_image_" I rename the images so that it recalls each image for every change in Daynumber?
If that's what you meant it would be really cool and it would abridge my code to a single line.
BTW I'm going to change my images but I'm figuring out a better code first.
I'm assuming the otherwise method is more efficient as it is executed only once every 100 days in the game (about 5 hours of gameplay) rather than the constrain which is constantly executed?
I might be wrong though
Maybe I went with Constrain to cater for instances where the player is playing during the crossover of day 99 and day 100. Change Attribute would work just as well. The rule should only ever fire whenever the self.Activated variable changes or every time the game is opened or scene is refreshed etc. assuming self.Activated is false at those times.
Excellent, if that's the case then your method is definitely more efficient than going through 10 "otherwises".
Yes Kevin's method boils it down to one behavior. This is the same method used for custom font scores.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS