Jump to content
Korean Random
Sign in to follow this  
CDC

Is it possible to reload a certain script file after returning to garage?

Recommended Posts

 Hello!

 

As said in the thread title I have the following problem:

 

I need to reload the contents a certain file (say "script.cs") that is part of my xvm config before every new round. That file is supposed to map specific sound event names to each vehicle (different ones every time), the change of the file is achieved with an external program.

 

Of course it is possible to reload the whole xvm config (via "autoReloadConfig") but that is excessive and very demanding for the computer. So I wonder if it is possible to reload parts of it at that specific moment.

 

 

Share this post


Link to post

Short link
Share on other sites

Hey, thanks for the reply!

 

I am new to the whole py_macro part of XVM, which seems to be quite powerful indeed. The first problem I ran into was how to use the {{my-vehiclename}} macro in connection with the sixth sense sound, as that is not supported in there apparently. How would I change that with py_macro?

 

What I am trying to do is to use a soundMappings.xc file like this:

{
	"china-Ch01_Type59": "SixthSenseSound1", // in MySoundBank.bnk
	"china-Ch02_Type62": "SixthSenseSound2",
	"china-Ch03_WZ-111": "SixthSenseSound3",
	"china-Ch04_T34_1": "SixthSenseSound1"
	...
}

With a line in sounds.xc:

"xvm_sixthSense": ${"soundMappings.xc":"{{my-vehiclename}}"},

Here the problem is the aforementioned lack of being able to use {{my-vehiclename}}. However, if it were to be static I could perhaps work around that, but as I want those sounds to (potentially) be shuffled around every single game I don't exactly know how to achieve that. And then the second issue is how to reload that file, i.e. remap the sounds, every time. Could you give any hints on how to do that?

Share this post


Link to post

Short link
Share on other sites

Sorry, I don't sure this is possible to do in the config, but you can write your own mod using py_macro.

Just for example - create and put this file to the py_macro folder and disable xvm_sixthSense in th config to avoid duplicate sounds:

 

my_sixth_sense_sound.py

import BigWorld
import SoundGroups
from gui.Scaleform.daapi.view.battle.shared.indicators import SixthSenseIndicator
from xfw import *
# config
SOUND_MAP = {
    "china:Ch01_Type59": "SixthSenseSound1",
    "china:Ch02_Type62": "SixthSenseSound2",
    "china:Ch03_WZ-111": "SixthSenseSound3",
    "china:Ch04_T34_1": "SixthSenseSound1",
}
# main
@registerEvent(SixthSenseIndicator, 'as_showS')
def SixthSenseIndicator_as_showS(self):
    vehicle_name = BigWorld.player().vehicleTypeDescriptor.type.name
    soundId = SOUND_MAP.get(vehicle_name, 'xvm_sixthSense')
    SoundGroups.g_instance.playSound2D(soundId)
Edited by sirmax

Share this post


Link to post

Short link
Share on other sites

Thanks again. I don't think - or I don't get enough - that this will do it, though. The idea is to change the sound map after each round. I would assume the .py File to be loaded into memory.

 

I tried to refer to a file this way:

# main
@registerEvent(SixthSenseIndicator, 'as_showS')
def SixthSenseIndicator_as_showS(self):
    vehicle_name = BigWorld.player().vehicleTypeDescriptor.type.name
    with open('cdc.json', 'r') as myfile:
      soundID=myfile.get(vehicleName, 'xvm_sixthSense')
    SoundGroups.g_instance.playSound2D(soundId)

This does not work, also not with a static link (say "test": "soundInSoundBank") instead of "vehicleName". As I know very little about python there might be a rather obvious problem I don't see.

 

Also does XVM load all .py scripts in the py_macro folder automatically or do I have to add it somewhere?

Share this post


Link to post

Short link
Share on other sites

Also does XVM load all .py scripts in the py_macro folder automatically or do I have to add it somewhere?

Yes, they are loaded automatically.

 

First of all, don't remove imports, they are required.

Also you can search for errors in the xvm.log.

Share this post


Link to post

Short link
Share on other sites

Yes, they are loaded automatically.

 

First of all, don't remove imports, they are required.

Also you can search for errors in the xvm.log.

 

I didn't remove the imports of course, I just omitted them in the quote.

Share this post


Link to post

Short link
Share on other sites

UPD: 

    with open('cdc.json', 'r') as myfile:
      soundID=myfile.get(vehicleName, 'xvm_sixthSense')

This part is wrong, you cannot use file descriptor as a dict. You should load and parse the data. The simpliest way is to use load_config method from xfw module (already imported), it was made specially for this:

cds = load_config('cdc.json') # path is relative to WoT root dir, loaded once per session
 
# main
@registerEvent(SixthSenseIndicator, 'as_showS')
def SixthSenseIndicator_as_showS(self):
    vehicle_name = BigWorld.player().vehicleTypeDescriptor.type.name
    if not cds:
        soundID = 'xvm_sixthSense'
    else:
        soundID = cds.get(vehicleName, 'xvm_sixthSense')
    SoundGroups.g_instance.playSound2D(soundId)
Edited by sirmax

Share this post


Link to post

Short link
Share on other sites

Thanks, very useful information. Particularly:

cds = load_config('cdc.json') # path is relative to WoT root dir, loaded once per session

So there is no way to reload the file based on some event, like returning to garage?

Share this post


Link to post

Short link
Share on other sites

You can load it every time when function called, just move this line to the method body. But this can affect performance, because your file will be loaded and parsed every time when the light bulb showed,

Of course, you can reload it on every event you need, but this should be implemented separately. For example, to reload this file on the battle start, you can add this hook:

 

from Avatar import PlayerAvatar
@registerEvent(PlayerAvatar, 'onBecomePlayer')
def onBecomePlayer(self):
    global cds
    cds = load_config('cdc.json')

Share this post


Link to post

Short link
Share on other sites

Hmm, I tried to implement it test wise, but without success.

 

I created a source file cdc.json:

{
	"xtest": "Sound_CDC1"
}

That sound is, under that name, part of my soundbank. If I use that in sounds.xc for the regular xvm_sixthsense (i.e: "xvm_sixthSense": "Sound_CDC1"), it works, so I can exclude a soundbank problem (xvm.log also says the soundbank is loaded). Do I have to reference it differently?

 

I tried to load it with the code you provided:

import BigWorld
import SoundGroups
from gui.Scaleform.daapi.view.battle.shared.indicators import SixthSenseIndicator
from xfw import *
# main
@registerEvent(SixthSenseIndicator, 'as_showS')
def SixthSenseIndicator_as_showS(self):
    vehicle_name = BigWorld.player().vehicleTypeDescriptor.type.name
    if not cds:
        soundID = 'xvm_sixthSense'
    else:
        soundID = cds.get('xtest', 'xvm_sixthSense')
    SoundGroups.g_instance.playSound2D(soundId)

But without success. The "cds" file is loaded with the code you provide (plus imports, of course):

from Avatar import PlayerAvatar
@registerEvent(PlayerAvatar, 'onBecomePlayer')
def onBecomePlayer(self):
    global cds
    cds = load_config('cdc.json')

I tried removing the if-clause in the first script in case that was the problem but also no success. If a sound is defined in sounds.xc it plays that, but only then and that.

Edited by CDC

Share this post


Link to post

Short link
Share on other sites

You can use log() method to print debug logs in the xvm.log, like:

 

from xvm_main.python.logger import *
 
log('initialization')
from Avatar import PlayerAvatar
@registerEvent(PlayerAvatar, 'onBecomePlayer')
def onBecomePlayer(self):
    log('onBecomePlayer')
    global cds
    cds = load_config('cdc.json')
    log('cds loaded')

Share this post


Link to post

Short link
Share on other sites

Hey, as I was getting back to this I have a question about the following:

 

You can use py_macro for this.

 

I got the idea to work to the point where that is all that is needed. I have a .json file with the sound mappings, a .bnk file containing the sounds and a py_macro that should load it.

 

In particular this works (for a sound file/event called "Default" in my sound bank):

"xvm_sixthSense": "Default",

The sound is played, so the bank will have been loaded correctly. All I need now is to make that vehicle-dependent. For that purpose I wrote the following short py_macro (the BigWorld import should be redundant, but anyway):

import BigWorld
import os.path
import json

def sounds(vehicle):
	with open('res_mods/CDC/sound/sound.json') as sound_file:
		sound_json = json.load(sound_file)
		if (vehicle in sound_json):
			return sound_json[vehicle]
		return 'Default'

@xvm.export('CDC_sounds', deterministic=True)
def CDC_sounds(vehicle):
	return sounds(vehicle)

Problem is, it doesn't even seem to get there. When I put in logs to see what is done the macro doesn't even seem to be accessed. I try to access the macro this way:

"xvm_sixthSense": "{{py:CDC_sounds('{{my-vehiclename}}')}}",

Tried the same with the regular sixth sense in sounds.xc. Python.log says "sound error".

 

Is it not possible to use macros in that file? Or at least not vehicle dependent ones? Or am I just using the wrong one?


This doesn't work either:

"xvm_sixthSense": "{{py:CDC_sounds()}}",

With the macro simplified to:

@xvm.export('CDC_sounds', deterministic=True)
def CDC_sounds():
	return 'Default'

Again the result is (from python.log):

[SOUND_ERROR] Sound fail: 1824773650 - ErrorCode: 15

Share this post


Link to post

Short link
Share on other sites

Now you are doing this wrong. You don't need to touch sounds.xc. As well as you don't need to export method using @xvm.export() decorator.

You have all necessary information above in my answers from 8 to 9 of September.

Share this post


Link to post

Short link
Share on other sites

With that code the problem remains. This:

import BigWorld
import SoundGroups
from gui.Scaleform.daapi.view.battle.shared.indicators import SixthSenseIndicator
from xfw import *
from Avatar import PlayerAvatar
from xvm_main.python.logger import *

cds = load_config('sound.json')

@registerEvent(SixthSenseIndicator, 'as_showS')
def SixthSenseIndicator_as_showS(self):
	global cds
	vehicle_name = BigWorld.player().vehicleTypeDescriptor.type.name
	if not cds:
		soundID = 'xvm_sixthSense'
	else:
		soundID = cds.get(vehicle_name, 'xvm_sixthSense')
	log('vehicle_name: ' + vehicle_name)
	log('soundID: ' + soundID)
	SoundGroups.g_instance.playSound2D(soundID)

Doesn't play the sound. The log shows that the sound is correctly identified from the file (e.g. 'Default'). But while that plays when defined in the config it does not play this way. It doesn't play 'xvm_sixthSense' either if I set 'soundID' to that.

 

There too the python.log shows:

[SOUND_ERROR] Sound fail: 2166136261 - ErrorCode: 15

EDIT:

 

Ah, got it. A simple str(soundID) solves the problem.

 

Thank you sirmax for your help!

Edited by CDC
  • Upvote 1

Share this post


Link to post

Short link
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...