How to go through an array of images one after another - arrays

I'm quite new to programming and GDScript and wondering how to do something that I did think would be quite simple, but I'm struggling a bit!
I've loaded an array of images and I want it to go through each of these images one after the other each time a button is clicked and replace a sprite texture with that particular image.
Right now I can successfully get the sprite to change to any of the images if I put its array number in e.g. new_texture[0] or new_texture[3] etc., but I would like it to go through each of the images one after the other every time the user clicks the button. (And once it's got to the last image, go back to the first image again)
What am I missing?
Here is the code:
extends Node
onready var my_sprite = get_node("/root/Game/Picture/Sprite")
var new_texture = [
load("res://Art/Picture1.png"),
load("res://Art/Picture2.png"),
load("res://Art/Picture3.png"),
load("res://Art/Picture4.png"),
load("res://Art/Picture5.png"),
load("res://Art/Picture6.png")
]
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[0]

As you know, you can get the first image of the array like this:
new_texture[0]
Where new_texture is a variable that holds the array, and 0 is a constant (a literal, to be more precise) that indicates the position on the array.
So you can change the constant to get a different image, for example the fourth element is:
new_texture[3]
Now, that is fine if you want to change which element you get form one version to another of the game, since it requires to modify the codeā€¦
But you want which element from the array you get to change during the execution of the program. In other words, you don't want it to be constant, but variable.
So declare a variable that will hold which element you get:
var texture_index := 0
Use it instead of the constant:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
And now you can change which element from the array you get by changing the value of the variable, for example:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
texture_index += 1
Here we are changing the value of texture_index so the next time this code executes it gets the next position.
But be careful to keep the value of the variable inside the bounds of the array to avoid errors. So, what do you want to do after it reaches the last element? Do you want it to loop? Well, we can check if we reached the last element with an if, and set a different value to the variable, like this:
func _on_Arrow_pressed() -> void:
my_sprite.texture = new_texture[texture_index]
texture_index += 1
if texture_index == new_texture.size():
texture_index = 0
I remind you that elements of the array go from the position 0 to the position array.size() - 1. For example, if the array has 10 elements, they are in the positions 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (from 1 to 9, there are 9 numbers, plus the 0 you have 10 numbers). So an array with 10 elements does not have an element in the position 10. So there is no element in the position array.size(), thus, when the index reaches that value it is out of bounds of the array, and we reset it to the first element (position 0).
There are, of course, variation of this approach.

Related

Changing one element in a vector within a function

I want to change a single element in a vector:
function vecteur_avec_delai=delai(input_vecteur, iteration)
vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
end
input_vecteur is a vector of size 6001 -by- 1 filled with the same value: 61.46 , and vecteur_avec_delai = ones(6001,1)
I want to change one value of vecteur_avec_delai to 61.46, so to still have a vector of length 6001, filled with ones except one cell with a value equal to 61.46, thus the vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
When I run :
vecteur_avec_delai=delai(input_vecteur, iteration)
It compiles but gives me for output a vecteur_avec_delai of size (iteration, 1 ) filled with 0s except for the last value equal to 61.46.
When I try this operation "manually" (directly in the MATLAB command window), it works, so why doesn't it when I go through this function?
You haven't declared vecteur_avec_delai as input into the function. This boils down to calling A(10,1) = 1 in a clean command window: it creates a 10-by-1 vector with 9 zeros and a 1 at the end. Instead, first declare your array to be ones and then set only this required element:
function vecteur_avec_delai=delai(input_vecteur, iteration)
vecteur_avec_delai = ones(size(input_vecteur));
vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
end
Or, given your comment about it working in the command window, simply add vecteur_avec_delai to the inputs of the function (you might want to change the names of the input and output variables to not be the same for clarity). Functions are closed name-spaces, i.e. they have their own "workspace" so to say, and cannot see anything outside of that function, hence the need to declare all inputs.

<Pinescript> Indicator error using bar prices in arrays

I'm using this simple code to learn arrays in pine v5:
var float[] my_arr = array.new_float(0)
if barstate.islast
array.push(my_arr, close[1])
a = array.get (my_arr, 0)
plot(a)
I'm trying to insert the previous close in my_arr and plot its value. I think the value of the close[1] should be entered in the array at the index 0 using: array.push() But when I plot that value, It appears to me this message:
The barstate last call will not run until the last bar of the chart. When you load a script it runs on all historical bars first beginning at the first bar in time. The array.get call is trying to pull a value from an array that is not populated. You can put in a ternary check to make sure the array size is greater than 0, or you could initialize your array with a size and a value like (1) which would give the first populated value na until populated by your of statement. If you just want to plot close[1] you can skip the array and just plot that series
Cheers

Can you use the dig method to get the last element of an array?

Lets say I have the following:
nested_object = [0, 1, 2, {foo: 'bar'}]
How do I use dig to select the last element of the array as I search for deeper nested objects?
A negative value integer in any dig method arguments starts at the end of the array and moves towards the beginning. So -1 selects the last element of the array, -2 selects the second from last, and so on. So, in your case:
target = nested_object.dig(-1, :foo)
will select the last element of the array and proceed from there.

How to utilize arrays in corona?

I have created this text array and image array.
How do i make it such that when a random image appears the corresponding text appears?
Example: When the image shows 9, I want the text displayed to be 9
I really need help.
Are you looking for getting the index of arrays in Lua? I'll just provide a small example.
You can create an array like:
local myArray = {"element_1","element_2",...,"element_n"}
And you can get an array element as:
print(myArray[1]) -- Prints 'element_1' (1st element)
print(myArray[n]) -- Prints 'element_n' (nth element)
So, if you have two arrays as below;
local myQuestionArray = {"1","2",...,"n"}
local myImageArray = {"img_1","img_2",...,"img_n"}
you can take the question and image with respect to a number as:
-- for number = 1
print(myQuestionArray[1]); // 1
print(myQuestionArray[1]); // img_1
-- for number = n
print(myQuestionArray[n]); // n
print(myQuestionArray[n]); // img_n
For mode details, refer: Understanding Lua tables in Corona SDK
Keep Coding.......... :)

Inserting data into an array sequentially

I am currently trying to figure out how to design some sort of loop to insert data into an array sequentially. I'm using Javascript in the Unity3D engine.
Basically, I want to store a bunch of coordinate locations in an array. Whenever the user clicks the screen, my script will grab the coordinate location. The problem is, I'm unsure about how to insert this into an array.
How would I check the array's index to make sure if array[0] is taken, then use array[1]? Maybe some sort of For loop or counter?
Thanks
To just add onto the end of an array, just use .push().
var myArray = [];
var coord1 = [12,59];
var coord2 = [87,23];
myArray.push(coord1);
myArray.push(coord2);
myArray, now contains two items (each which is an array of two coordinates).
Now, you wouldn't do it this way if you were just statically declaring everything as I've done here (you could just statically declare the whole array), but I just whipped up this sample to show you how push works to add an item onto the end of an array.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push for some reference doc on push.
In case you need to know the array's length when reading the array in the future, you can use the .length attribute.
var lengthOfArray = myArray.length;
Using the .push() method as suggested by jfriend00 is my recommendation too, but to answer your question about how to work out what the next index is you can use the array's length property. Because JavaScript arrays are zero-based The length property will return an integer one higher than the current highest index, so length will also be the index value to use if you want to add another item at the end:
anArray[anArray.length] = someValue; // add to end of array
To get the last element in the array you of course say anArray[anArray.length-1].
Note that for most purposes length will give the number of elements in the array, but I said "one higher than the current highest index" above because JavaScript arrays are quite happy for you to skip indexes:
var myArray = [];
myArray[0] = "something";
myArray[1] = "something else";
myArray[50] = "something else again";
alert(myArray.length); // gives '51'
// accessing any unused indexes will return undefined

Resources