#how to select one instance of an object to run code for?

49 messages · Page 1 of 1 (latest)

full agate
#

Im making a cups and balls game where you have 3 cups which will be shuffled and you need to guess under which cup the ball is. im making a simpler version with 2 balls but im immediately running into a problem. I need the cup on the left to move to the right and the cup on the right to move to the left to ''shuffle'' the ball. I could just make it so that they move left and right based on their coordinates but I want to expand the game to 3 or maybe even 4 cups later. How do i go about making this?

brazen spade
#

@full agate I think I would put these into a ds list (lowest to highest xpos)

Then create a 'shuffle' action that randomly selects any 2 cups next to each other (2 entries next to each other in the ds list) and switches them (for example with a transition).

And then just repeat that x amounts of time for your shuffle action.

full agate
#

also, how do i specify which instances need to Be moved and and where?

severe shore
#

So you have two global arrays: one array that's a position array. that stays fixed and are basically slots of where cups should be
And then you have one array that stores the instance_id's of the cup.

And it's a matter of matching up id's with positions

for i=0; i< num_cups-1; i++{
cups[i].x = position[i][0]
cups[i].y = position[i][0]
}

At the start of the game, you can make the cups manually submit their id's and positions into the arrays in their instance creation code

To shuffle, you pick random numbers between 0 and num_cups-1. If they're the same, try again. otherwise, swap those positions id's in the id array, and run the id-position matching game again.

#

If you're adding more cups mid game, just expand the two arrays and have the new cup register itself to both

full agate
#

whats the function to make them submit their id? and will that id always be the same?

severe shore
#

it will juts be id

#

and the id stays the same until the instance is destroyed yes

#

obj_cup.id is the id

#

built in variable, like x and y - should turn green

full agate
severe shore
#

hell if you're feeling fancy you can put your own custom id's

severe shore
full agate
#

damn ok now im getting off track i think

#

so in the slots the objects will request the id of the cup currently inside them

severe shore
#

question actually

full agate
#

but if the id is random how can i use the id in code?

severe shore
#

are the empty cups different

full agate
severe shore
#

ok let's not beat around the bush

#

what exactly is the game you're tryna make

#

let's not try to make a cup swapping game if that's not even what you're doing

full agate
#

you have 2 cups, one of which has a ping pong ball under it, they get shuffled, and you have to choose the right one

#

thats the entire game

severe shore
# severe shore are the empty cups different

my suggestion here was gonna be

if all the empty cups are the same you don't even really need to bother moving them around. Just make the animation of swapping, and test for the ball externally.

#

like which ball has the cup is an external variable. the ball doesn't exist until you click on the cup

severe shore
#

each cup knows it's own id, and so can go
"Hey, my name is [id], and i am cup #3 at position X, Y"

severe shore
full agate
#

say I use instance_find

#

and put all 3 cups in an array

#

and then i can do something with those values

severe shore
#

it won't be the same order

#

don't use instance find. the order is kinda random, cuz it has to do with creation order

full agate
#

so... how do I find instances, always have them in the same order, and have the code know what value the ID is so it can be used for something?

severe shore
#

the most reliable is to use creationc ode

#

explictily code into that instnace that it is #1

#

in your room editor, right click the instance, and there's a button called creation code

#

you can run whatever code specific to that instance, and it's like a 2nd create event but only for that instance specificially

full agate
#

That’s pretty useful

severe shore
#

see how it says obj_npc?

full agate
#

Alright I think I got it now

full agate
severe shore
#

Its like a 2nd create event that runs after the object crea the event

#

so if you declare like
Variable=0 in the object creation code, and then Variable=1 in the instance creation code, Variable will be 1 in the step event