Saving actor .self attributes
OrvilFox
Member Posts: 67
I have many actors, each one generate a random number and store them on themselves (Not a . game nor .scene attribute, because they are too many, I'm not creating 100 attributes to store every single one of those unique random numbers), and I want that every time player change scene and change back, the number could remain the same, which means I'll somewhat need a "save function" because GameSalad clear everything if scene changed.
Saving actor .self attributes, how do I do this?
Comments
You'll need to use a table to store those values. There's no way to save an actor's self attribute value except with a Save Attribute behavior and a Load Attribute behavior. The whole point of tables is to avoid needing 100 of each of those behaviors.
So, you'll need a single table with 100 rows. Store each actor's random value in a table row. How do you do that? You give each actor a unique value. Create an integer attribute called self.row. Place each actor in the scene and then double-click on it to edit its instance attribute values. Double-click on the 0 value next to self.row and type in 1 or 2 or 3, etc. all the way up to 100. [By the way, if you're able to spawn the actors instead, it's MUCH easier to assign them all unique numbers by incrementing a game attribute by 1 before each actor is spawned in a Loop Behavior.]
Use a Loop Over Table behavior to generate all of the 100 random numbers in table cells... Change Table Value row:self.counter col:1 value:random(1,50). When the loop is completed, Save Table. You'll need a "lock" attribute to keep this from happening every time you reset/change the scene. One way to do this is to change the value of an attribute (e.g. game.generatedValues, a boolean) and then use a Save Attribute behavior along with a Load Attribute behavior to store and retrieve the value of it. Place the Loop Over Table behavior inside of a rule that says When attribute game.generatedValues is false.
Then display the values in a Display Text behavior in each actor using tableCellValue(tableName,self.row,1).
*Obviously, you can use any random max you want... I just chose 50 as an example.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
First, I'll say @tatiang is absolutely correct that tables are the right way to go here. That said, you can quite easily save self attributes. Example is attached.
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
Thank you so much
Well, I do agree using tables is a more organized and better way.
Those actors are actually not that much.. way lesser than 100.
But I want to know by using the method you provided, compare to the tables, what are the differences since they all do the same?
And I'm interested in your expression ("text"..self.id), is ("") and the two dots necessary? Can it be three dots? What does it mean?
Very little really. Saving the attributes might be slightly more efficient, but it'll be a lot more hassle to organise if you're looking at more than about 5 actors. Definitely go with tables if you're saving more than a handful.
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
When building a function in the expression and you want something to be recognized as text, it must be closed in quotes "text" this is a common basic coding norm across all computer languages. Then when you want to display some text along with the value of a variable you need to periods to seperate them. This again is computer code, for GS it's LUA. Example: "score"..game.score if you wanted a longer string just keep the same method. "Score"..gamescore.."more text"...game.whatever
Search the forum using the term display text expressions.
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS