Downloadables

Discuss API design here.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Downloadables

Postby satoon101 » Mon Jan 07, 2013 10:55 am

I have only done minor tests on the new Python-side Downloadables, but my tests so far seem to work just fine (with the newest updated code). You should use it something like the following:

Syntax: Select all

from core.downloads import Downloadables

# Get an instance of Downloadables
downloads = Downloadables()

# Add files you want using the path from the game's path
downloads.add('materials/models/test.vmt')
downloads.add('materials/models/test.vtf')
downloads.add('sound/source-python/test.mp3')
Unloading the script will remove the items from the Python-side list, but will not remove them from the actual stringtable for downloadables. Changing the map should do the trick for that.

Satoon
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Jan 07, 2013 11:08 am

Very nice! :)
Woody
Global Moderator
Posts: 42
Joined: Sat Jul 07, 2012 2:45 am
Location: California

Postby Woody » Mon Jan 07, 2013 4:55 pm

Thanks! Nice work! :D
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Dec 01, 2014 8:47 pm

how do you add a list of file to the Downloadables??

Syntax: Select all

from stringtables.downloads import Downloadables
import os.path
from path import Path

dl = Downloadables()
base_path = Path(__file__).parent
with open(base_path + '/models.txt', 'r') as open_model_file:
for line in open_model_file:
stripped_line = line.rstrip()
d1.add("%s" % stripped_line)


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/download/download.py', line 10, in <module>
    d1.add('%s' % stripped_line)

NameError: name 'd1' is not defined
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Dec 01, 2014 8:58 pm

Did you really look at the error you just posted? It clearly states what your issue is.

Also, there is no need to use "%s" % stripped_line , just use stripped_line
Image
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Dec 01, 2014 9:00 pm

The problem is that you defined dl, but you are trying to access the variable d1 which doesn't exist.

Syntax: Select all

from stringtables.downloads import Downloadables
from path import Path

dl = Downloadables()
base_path = Path(__file__).parent

with open(base_path / 'models.txt', 'r') as open_model_file:
for line in open_model_file:
stripped_line = line.rstrip()
dl.add(stripped_line)


Edit: Satoon was faster :(
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Dec 01, 2014 10:10 pm

lol sorry tired as hell :)
didn't notice d1 and dl :o
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Dec 01, 2014 10:23 pm

so there is no need of the ' ' mark??
if i just want to download 1 file

Syntax: Select all

downloads.add(materials/models/test.vmt)
is ok as well???

vs.

Syntax: Select all

downloads.add('materials/models/test.vmt')
i
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Dec 01, 2014 10:32 pm

You need to pass a string so the quotes are needed. In your first example, you are trying to add the result of a math operation with variables that doesn't exists in your code.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Dec 01, 2014 10:33 pm

You must pass a string. When you are reading the lines of a file, you are already reading string values. If you were to use print(type(stripped_line).__name__) , it would show str . Since materials/models/test.vmt is not a 'variable', you have to make it a string by using quotes or apostrophes.


*Edit: L'In20Cible beat me
Image
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Postby Doldol » Mon Dec 01, 2014 10:36 pm

No not at all, you want to specify text, which you do in Python by using strings, which need " or ' around them.
Simply put look at the highlighting, if it's green on this website, it's seen as text by Python.
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Dec 01, 2014 10:39 pm

ah ok thanks now i get it :)

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 10 guests