• Paradox Games has announced today their new game “Millennia”, a semi-historical turn-based 4X game. Find out more here .

[PYTHON] Global variable

muzbang

Prince
Joined
Feb 22, 2007
Messages
319
Hi all,

I'am pretty new to civ4 modifications, and despite i've got some programming basics, i have a problem to implement an event with python (i've done a lot, and now trying some tricky ones:scan:)

Here is what i want to do :
-- to select 4 random ressources. That is ok, i got a function, written in CF, that return a 4 ressources list
-- to lauch a quest with the goal to link a city with those 4 ressources. No problem to launch the quest based on my triggers.

But i got a Problem :
I can't check if the quest is done cause i can't access to the 4 random ressources
I don't want my function to random 4 ressources more than once. I just want to have 1 draw.

So... i put 2 lines in the CvEventManager, in the onGameStart function.
Code:
global ManaL
ManaL = cf.Random_4_mana()
(I know... global is something to avoid :nuke::shifty::nuke:)

When i do tests (in the CvEventManager's onBeginGameTurn function), i can access to my global variable ManaL (and print the random selection to ensure that it's "static")

But when i want to access to it in the CvRandomEventInterface, it's not recognized.

I hope the problem is sufficiently clearly explained that some of you can help :thumbsup:.
i'm pretty sure it's simple but i miss something.

Thanks !
 
Hi, thanks for your consideration.
I have a few questions you should consider:
  • can you modify the DLL code?
  • should the 4 resources be saved? (savegame code)
  • are you ready to sacrifice network multiplayer or should network sync be considered?
  • should the event be once for each game or can it be repeated?
The solution to your issue depends on the answers to those questions.

1) I never modified DLL, i recognize that i would prefer not to, but... i'll probably have to one day. I guess that initializing a global variable in the game context would be the solution. If a solution without DLL is possible i'll be interested in
2) Yeah, of course, if game is saved, i'll expect that a current quest don't disapear after a reload
3) No, i just do MODification to play with friends :cool:
4) In this particular case, it's just once (my aim is to provide 1 specific quest for each civilization)

PS : i've read a topic https://forums.civfanatics.com/threads/python-sd-toolkit.146130/ speaking about Script that maybe help ? but i'am not aware how to use it...
 
3) No, i just do MODification to play with friends :cool:
You don't want multiplayer because you just want to play with friends? To me that sounds like needing multiplayer, but it's entirely up to you.

I'm trying to think of a way to save a new variable without modding the DLL, but I can't think of a way. Regardless of what you do, it will feel like a hack as the DLL is responsible for savegames.

PS : i've read a topic https://forums.civfanatics.com/threads/python-sd-toolkit.146130/ speaking about Script that maybe help ? but i'am not aware how to use it...
I never looked at this. I always mod the DLL and use python only if the DLL can't handle the task.
 
You don't want multiplayer because you just want to play with friends? To me that sounds like needing multiplayer, but it's entirely up to you.
I think he meant that he is not ready to sacrifice multiplayer.
 
An easy/lazy solution without DLL changes could be:

1. The event popup triggers an onModNetMessage to guarantee that
player's selection is handled on all clients.
Code:
CyMessageControl().sendModNetMessage (INT iData1, INT iData2, INT iData3, INT iData4, INT iData5)
# This is a NetMessage designed specifically for modders to use to make their mods Multiplayer friendly, eliminating Out-of-Sync errors. Check out 'onModNetMessage()' in CvEventManager for the callback
(from http://civ4bug.sourceforge.net/PythonAPI/)

2. CyGame, CyPlayer, and CyCity providing the setScriptData(String) and getScriptData() functions.
I assume here that the script data string is not already used by other features of your mod.
Then you can add the list of ressources or/and the selection into this string
 
Ok thanks for your replies. I'll probably set a static prereq ressource list by now (i'am pretty lazy too) and learn how to change the DLL for a proper implementation, as scripting seems to have limited capabilities (fort several random list. Or not easily)
 
Top Bottom