How do I make NPC attributes in 1 variable?

Get help using Construct 2

Post » Sun Oct 19, 2014 12:16 pm

I don't remember exactly this is called in programming, I think it's massives. It's when you can put multiple things in a variable. This would be useful for enemy attributes in an RPG or action game, for example, I can put "zombie", "poisoned" and "big head" in 1 variable and let C2 check for those things being in the massive variable, instead of making 3 separate variables, like "Zombie", "Poison" and "HeadSize" with 2 possible values in each, which adds to the already cluttered variable list in a complex project like an RPG. Is there any way to do this?
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,377

Post » Sun Oct 19, 2014 12:46 pm

Use tokenat.
Example: vartext=zombie#9_poisoned#8_bighead#7

tokenat(tokenat(vartext,0,"_"),0,"#") = zombie
tokenat(tokenat(vartext,0,"_"),1,"#") = 9

tokenat(tokenat(vartext,1,"_"),0,"#") = poisoned
tokenat(tokenat(vartext,1,"_"),1,"#") = 8

tokenat(tokenat(vartext,2,"_"),0,"#") = bighead
tokenat(tokenat(vartext,2,"_"),1,"#") = 7

I used this a lot in my multiplayer CCG and extremely useful in simplifying communication, except that I'm doing it on a bigger scale, which involves 450x46 array. It might seem it will increase the number of event you have to write, but in my case, it reduces amount of event and let me to make a very "compact" loop.

Have fun :D
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Sun Oct 19, 2014 3:34 pm

DuckfaceNinja wrote:Example: vartext=zombie#9_poisoned#8_bighead#7

tokenat(tokenat(vartext,0,"_"),0,"#") = zombie
tokenat(tokenat(vartext,0,"_"),1,"#") = 9

tokenat(tokenat(vartext,1,"_"),0,"#") = poisoned
tokenat(tokenat(vartext,1,"_"),1,"#") = 8

tokenat(tokenat(vartext,2,"_"),0,"#") = bighead
tokenat(tokenat(vartext,2,"_"),1,"#") = 7


Aaaugh! My eyes! My BRAIN! Pain!
I mean, thank you, but, I don't understand a thing. :/
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,377

Post » Sun Oct 19, 2014 3:44 pm

fassFlash wrote:Aaaugh! My eyes! My BRAIN! Pain!
I mean, thank you, but, I don't understand a thing. :/

Too bad nobody can shove "understanding" into your brain except yourself and manual is your girlfriend, play with her.

This technique is usable when you want to simplify/automate thing much further than array, I used this whenever I come across irregular data size and array can't do this without serious logic in place, tokenat simplifies it. Maybe you have to look at array first before using the tokenat method.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Sun Oct 19, 2014 3:53 pm

DuckfaceNinja wrote:
fassFlash wrote:Maybe you have to look at array first before using the tokenat method.


Well, an action RPG has at least from 20 to 100 enemies. Having an array for each of them is even worse than making a clutter out of instance variables. And I haven't heard anything about Instance Arrays. Is there such a thing?
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,377

Post » Sun Oct 19, 2014 3:58 pm

fassFlash wrote:Well, an action RPG has at least from 20 to 100 enemies. Having an array for each of them is even worse than making a clutter out of instance variables. And I haven't heard anything about Instance Arrays. Is there such a thing?

Yes you can do this with a container.

The other method is, if you currently use 2D array, you can make use of 3D array, it should do the same thing, however this will be quite advanced in a way when you manipulate the array.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Sun Oct 19, 2014 4:14 pm

DuckfaceNinja wrote:
fassFlash wrote:Well, an action RPG has at least from 20 to 100 enemies. Having an array for each of them is even worse than making a clutter out of instance variables. And I haven't heard anything about Instance Arrays. Is there such a thing?

Yes you can do this with a container.

The other method is, if you currently use 2D array, you can make use of 3D array, it should do the same thing, however this will be quite advanced in a way when you manipulate the array.


What about enemies that are spawned randomly? Will they position on the 3D array somehow too?
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,377

Post » Sun Oct 19, 2014 4:48 pm

fassFlash wrote:What about enemies that are spawned randomly? Will they position on the 3D array somehow too?

You have to assign the array's z-index to the instance variable of the spawned enemies. As I told you, doing this by array is very advanced, use container if you are unsure how to tackle the array.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Sun Oct 19, 2014 9:03 pm

fassFlash wrote:I don't remember exactly this is called in programming, I think it's massives.


Never heard of it :)

fassFlash wrote: which adds to the already cluttered variable list in a complex project like an RPG.


I'm not sure why instance variables should be cluttered? Sure if there are repeated variables across many objects you could make it a bit nicer with Family instance variables but otherwise, you have data, you need variables! Be wary of doing 'tricky' things like combining values unless you really need to, because you will trade off ease of use/readability with 'efficiency'.

I think first thing to try is a 2D array with Y being each NPC 'number' and X being each attribute. 2D arrays aren't too hard. You will need to use '.at() for everything, but you can make this easier with constants such as "HEALTH=0", "XP=1" etc and use these to index into your array columns (X). You can have a npc.number to index into your rows (Y). You can go a step further then and have functions do this for you, e.g. GetHealth( npc.UID)

I can see by your question "Will they position on the 3D array somehow too?" you must read up and experiment with arrays before tackling them in your game. Good luck and remember, eschew obfuscation!
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: Elliott, Google [Bot] and 35 guests