Read Private Variable Dynamically?

For questions about using Classic.

Post » Sun May 31, 2009 12:58 am

So I've started working on enemy AI, and I've come upon the problem of trying to randomly select a movement or attack to perform. My NPC's are set up with private variables like so.

+NPC
--STATUS = WHAT NPC IS DOING
--NUMERIC = TEMPORARY NUMERIC VARIABLE HOLDER
--STRING = TEMPORARY STRING VARIABLE HOLDER
--MOVEMENTS = 2
--MOVEMENT1 = WALK TOWARDS
--MOVEMENT2 = HOP TOWARDS

Now I've already coded the walking towards and hopping towards movements, and can make the NPC's move as wanted, but I cannot select one to do randomly.

As it is my code is going.
SET NPC('NUMERIC'): random(NPC('MOVEMENTS'))
SET NPC('STRING'): "MOVEMENT" & NPC('NUMERIC')
SET NPC('STATUS'): NPC.Value(NPC.Value('STRING'))

This results in my NPC private variable of STRING equal MOVEMENT1 or MOVEMENT 2, so far things are working perfectly. Now I attempt to read the value of the the MOVEMENT1 or MOVEMENT2 stored in NPC('STRING') and set it to NPC('STATUS').

However instead of having NPC('STATUS') have a value corresponding to MOVEMENT1 or MOVEMENT2, or even the strings MOVEMENT1 or MOVEMENT2, I end up with a string of numbers, "577045393604775701".

I'm sure there are other ways to go about this, however I like the idea of each object containing it's own information for moving and attacking, and the engine interpreting them.

I'm using the most recent STABLE version of Construct 0.98.9, so I don't know if this is a bug that has been fixed in the UNSTABLE releases. I would have switched over, but I have a bit of a deadline for this project and I don't wish to switch over to an UNSTABLE build and risk losing all progress.
B
2
G
3
Posts: 15
Reputation: 880

Post » Sun May 31, 2009 1:21 am

it might help to throw in a few explicit casts to string, like:
SET NPC('STATUS'): str( NPC.Value( str( NPC.Value('STRING'))))
not sure, though.
B
2
S
2
G
4
Posts: 171
Reputation: 1,792

Post » Sun May 31, 2009 1:45 am

[quote:x2m8pny0][code:x2m8pny0]SET NPC('STRING'): "MOVEMENT" & NPC('NUMERIC')[/code:x2m8pny0][/quote:x2m8pny0]

I believe it should be

[code:x2m8pny0]SET NPC('STRING'): "MOVEMENT" + NPC('NUMERIC')[/code:x2m8pny0]

with a '+' instead of an '&'
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun May 31, 2009 1:58 am

Actually, the + is used for addition where as the & is used for concatenation. I tried the + first unknowingly.

[url:1mpq21a6]http://apps.sourceforge.net/mediawiki/construct/index.php?title=Expressions[/url:1mpq21a6]
B
2
G
3
Posts: 15
Reputation: 880

Post » Sun May 31, 2009 7:52 am

[quote="revolther":1nzvmaxx]Actually, the + is used for addition where as the & is used for concatenation. I tried the + first unknowingly.

[url:1nzvmaxx]http://apps.sourceforge.net/mediawiki/construct/index.php?title=Expressions[/url:1nzvmaxx][/quote:1nzvmaxx]
I see, my mistake,
just for info though
[quote="the wiki":1nzvmaxx]The & operator is a very useful way of combining numbers in to strings. It automatically converts any numbers to a string and appends them. For example, instead of:

"You have " + str(Player('lives')) + " lives left!"

you can use the & operator like so:

"You have " & Player('lives') & " lives left!"[/quote:1nzvmaxx]


now, on to your question:

if you copied this verbatim from your cap:
[code:1nzvmaxx]SET NPC('STATUS'): NPC.Value(NPC.Value('STRING'))[/code:1nzvmaxx]
that is where the problem is
I'm sure it's a typo, but it should just be
NPC.Value('STRING')
not
NPC.Value(NPC.Value('STRING'))

the second one I'm not sure is even supposed to work, but it converts your private variable to it's native type. private variables are numeric unless you use the private variable manager to create them. this won't matter at all though if you take out the extra npc.value('. construct automatically converts to whatever it should be
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun May 31, 2009 12:13 pm

I don't think what you want to do is supported (dynamic private variable grabbing). You could put the sprite in a container with a hash table, but currently you can't set up initial hash table variables, so I'm not sure if that solution will appeal to you.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sun May 31, 2009 4:08 pm

[quote="Rich":tcymfd8r]I don't think what you want to do is supported (dynamic private variable grabbing). You could put the sprite in a container with a hash table, but currently you can't set up initial hash table variables, so I'm not sure if that solution will appeal to you.[/quote:tcymfd8r]
I think you may have misunderstood him rich, I think what he's asking is perfectly possible, it's just that it was converting his string value to a numeric value
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun May 31, 2009 4:17 pm

From what I can gather he wants to do something like:

Sprite("Some text" & Sprite('A number'))

This isn't possible, you must explicitly name the variable you want to use in an expression, it can't be dynamically built at runtime through other values/expressions.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Post » Sun May 31, 2009 5:08 pm

[quote="Rich":qcu1k5ax]From what I can gather he wants to do something like:

Sprite("Some text" & Sprite('A number'))

This isn't possible, you must explicitly name the variable you want to use in an expression, it can't be dynamically built at runtime through other values/expressions.[/quote:qcu1k5ax]
I think that was a typo that was causing his numeric nonsense glitch
I think he meant to say

somestring ="Some text" & Sprite('A number')
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun May 31, 2009 5:32 pm

I was answering this segment of the opening post:

SET NPC('STRING'): "MOVEMENT" & NPC('NUMERIC')
SET NPC('STATUS'): NPC.Value(NPC.Value('STRING'))

which attempts to dynamically read a private variable without explicitly naming it. This is not possible.
B
3
S
2
G
5
Posts: 1,777
Reputation: 5,529

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests