The cases of a switch statement must be constants. Not variable. The value of the cases must be known at compile time.Kind of a stupid question but ya. My testing suggests the answer is no (not easily anyway) but I'm looking for confirmation.
example ~
GML:box[0] = puppies box[1] = kittens switch box { case box[0] == puppies (statement) ; break; case box[1] == kittens (statement) ; break; }
switch (someVariable) {
case 0: // Constant value, not variable.
// do something;
case 1:
// do something;
}
switch (box[0]) { // this makes sense, a single element of the array
}
switch (box) { // this doesn't make much sense in gml
}
enum EAnimalType {
puppies = 0,
kittens = 1
}; // These are constants
myArray[0] = EAnimalType.puppies;
myArray[1] = EAnimalType.kittens;
arraySize = array_length_1d(myArray);
for (i = 0; i < arraySize; i++) { // loop through each element of array and check case for each
switch(myArray[i]) { // switch on a single element of the array at a time
case EAnimalType.puppies: // cases are constant values
// do puppies stuff
break;
case EAnimalType.kittens:
// do kittens stuff
break;
default:
// do unhandled stuff
break;
}
}
if (box[i] == puppies) {
// do puppy stuff
} else if (box[i] == kittens) {
// do kitten stuff
} else if (box[i] != someVariable) {
// do some other stuff
} else {
// do some other stuff if no other case was met
}
if (var1 == value1)
...
else if (var1 == value2)
...
else if (var1 == value3)
switch(var1)
{
case value1:
case value2:
case value3:
}
collision_id = instance_place(x, y, oClickable);
unless you're using those arrays for other things. Even then, you're going about things in way too complicated a fashion.var min_index = 0;
var min_value = array[0];
for (var i = 1; i < array_length_1d(array); i++)
{
if (array[i] < min_value)
{
min_index = i;
min_value = array[i];
}
}
I think it's quite clear that I was over my head this time. But regardless, I'm still kind of impressed with myself, seeing as I found a way to accomplish this, even if it was in a ridiculously overcomplicated stressful way. I've never used parent objects before.If each condition is independent or uses a different variable, as in your case, just use the if-else. I think the problem here is that you're putting a lot of things into arrays that don't need to be in arrays. You can simplify all of this, the whole thing, to one line by giving all of the objects you're looking at a single parent object, maybe oClickable or oCollidable or something like that. Then that whole thing resolves tocollision_id = instance_place(x, y, oClickable);
unless you're using those arrays for other things. Even then, you're going about things in way too complicated a fashion.
collision_id = instance_nearest(x, y, (instance_place(x, y, oClickable)));
//draw_event//
if collision_id != noone and instance_exists(collision_id)
{ draw_text(x+15, y+15, (collision_id).size)
draw_text(x+15, y+35, (collision_id).age) }
collision_id = instance_nearest(x, y, 80012)
This is still doing too much. The instance_place is giving you an instance id. You're then asking "What is the nearest instance with that id to itself?", which is exactly the same id. Either use instance_place to get the instance at x,y, or use instance_nearest to get the one closest to x,y.collision_id = instance_nearest(x, y, (instance_place(x, y, oClickable)));
I'm aware... I was just trying to figure this out.This is still doing too much. The instance_place is giving you an instance id. You're then asking "What is the nearest instance with that id to itself?", which is exactly the same id.
How would you account for distance then? Consider that there are two of the oClickable objects being detected. Using the place_meet function it will return multiple conflicting instance ID's, which will make the data distorted. Using the instant_nearest function it will return the data regardless of whether there is an actual collision occurring. I'm trying to display the nearest instance ID to avoid those data conflicts, in order to accomplish this I need to identify which is the nearest instance_id, and then make sure that a collision is taking place. I'm having a hard time solving that puzzle.Either use instance_place to get the instance at x,y, or use instance_nearest to get the one closest to x,y.
There are no lists involved in anything that you've done. Whatever you're thinking about efficiency, it's not right, and I don't think you need to worry about that.
collision_id = instance_place(x, y, obj_fireflies);
if collision_id != noone
{ collision_id = instance_nearest(x, y, obj_fireflies); }
else
{ collision_id = noone }
This is not true. place_meeting will return true or false. instance_place, on the other hand, will return a single instance ID. If there are multiple instances at that location, it will choose one of the collided instances to check. Since I don't know what the shape of the instance that's calling this code is, I can't tell you how to resolve exactly what you want, but you could use instance_nearest and then determine the actual distance to see whether it's within the range you want. I expect there to be some strange edge cases in what you've posted, but if it works for you, then so be it.Using the place_meet function it will return multiple conflicting instance ID's
Right, sorry, I was using them interchangeably by accident. I meant to say instant_place. I'm kind of unsure about what your saying? Why wouldn't the code I posted address this problem? It's checking the instance ID, returning a value, this satisfies the collision detection. The statement underneath it will only return the nearest instance of the object in question (which is the parent object of all the other objects). Which (at least in theory) would always return the nearest collided instance ID. Am I wrong about this? It seems to be working, but you suggest that there could be problems.This is not true. place_meeting will return true or false. instance_place, on the other hand, will return a single instance ID. If there are multiple instances at that location, it will choose one of the collided instances to check. Since I don't know what the shape of the instance that's calling this code is, I can't tell you how to resolve exactly what you want, but you could use instance_nearest and then determine the actual distance to see whether it's within the range you want. I expect there to be some strange edge cases in what you've posted, but if it works for you, then so be it.
Good question. I'm making kind of a weird game. It basically uses the cursor in the same manner as a game like insaniquarium. The screen is filled with little fireflies, tons of them. Sometimes they pile up. The cursor could very well have in excess of 10 of them within the collision mask. So if the player wants to access, say, the species of a specific firefly, this would be challenging in that situation. Using this code the player, using a degree of precision, can always access the information they want. If I didn't implement this, it would frustrate the player.I'm saying that if there's a collision, then you're colliding with something. How often is that not the nearest instance? What is nearer than "right here"?
I'm a complete novice, so me if I'm wrong. But I guess my understanding was that using instance_place it would return the first instance that enters the collision mask. So I guess the question I asked myself, building off that inference, was what if there are multiple instances within the collision mask? How do you prioritize which one to select? So I wanted the nearest one in particular, instead of whatever displays first and is retained inside the function. This becomes important when there are multiple instances within the collision mask. Maybe the instance ID that first enters the mask is maintained over the other (closer) instances. Because technically the more distant one is the first one received, and likely to be retained. At least hypothetically. I don't entirely understand how the game engine would prioritize instances using this function.I've never played Insaniquarium and still don't understand a situation where the nearest instance doesn't collide with instance_place, but a different one does.
If the answer to this question matters to you, then instance_place is not the function that you want to use. There is no "first" or "retained" or "maintained" or "distant". There is "Find me an instance whose bounding box overlaps mine, and I don't care which one."what if there are multiple instances within the collision mask?
I think there is some kind of fundamental misunderstanding between us. I don't want to frustrate you, so you don't have to keep responding if you don't want. I'm entirely aware that instance_place doesn't care which instance it grabs within the collision mask. That's what I'm trying to account for. I'm also entirely aware that instance_nearest returns the nearest instance. Using instance_nearest alone would perpetually display the data, instead of activating during a collision. When you use either of these independently it doesn't give you the right result. But if you combine the two of them like I did, it does.If the answer to this question matters to you, then instance_place is not the function that you want to use. There is no "first" or "retained" or "maintained" or "distant". There is "Find me an instance whose bounding box overlaps mine, and I don't care which one."
If you want the nearest instance, use instance_nearest. That's what the name is, that's what it does. If you then want to know whether it's within a certain distance, you can find the distance to it using distance functions, or you can check whether there's overlap by using place_meeting and that single instance id, rather than checking all instances twice over.
collision_id = instance_place(x, y, obj_fireflies);
if collision_id != noone
{ collision_id = instance_nearest(x, y, obj_fireflies); }
else
{ collision_id = noone }