Hi again, Zivouhr!
I'm now going to try to explain what is wrong with your code, how you should fix it, and what you should do to make your game perform the desired actions more efficiently. I urge you to read the full post before beginning to modify your code based on my corrections of your code, since I first tell what is wrong with the code and how you could fix it, but after that I tell you about an alternative way of achieving what you want to do, and I strongly recommend using that one instead.
This code doesn't do anything:
- Code: Select all
type=rand(4);
switch(type)
{
case 0:
"treasure.0";
break;
case 1:
"treasure.1";
break;
case 2:
"treasure.2";
break;
}
Or, well, it does set a random value varying from 0 to 3 to type and then the compiler goes through the switch case and it type is equal to 0, 1 or 2, it interprets the texts but doesn't do anything with them. What is this part supposed to do? I don't see any meaning with this since you don't actually need to do anything in any actor's draw actor code to achieve what you want to do. Also, you should know that you can't add any code just for a single clone. You said that this code is in treasure.0's draw actor code, but it's actually in every treasure actor's draw actor code. The way to make some code happen only in certain clones is by using an if check to see what clone is interpreting the code, or to use a switch case for the same result as using an if statement.
Also, here you are using collide.cloneindex, when you'd want to only use cloneindex:
- Code: Select all
switch(collide.cloneindex) //You shouldn't use collide.cloneindex here
{
case 0: //just wondering where I went wrong with this code here?
ChangeTransparency("treasure.1"), 0.000); //this makes invisible treasure.1 visible after contacting treasure.0.
break;
case 1:
ChangeTransparency("treasure.2"), 0.000); //this makes invisible treasure.2 visible after contacting treasure.1.
case 2:
ChangeTransparency("treasure.3"), 0.000); //this makes invisible treasure.3 visible after contacting treasure.2.
}
I see that you're confusing this collision event with the thing I talked to you about earlier. The thing with "collide" is that it's meant for reading the variables of the actor the current actor is colliding with. So, for example, in your previous problem with the saving of the food actors, you had the code in the player's collision event with the food actors. And you wanted to know the cloneindex of the food actor the player collided with, so you had to use collide.cloneindex.
But now, in this case, the situation is reversed, you don't now have the code in the event when the player actor collides with the treasure actor, but instead where the treasure actor collides with the player actor. Of course, this is the same thing when you look at it in the game, but it differs in the way the code gets interpreted, and in this case, "collide" refers to player actor, not the treasure actor.
So now, because you're always using player's cloneindex, the code will always make treasure.1 reappear, since there's only 1 player actor and thus it's cloneindex is 0. Just remove the "collide." part from before of the "cloneindex" in the switch statement, and you should be good to go.
BUT. There is a lot simpler way to achieve what you want to do. Yes, this setup you have would do what you want, but think of it, you'll have to write 3 more lines of code for every new treasure you add to your game! Think if you end up to a point where you have 100 treasure actors in there, way too much code for something as simple as this. There is a very nice trick to achieving what you want to do, but first I'll point out another issue with this concept.
You currently have the code in the treasure actor's collision event with player. What this means in practice, is that every single frame of the game, each of the treasure actors runs a collision detection event to see if they collide with player. And that can cause slowdown on your game, and it's generally a bad habit to add any events to clones when you can have some single actor handle it all for the clones. So, instead of using treasure -> collision with player, use player -> collision with treasure. Only one actor having to check for collisions now.
(This would of course mean, that you now then again have to use collide.cloneindex in the collision event, to get the cloneindex of the treasure the player collides with, instead of using only cloneindex, because that would give you the 0 cloneindex of the player actor's)
Now, about that neat trick for avoiding the need to write three new lines of code for every new treasure you add to your game.
This is very simple because you want to have the treasures activate each other in the simple order based on their cloneindex. So what you want to do is this:
Player -> Collision with treasure -> script editor:
- Code: Select all
char temp[256];
//The above line of code creates a temporary variable to be only used in this event. This variable is an array of characters, also called a string. This string is 256 characters long. The name temp is an abbreviation for temporary, you can call it whatever you want.
sprintf(temp, "treasure.%i", collide.cloneindex + 1);
//The above line of code prints the clonename (=actor name.cloneindex) of the next clone to the temporary string called temp. For example, if the treasure that collides with player in this event has the cloneindex of 2, the string printed to temp will be this: "treasure.3". That's because we use the "%i" to tell sprintf to add an integer value to that place in the string, and then we tell sprintf that this integer value will be equal to the collide actor's (the treasure the player collides with) cloneindex plus 1, which then is 2 + 1 = 3.
VisibilityState(temp, ENABLE);
//The above line of code sets the visibility state of the actor who's clonename is stored in the temp, to ENABLE, and that meas that the actor will become visible.
And that there, is all. This code will do what you want no matter how many clones of the treasure actor there is.
But now you might think what this VisibilityState command is? Well, I explained that in my latest answer to your previous question, and basically, it's just a better way of making actors invisible / visible. For more information on the command, see
this page and search (F3 or CTRL + F) for the word VisibilityState.
Of course, for using VisibilityState instead of changing the transparency of the actors, you'll need to make them invisible via the use of VisibilityState in the first place. For doing this, just add a code like this to the Create Actor event of the treasure actor:
- Code: Select all
if (cloneindex != 0) //This means "if cloneindex is not equal to 0". That's because we want the treasure number 0 to be visible upon startup, right? =)
{
VisibilityState("Event Actor", DISABLE); //This makes the current clone have it's visibility state set to DISABLE, and because this code gets interpreted for every clone, it sets all clones (except clone 0) to be invisible
}
I hope this wasn't too messy of an explanation, I tried my best to cover all things as well as I could. If there's still some trouble, don't hesitate to ask more.
Oh, one more thing..
Zivouhr wrote:And for the variable, I added the word "type" (without quotes) into the Integer Global code, and then tried it with Integer Actor code too
Just so that you know, the variables created via the Variables window, are either:
- Global variables = This means that there's just that one variable with that name and it's accessible by every actor at any point in the game.
- Actor variables = This means that every actor in the game has it's own unique case of this variable, every actor has it's own copy of this variable, just like every actor has it's own value for it's x and y coordinates, for example. These are also accessible by any actor in the game, but you'll have to define which actor's variable you want to access, except if the code you're using for accessing the variable is in an event of the actor who's variable you want to access.
An example of actor variables:
Let's say we've created an actor variable called "myVar", and it's an integer variable.
Now, let's say we have an actor called "myActor" and we want to set it's "myVar" to 5.
What we can do, is to, for example, add this to "myActor"'s Create Actor event:
- Code: Select all
myVar = 5;
Note that we can simply write myVar, because we are accessing the variable from an event of the actor itself, and Game Editor will then use the current actor's variable.
But if we wanted to set "myActor"'s "myVar" to 5 when, for example we click another actor with the left mouse button, we would add this code to that actor's mouse button down -> left -> script editor:
- Code: Select all
myActor.myVar = 5;
I think that's all I wanted to say