GMS 2 Array in GM2 - Code Error (Help)

Ednei

Member
I tried to use this code in GM2:

Code:
var display_number_x = 0;

 for(var i = 0 ; i < number_of_displays ; i +=1)
{
if !instance_exists(display_number[i]){

display_number[i] = instance_create_depth(x + display_number_x, y ,-99, obj_display_number)

display_number_x -= 5
}
}
And I got this error:

Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obSYS_Message_Box1:

trying to index a variable which is not an array
 at gml_Object_obSYS_Message_Box1_Draw_0 (line 1061) - if !instance_exists(display_number[i]){
############################################################################################
I know I should not have put this code in the Draw Event.

But is this code wrong? I already used something similar in GM 1.4 and it worked.

Can someone help me?

Thank you.

Ednei
 

Tony Brice

Member
var display_number_x - is the name of your variable.

You're referencing display_number

Edit: Actually, read that wrong. Sorry.
 
P

PandaPenguin

Guest
you need to initiate that array at least once if you want to access it in your later code
Code:
!instance_exists(display_number[i])
this fails because you try to access display_number[0] but that array doesn't exist at all

you can't check for a non-existing array and populate it afterwards, you have to do it the other way round ;)
 

Ednei

Member
you need to initiate that array at least once if you want to access it in your later code
Code:
!instance_exists(display_number[i])
this fails because you try to access display_number[0] but that array doesn't exist at all

you can't check for a non-existing array and populate it afterwards, you have to do it the other way round ;)
Yeah, I figured that out.
However I do not know how to fix this problem. I need a workaround to prevent obj_display_number from being created infinitely (like a loop).
Would you have any suggestions?
 

Ednei

Member
Initializing the array in create event is too hard?
Is not difficult. I could initialize the arrays in the create event. Like for example:

Instance_array = array_create (100, noone);

However I would like to know if there is another way.

Because I will only know the amount of arrays I need after the create event. (Number_of_displays)

Thanks for the answer.
 
B

barnack

Guest
I see only conceptual problems here.
Will you dinamically add displays during execution?
If not then just create the initial array within a for loop, no need to check if there's something in the position youre going yo populate.
Code:
for(var i=0; i<displays_amount; i++)
   {
   display[i]=whatever
   }
If you want to add things dinamically (without removing)
Script add display called in item with the array
Code:
/// add_display(display)
var d = argument0
array[array_length_1d(array)] = d
If you want to dynamically add and remove items, then i'll write you later when i can put my hands over a keyboard, typing that with phone already made me mad
 

Ednei

Member
I see only conceptual problems here.
Will you dinamically add displays during execution?
If not then just create the initial array within a for loop, no need to check if there's something in the position youre going yo populate.
Code:
for(var i=0; i<displays_amount; i++)
   {
   display[i]=whatever
   }
If you want to add things dinamically (without removing)
Script add display called in item with the array
Code:
/// add_display(display)
var d = argument0
array[array_length_1d(array)] = d
If you want to dynamically add and remove items, then i'll write you later when i can put my hands over a keyboard, typing that with phone already made me mad


Thank you for your instructions.

In my case I solved the problem with a not elegant solution:

I deleted the "if! Instance_exists (display_number )" and modified the code as follows:

Code:
// in CREATE EVENT

input_switch = false;

Code:
// in DRAW EVENT

if input_switch = false {

var display_number_x = 0;

 for(var i = 0 ; i < number_of_displays ; i +=1)
{

display_number[i] = instance_create_depth(x + display_number_x, y ,-99, obj_display_number)

display_number_x -= 5

}
input_switch = true
}

The result follows below according to the screenshots of the rpg that I am creating:

screen1.png screen2.png screen3.png

My goal is to use a flexible box to insert numbers into a dialog box as the game needs.

The number of digits varies as needed. You can use it to enter codes, money values, quantity of items and other things that RPG games require.

In my example, I need to enter numbers of varying digits as the dialog between characters occurs.

Thanks for the lessons.

Ednei
 
J

JFitch

Guest
I don't think you're supposed to create instances in the draw event. You should be using the step event instead.
 
Top