Legacy GM Really Help, Cannot set the requested sprite_index | No one?

Y

Ysela_Creyo

Guest
I have this code in my arrows (click on these to select character)

image_index -= 1 (on left arrow obj) and image_index += 1 (on right arrow obj) these codes apply to cpu_object (which is the object I want to change sprite "character" ) in my selection room

for that I'm using this code in the step of CPU_object

if image_index == 8
{
sprite_index = spr_uruguay;
image_speed = 0;
}
else if image_index == 7
{
sprite_index = spr_portugal;
image_speed = 0;
}
else if image_index == 6
{
sprite_index = spr_mexico;
image_speed = 0;
}

and so on...

the problem is when i Star the game (playing room), the sprite for the cpu_object is ALWAYS the last set in the code for example lest say "spr_mexico", instead of being the one selected previously in the selection_room.

I have no clue.
 
Ok, so why are you changing your cpu image index and sprite index? How many images are there in your cpu assigned sprite, or the sprites you assign to it? If you change the sprite then you're then testing the image index of the new sprite. And is your cpu object persistant? Otherwise it will just revert to its original sprite on room change. Apologies if I'm misunderstanding/not getting your methodology, but this seems very odd to me.

Instead of using image index, use a new variable, e.g. choose_sprite.

Oh and you'd probably be better served with a switch statement rather than all those if statements.
 
Y

Ysela_Creyo

Guest
Ok, so why are you changing your cpu image index and sprite index? How many images are there in your cpu assigned sprite, or the sprites you assign to it? If you change the sprite then you're then testing the image index of the new sprite. And is your cpu object persistant? Otherwise it will just revert to its original sprite on room change. Apologies if I'm misunderstanding/not getting your methodology, but this seems very odd to me.

Instead of using image index, use a new variable, e.g. choose_sprite.

Oh and you'd probably be better served with a switch statement rather than all those if statements.
Hi, thank you for your answer

In my CPU sprite there is 9 sub-images (0 to 8). My CPU object is not persisten, but even so (didn't have default sprite asigned).

Yeah is very odd, because suppouse just usind Image_index should be enought (since in the selection room it is) but if I only do this the cpu_object sprite will animate (0 to 8), if I set speed=0, (to make it stop animate) it will not set the Image_index selected.

That's why I change to this way. which mean (when the selector select) a sub-image (image_index) then set to CPU_object a sprite_index. This works but I cannot find out how to let me set the choosen one, (it just set the last spr_index) in change of the one selected in the previous room.

Sorry if i'm not being concrete
 
Ok, I think I understand what you're trying to do... You're going to have to change your methodology.

When you switch room, the objects are destroyed and if placed/created in new room - start again. You then have a cpu object which has no sprite assigned, it goes into the step event and by default has an image index of 0. So it goes through your if statement list, agrees with the last statement (image_index == 0) and so assigns the final sprite index and sets the image speed to 0.

Everything you're doing in your selection room is irrelevant as it's just destroyed on room change.

You need to modify your if statements so they are either done within your select objects or are only called in room select.

You need to determine a method for transferring your chosen sprite across rooms, e.g. with a global var.
 
Y

Ysela_Creyo

Guest
@michael_bateman

Ok, I think I understand what you're trying to do... You're going to have to change your methodology.

When you switch room, the objects are destroyed and if placed/created in new room - start again. You then have a cpu object which has no sprite assigned, it goes into the step event and by default has an image index of 0. So it goes through your if statement list, agrees with the last statement (image_index == 0) and so assigns the final sprite index and sets the image speed to 0.

Everything you're doing in your selection room is irrelevant as it's just destroyed on room change.

You need to modify your if statements so they are either done within your select objects or are only called in room select.

You need to determine a method for transferring your chosen sprite across rooms, e.g. with a global var.
I tried this
In the create event of CPU_object
global.choose_sprite = image_index

then I the step event I change image_index for choose_sprite in the if statements

global.choose_sprite = (the number of subimage)

but still is the same, it select the last if statement, in change of the one selected in the selector room.
 
You still aren't understanding the logic. Try and read through your code as if you were a computer. Remember, when you change room, if the object is not persistent, the object code will be started all over again - including the create event.

Hopefully this will help you to understand:

Code:
// In the create event of cpu object
select_image = 0;
// We only do this action if we are in the room_selection, otherwise, we will just reset the variable when we go to room_play and the create event is called again.
if( room == room_selection )
{
global.choose_sprite = spr_mexico; // just to have as default
}

// Step event of cpu object
/* Again, we only want to be able to edit the sprite if we are in room_selection. If we don't differentiate the room, the code will run fresh in our room_play, using the variables we set in the create event*/
if( room == room_selection )
{
switch( select_image ) // Much tidier than multiple if/else statements
{
case 0: global.choose_sprite = spr_mexico; break;
case 1: global.choose_sprite = spr_portgual; break;
}
}

// Now, we set the sprite of the cpu object, depending on what we have set the global variable to.
sprite_index = global.choose_sprite;

/* Then in your selector objects, alter the variable select_image NOT image_index.  As this is done in the room_selection, it will enable you to choose what the global variable will be set as.*/
 
Y

Ysela_Creyo

Guest
You still aren't understanding the logic. Try and read through your code as if you were a computer. Remember, when you change room, if the object is not persistent, the object code will be started all over again - including the create event.

Hopefully this will help you to understand:

Code:
// In the create event of cpu object
select_image = 0;
// We only do this action if we are in the room_selection, otherwise, we will just reset the variable when we go to room_play and the create event is called again.
if( room == room_selection )
{
global.choose_sprite = spr_mexico; // just to have as default
}

// Step event of cpu object
/* Again, we only want to be able to edit the sprite if we are in room_selection. If we don't differentiate the room, the code will run fresh in our room_play, using the variables we set in the create event*/
if( room == room_selection )
{
switch( select_image ) // Much tidier than multiple if/else statements
{
case 0: global.choose_sprite = spr_mexico; break;
case 1: global.choose_sprite = spr_portgual; break;
}
}

// Now, we set the sprite of the cpu object, depending on what we have set the global variable to.
sprite_index = global.choose_sprite;

/* Then in your selector objects, alter the variable select_image NOT image_index.  As this is done in the room_selection, it will enable you to choose what the global variable will be set as.*/
I do everything, and now cannot change sprite, the sprite is set to the first sprite added in the sprite section. Maybe i put the variable wrong? sprite_index = global.choose_sprite; this I put on the top of create event of CPU_object. (which is persistent) i try bot, (and before I tried already).

also I'm not sure how "case 0; case 1" works. but I guess that "0" and "1" ( is equeal to the select_image number right? )

and in my arrows I change image_index -= 1 to select_image -= 1
 
Last edited by a moderator:
Y

Ysela_Creyo

Guest
Post your code for create and step event for the cpu object please.
sure

///Initial position
x = 20;
y = room_height/2;
//selector
select_image = 0;
sprite_index = global.choose_sprite;
if( room == rm_quickmatch )
{
global.choose_sprite = spr_alemania;
}

also using this one as default makes my CPU_object didn't have sprite at all. (invisible)

/// Simple IA
if (instance_exists(obj_bola)){

// si 0,1,2 => 60% y 3,4 => 40%
if (irandom(4) < 3){

if (obj_bola.y > y) {
y += abs(obj_bola.vspeed)*1.5;
} else {
y -= abs(obj_bola.vspeed)*1.5;
}

}

y = clamp(y,115,610);
}
if( room == rm_quickmatch )
{
switch( select_image )
{
case 8: global.choose_sprite = spr_uruguay; break;
case 7: global.choose_sprite = spr_portugal; break;
case 6: global.choose_sprite = spr_mexico; break;
}
}

(the 8, 7,6, are the sub-images)
 
Last edited by a moderator:
1 - Is rm_quickmatch your selection room? Because that's where your testing the select_image variable...
2 - Why are you using 8,7,6 for your select_image variable? Why not 0,1,2 or 1,2,3? select_image isn't an inbuilt variable. It's a variable you've created.
2a - You state 8,7,6 are the sub-images but you're not changing the sub image (image_index), you're changing the sprite_index, so this makes no sense.
3 - sprite_index = global.choose_sprite; is in your create event, it should be in your step event. You are currently setting the sprite_index before you've determined what you want it to be.

I recommend you consult the manual and learn about variables, image_index, sprite_index, and switch statements. It's really important to understand the code you're writing otherwise you are constantly going to come up against brick walls all the time. It's OK copying and pasting code to save time if you understand what it does, but otherwise you learn nothing and are not able to apply it properly.

I hope this doesn't sound harsh as it's not meant to be, just trying to give some good advice :) Happy to help if you're stuck, but you need to understand what you're coding otherwise it'll be a constant battle.
 
Y

Ysela_Creyo

Guest
1 - Is rm_quickmatch your selection room? Because that's where your testing the select_image variable...
2 - Why are you using 8,7,6 for your select_image variable? Why not 0,1,2 or 1,2,3? select_image isn't an inbuilt variable. It's a variable you've created.
2a - You state 8,7,6 are the sub-images but you're not changing the sub image (image_index), you're changing the sprite_index, so this makes no sense.
3 - sprite_index = global.choose_sprite; is in your create event, it should be in your step event. You are currently setting the sprite_index before you've determined what you want it to be.

I recommend you consult the manual and learn about variables, image_index, sprite_index, and switch statements. It's really important to understand the code you're writing otherwise you are constantly going to come up against brick walls all the time. It's OK copying and pasting code to save time if you understand what it does, but otherwise you learn nothing and are not able to apply it properly.

I hope this doesn't sound harsh as it's not meant to be, just trying to give some good advice :) Happy to help if you're stuck, but you need to understand what you're coding otherwise it'll be a constant battle.
Well first of all, thank you, because you take your time, to come to answer me :)

1.) Yeah rm_quickmatch is my selection room.
2.) I don't remember if I mention, but using 0,1,2, well both, didn't work, (it select the sprites) I also mentioned I wasn't sure how it works, so of course i didn't know :p (that's why you think is nonsense) btw sorry.
3.) I put it on my object where i have all the global. (and I also put it on the step event) not working neither. It could be because the coding not suitable for what I want, maybe we are misunderstanding, (in the way, you maybe didn't get what's my point for the code) but not sure of this.

Thank's for the advice, but kind of you're barking up the wrong three, which also means you don't need to asume I'm expert, which I'm not, but also i'm not new. And yes I also think copying and pasting code is ok, and you can learn of it, of course as you say, if ones knows (have at least a basic knowledge) about it.

If I ask a question in here is probably, becasue I try already (or fail to find a solution, or the solution was so clear-simple one fail to see at first sight) it happens. :p - otherwise the site will be full of post don't you think :p
 
Top