![]() |
Variables in arrays not unique across multiple instances.
Hey guys, I've created the following code in preparation for a flocking AI system for Era. There is a problem, however, that when I spawn multiple instances (or even create separate autonomous NPCs), their variables become entangled (they move in exactly the same manner at exactly the same time). Is there any way to ensure that the variables are unique for every instance of the class?
Quote:
Frans |
I think you should qualify the object-local variables with 'this.'. I dont really know the default scope of unqualified variable in the body of functions/events, but i think it either makes them class local, or even global variables.
NPC Code: and i think the general convention for function local-variable, is to just make them members of the temp object, like: NPC Code: to avoid unintentionally over writing global variables with the same name. |
@upsilon: The default scope for un-prefixed is global. They can be accessed by any script that is currently running.
|
I tried prefixing the variables with "this.". The entire thing stopped working. Perhaps I missed something.
*shrugs* |
Quote:
Quote:
without seeing the code, all i can think of, is that maybe you qualified the variables x and y wrong when you were prefixing things. If it worked before without those prefixed, i would leave those two as they were originally. |
the scope of non-prefixed vars:
An NPC's static variables - local to the NPC (can be written x= or this.x=). Any other vars that are built-in (ex. players, npcs arrays) A function parameter- local to the function. Otherwise, it's a global variable that all scripts share. If you're using a variable temporarily, like send some data into a function and simply returning a value, I'd use a temp.var. |
Okay, so I've scoped the vars properly with this. now, and that works fine for individually spawned NPC objects.
There is, however, still the same problem when instantiating instances of this code as a class - despite this.name attribute being unique for each instance. I believe that there may be an issue with the compiler/interpreter for the scripting language w/r/t classes: Perhaps only copying array header/reference when instantiating objects (thus leaving the reference to the start of the first array, rather than creating a new array for each object)? Awesome if it could be fixed, anyway. Cheers, Cel PHP Code:
|
Use code tags to post code, not quote tags. That is extremely hard to read in the state that it's in.
|
Or even better, PHP tags.
|
Thanks for that tip.
Is my code faulty, or is the engine lacking that functionality. If the latter is so, do you know of any workarounds I could implement? |
All times are GMT +2. The time now is 10:21 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.