Getting the values from an array or hash table in python

For questions about using Classic.

Post » Wed May 12, 2010 11:41 pm

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?
B
1
G
2
Posts: 4
Reputation: 633

Post » Thu May 13, 2010 7:09 am

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
B
16
S
8
G
4
Posts: 136
Reputation: 3,144

Post » Thu May 13, 2010 10:11 pm

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:

[url:30gk0id5]http://dl.dropbox.com/u/5868916/PyStuff2.cap[/url:30gk0id5]
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Fri May 14, 2010 3:38 pm

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.
B
1
G
2
Posts: 4
Reputation: 633

Post » Mon Sep 09, 2013 6:25 pm

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)
https://www.dropbox.com/s/yp3vzp3sakfpl04/PyTable.rar

Edit: Also, I wouldn't have done it without Silent Cacophony's guides. You rock, man!bitworks2013-09-09 18:26:35
B
4
S
1
Posts: 28
Reputation: 435


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests