Complex arrays

Get help using Construct 2

Post » Mon Feb 27, 2012 10:26 am

I think this may require a plugin, I need to handle a VERY large arrays from where I'll select values at randomly.

The array would be structured like this:

var group1= [ 'zero', 'one', 'two', 'three', 'four', 'five', ... ];
array['group1'] = group1;

This repeated for a lot of times.

Then I need to do this:
Select array['groupX'][Math.floor(Math.random() * array['groupX'].length)]
For each of the "main" indexes.

Is this possible to do with construct2?
I presume I need to write a plugin since inputting this much data in c2 would be an endless job.

How do you create an array plugin like this?

Thanks






B
29
S
9
G
6
Posts: 525
Reputation: 8,294

Post » Mon Feb 27, 2012 10:32 am

I don't know the answer on how to do this, but may I ask 'why' this is needed?

Is there another way to approach the problem that is less memory intensive?
B
21
S
5
G
2
Posts: 26
Reputation: 2,512

Post » Mon Feb 27, 2012 10:37 am

Arrays are not memory intensive at all, and there are no other ways to do what I want, it's an educational application, basically I procedurally generate questions from questions and answers arrays and then load the corresponding graphics. It's an easy task and I'm evaluating if doing it on c2 or a "normal" javascript engine.
B
29
S
9
G
6
Posts: 525
Reputation: 8,294

Post » Mon Feb 27, 2012 10:40 am

You should have a look at the system expression(Text section) tokenat, it sounds like it is exactly what you would need.

Search it as a keyword, there should be topics available that give examples of use.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Mon Feb 27, 2012 10:44 am

[code]Global Text groupList = ""
+ System: on start of layout
-> set Text groupList to : "zero,one,two,three,four,five
zero,one,two,three,four,five
zero,one,two,three,four,five
zero,one,two,three,four,five
zero,one,two,three,four,five
..."[/code]Each line is a group with its values.
And then if you want to pick a random value from let say, group 3
[code]+ On what you want
Local Text group = ""
-> System: Set group to tokenat(groupList,3,newline)
-> Text: set text to tokenat(group,floor(random(tokenCount(group,","))),",")[/code]
Works like a charm.
Yann2012-02-27 10:45:35
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Feb 27, 2012 10:51 am

You can create as many of the Array object as you like at runtime, as well as access them using pick nth, and array(index).
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Mon Feb 27, 2012 1:06 pm

@Yann I see thanks.

Other question, is it possible to pick an object with its name. Like I have

global text var sprite = "mysprite"

system create object sprite at x 10 y 10
?0plus12012-02-27 14:03:14
B
29
S
9
G
6
Posts: 525
Reputation: 8,294

Post » Mon Feb 27, 2012 4:45 pm

nope 'cause 'mySprite' isn't the name of an object, but the name of an objectType. The object you have on your canvas are instances from this objectType.
There's two ways to refer to a specific instance:
- picking
- using IID in expression

To pick you have to isolate an object via a condition.
It could be for example
- mouse: is overlapping this object
- object: pick by Unique ID
- System: pick nth instance of an objectType
- object: myVar = something
- ...
Any condition for which the specific instance will be true will initiate the picking of this specific instance, and then you'll be able to use it and only it in an action

The other one is using IID in expression. Each instance of an objectType has an Instance ID.
(Not the same as UID, Unique ID is a Unique number identifying all objects present in your layout, whereas IID are assigned by objectType)

Then via this IID you can do something like
[code]System: Every Tick :
-> Sprite: Set angle to angle(self.X,self.Y,Sprite((self.IID+1)%Sprite.Count).X,Sprite((self.IID+1)%Sprite.Count).Y)[/code]
Basically each Sprite will angle toward the next in the IID list. (I used that in cubemaze to draw the ellipse in the menu)
Yann2012-02-27 16:47:24
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Feb 27, 2012 5:10 pm

Is it possible then to set the unique ID to something arbitrary?
B
29
S
9
G
6
Posts: 525
Reputation: 8,294

Post » Mon Feb 27, 2012 6:41 pm

nope Unique ID and Instance ID are automatically assigned on creation
By setting a unique ID arbitrary you would have risked it to not be unique anymore...Yann2012-02-27 18:41:58
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: nicklowkcmagic, WhosWho and 17 guests