# Random Actor Question

Member, PRO Posts: 31

Hi

I have a game with 32 actors, that when the game starts one of these random 32 actors are shown, and it will be random each time. During gameplay this actor gets destroyed and should be replaced with one of the other 31 actors, which should also be random, I have created a table with all the actor names and boolean integer values possible, After research I realise it isn't possible to load an actor from a table value, so this way is out, I was going to use the tables and then remove each row when that actor was destroyed to keep it clean coding. But now im stuck and really dont know the best way forward.

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,944
edited August 2018

Yep, you'd be creating 32 rules that say if random(1,32)=1 then spawn Actor1; if random(1,32)=2 then spawn Actor2; etc.

Another way to think of this is to create an attribute in each actor called something like self.ID and manually change each actor's attribute value to a sequential value (1.. 2.. 3.. all the way up to 32).

Use a table with values from 1 to 32, one value in each row and use the method (shown in many online tutorials/demos) to choose a row, assign it to an attribute such as game.ActorNumber, and then delete that row before choosing the next.

Then have a rule in each actor that says if self.ID = game.ActorNumber then move the actor from an off-screen position to an on-screen position. Or depending on how your game works, maybe just change self.color.Alpha from 0 to 1. There are different ways to go about making an actor appear but perhaps one of those will work for your project.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,944

I'm assuming that your 32 actors have significantly different rulesets. If that's not a correct assumption, forget what I said and just use a single prototype actor with 32 different images and change the image according to the random value that's chosen.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member, BASIC Posts: 39
edited August 2018

I'm on GameSalad for only a few weeks, so I'm pretty sure there are better ways to do this, but one way I could think of is this:

You must have a rule saying why and when an actor gets destroyed. Add a boolean game.attribute.ActorDestroyed that turns to true when this rule's conditions are valid and put it above the [Destroy]-line. Also remove the current actor from the Table you're using before destroying it. What I can think of is adding a text column to your TableActors and put the exact name of each of your actors in the row your actor's information is in.

Remove Row from Table:TableActors
At Index: tableSearch(game.TableActors, self.name, "col", 1, 1, tableRowCount(game.TableActors), "exact")
This removes the row of the actor that gets destroyed in column 1. If you put the exact names of the actors in another column, change the 1 right after "col" to the column you put the names in.

Then.. create an index/integer game-attribute, let's call it game.attribute.SpawnActor. Create an actor with 32 similar rules inside it and put it off screen, but in the scene.
Rule 1:
if game.attribute.SpawnActor = 1
do Spawn Actor: 1
You can replace the 1 for 2, 3 and so on.

At the top of these rules, should be a rule that says:
if game.attribute.ActorDestroyed = true
do
Change Attribute game.attribute.SpawnActor to random(1, tableRowCount(game.TableActors))

Right, I already said this probably isn't the best practice. But I thought I'd just think along here

• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,944

@JB makin' a game

Actually, I didn't think of spawning the new actor within the destroy rule for the existing actor. That's a much better plan.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Member, BASIC Posts: 39

That's nice to hear, tatiang.

There's a flaw in what I wrote, though. Because game.attribute.SpawnActor will have a decreasing value range (first 1 to 31, then 1 to 30, etc.). So just writing the rule:
if game.attribute.SpawnActor = 1
do Spawn Actor: 1
You can replace the 1 for 2, 3 and so on.

, won't spawn all actors. This is fixable, though. Probably by setting attributes to tableCellValues from somewhere.
No time to think it through now. Perhaps you'll be getting somewheree with this.

• Member, BASIC Posts: 39
edited August 2018

The first screenshot shows the table in which your actors are. Any content of that table is used to explain the use of the column. You should have a copy of this table, so at the end of the game, you can copy the [Copy]-table to the [Runtime]-table, so you can use it again.
The second screenshot shows the actor that contains the 32 similar rules.
The rule: "if game.actorNumber = 1, Spawn Actor 1" should be repeated 32 times for each of your 32 actors, changing the 1 to 2 and spawning the corresponding actor.
The third screenshot shows one of your 32 actors. They should all contain this same rule, so that's just a matter of copying it.

And of course you should create the game-attributes: actorDestroyed (boolean), getRandomRow (integer/index), actorNumber (integer/index).

I believe this should work…
Edit: I created a boolean column (3) in the table, but I didn't use it after all, so you don't really need that.

• Member, PRO Posts: 31

Thanks for the helpful information, I am working on it now. Just a few questions, if you can spare another bit of time please?

At what point in the demo would actorDestroyed = False?

I can't work it out, initialised at the start of the game?
set after the destroyed command on the actor?

• Member, PRO Posts: 31

game.actorNumber isn't updating after the loaded actor is destroyed.

• Member, BASIC Posts: 39

game.actorDestroyed is false by default, so at the start of the game as well. And you're right, as it is, once an actor is destroyed it sets the attribute to true and keeps it like that.
As I mentioned, I'm new to GameSalad, so I've found tons of stuff out by just trying things out and trying to understand why the things happening happen. Tutorials are helpful, but solving problems like this is what made me learn GS's functionality rather quickly.

In this case I'd put this as the very first line of code in your rule:
if game.actorDestroyed = true
do
Change Attribute:
set: game.actorDestroyed to: false

I'm still trying to figure out how the chronological order of all actors work, but I believe it should still execute all code underneath it, because the attribute was true to begin with, however it doesn't mess up any things you change when you spawn your new actor.

If this doesn't work, change the attr. to false somewhere lower in that same rule, would be my guess. I'd recommend trying some stuff out and especially if it doesn't work the way you want, look what it is doing. Some initial errors have brought me great work-arounds I really couldn't have thought of by myself at this stage.