Read Private Variable Dynamically?

This forum is currently in read-only mode.
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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.

  • 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.

  • [quote:x2m8pny0]

    SET NPC('STRING'): "MOVEMENT" & NPC('NUMERIC')[/code:x2m8pny0][/p]
    [/p]
    I believe it should be[/p]
    [/p]
    [code:x2m8pny0]SET NPC('STRING'): "MOVEMENT"   +   NPC('NUMERIC')[/code:x2m8pny0][/p]
    [/p]
    with a '+' instead of an '&'
  • Actually, the + is used for addition where as the & is used for concatenation. I tried the + first unknowingly.

    http://apps.sourceforge.net/mediawiki/construct/index.php?title=Expressions

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

    http://apps.sourceforge.net/mediawiki/construct/index.php?title=Expressions

    I see, my mistake,

    just for info though

    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!"

    now, on to your question:

    if you copied this verbatim from your cap:

    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
  • 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.

  • 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.

    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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

    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')

  • 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.

  • 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.

    right:

    [quote:1nd7w4n3]--STATUS = WHAT NPC IS DOING

    --NUMERIC = TEMPORARY NUMERIC VARIABLE HOLDER

    --STRING = TEMPORARY STRING VARIABLE HOLDER

    that's all the variables he needs

    I believe he meant to say:

    SET NPC('STRING'): "MOVEMENT" & NPC('NUMERIC')

    SET NPC('STATUS'): NPC.Value('STRING')

    the symptoms he was having also point to that. he said when he read the value of Status is was coming out as a long number instead of the value of NPC.Value('STRING'), which was holding the correct value

    this statement

    NPC.Value(NPC.Value('STRING')),

    would cause that problem, because by accident or design, this statement is valid, but does not automatically convert STRING to a string value. Unless he explicitly made it a string in the private variable editor, by default, STRING is a numeric value, which normally doesn't matter unless you use that exact (npc.value(npc.value('STRING')) syntax. i think it was a typo

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)