0 Favourites

Getting the values from an array or hash table in python

This forum is currently in read-only mode.
  • Hello.

    In my game, I am working with a bunch of strings that I need to retrieve in python script. However, that was much harder than I first though. First I tried with hash tables and I made it store the strings correctly. But the hash table don't seem to have a function for retrieving values in python script and after some experimentation I gave up.

    I then saved the strings to an array(construct array, not python array) instead. Sadly, I encountered the same problems. Apparently, the common ArrayName[index] didn't work with constructs arrays. They too had no visible functions that could solve it, so I gave up on this too.

    My third attempt was to use a python array entirely. I did it like this:

    http://codepad.org/HPAf47o4

    The "x"'s are just default values. To insert values in the array, I first set the global variables, then I run the script. This didn't work either. After the script was run, the array was still filled with "x"'s.

    I never thought that this simple thing would be so darn complicated, lol. I am considering to write a C++-plugin that acts as a container for strings that you can actually fetch inside of python, but is this really necessary?

    Any ideas? Am I missing something really obvious?

  • Looks like you indeed can't get the value of a key with Python. However, a quite easy workaround exists by using the Run script action. See this cap:

    http://dl.dropbox.com/u/326175/pythonhashtable.cap

  • Hello.

    It's odd, but I can't seem to find a way to read the HashTable or Array objects either. I assume that this will be fixed sometime, but it can be worked around as well.

    As Daiz pointed out, the 'System: Run Script' action would be the way to set python variables from a construct event. Note that if a string is used instead of a number for the value, you'll have to enclose the value in single quotes, like so:

    -> System: Run Script ("mylist[" & index & "] = '" & stringvalue "'")

    Also, retrieving the python variable value is quite easy, with the 'Python()' expression in construct. Like so :

    Python("mylist[" & index & "]")

    I'd probably just use python lists and these methods, myself.

    I've included a .cap that demonstrates this, and also a way to access a construct array from python using functions. Here:

    http://dl.dropbox.com/u/5868916/PyStuff2.cap

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • All right.

    Thanks for the workarounds on how to fix it, but since there was no really nice way for this I think I'll write a c++ plugin for this. A simple container object that is setable and getable from python.

    I'll upload it later.

  • Sorry, for bumping such an old post, but for anybody who would ever search for a solution that somehow works.. I've managed to write a little script for Python and HashTable integration (I called it PyTable). I still hope to develop it some more (I do this mostly for myself, but would gladly share). Right now it can:

      create, read, change, delete STRINGS create, read, change, subtract, add, multiply, divide, delete INTEGERS create, read, add elements to a LIST create, read, change DICTIONARIES and DICT KEYS

    all written and stored in a CC HashTable plugin, could be accessed globally. If anybody would like to use it, here it goes:

    (script file, *.cap file, readme.txt)

    dropbox.com/s/yp3vzp3sakfpl04/PyTable.rar

    Edit: Also, I wouldn't have done it without Silent Cacophony's guides. You rock, man!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
[Plugin] Hash table
122 34,055
DARKSETH's avatar
DARKSETH
Unread hot topic
89 31,548
mralian1383's avatar
mralian1383
Unread hot topic
35 3,129
deadeye's avatar
deadeye