Jump to content
Sign In to follow this  
jms

problem with my lua script :(

7 posts in this topic Last Reply

Highlighted Posts

Posted:
Last Online: A long, long time ago... 
 

hi guys,
i've been slowly teaching myself lua over the last little while but i'm stumped at the moment.
my script doesn't seem to work.
at the moment, all i want to do is create an external file and put two variables in it.  i'm taking small steps at the moment, but i've run into trouble.
can anybody see what's wrong with this?




--#-package:031EBA61# -- package signature

-- Process a file

thisCity  = 0

-------
DataFile = [[Apps\DataFile.dat]]

function Create_DataFile(filename)
 local exist = io.open(filename)                   --open file
 if not(exist) then                                --if file doesn't exit, create one with this data...
  io.output(filename)
  io.write("$beginfile","\n")
 -- io.write("$nextregion","\n")
  io.write("$endfile","\n")
  io.flush()
  io.output()
 end
end

Create_DataFile(DataFile)

--functions---

function WriteCrime()
    io.output(DataFile)
    io.write(game.trend_value(game.g_police_funding_p, 0) .. "\n")      -- these are just here to test which format is the correct way to write
    io.write(game.g_police_funding_p, 0) .. "\n")                                        -- they are the same variable
    io.flush()
    io.output()
end 


function Proj_main()
     thisCity = 1
 
     if (thisCity == 1) then
         WriteCrime()
     end
 return false
end

a = create_advice_cityplanning('162ea2b5')
a.type = advice_types.ENVIRONMENT
a.trigger  = "(thisCity == 0)"                --triggers at the start of the game until thisCity is given a value
a.frequency = 1
a.timeout = 20000
a.title = [[text@4a4e15a1]]
a.message   = [[text@4a4e15a2]] 
a.priority  = 100                                                                                                         
a.mood = advice_moods.GREAT_JOB                                                                                           
a.once = 0

Share this post


Link to post
Share on other sites
Posted:
Last Online:  
 

you should NEVER use global variables with the same name as my globals. If you do, you will either

a) screw up my mods

or

b) screw up your mods

so when you copy my stuff => RENAME THE GLOBAL VARIABLES <=

In this case I'm using "thisCity" in a totally different way then you, so there's a big chance b) is happening. Rename it and try again. In the case I'm sounding a bit harsh, sorry but I want to exclude the possibility of a) at all cost.

edit: also, writing to files is not exactly beginner's stuff... I suggest you try something (a lot) easier first, like showing stuff in a query window...

Share this post


Link to post
Share on other sites
  • Original Poster
  • Posted:
    Last Online: A long, long time ago... 
     

    hi daeley, i've already shown stuff in query windows and i found it to be quite basic. the only reason i've taken some of your code/variables is because there's no other example of this sort of stuff for sim city.  I am fluent with IO for c, c++, java and i don't find it to be difficult at all.  the only problem with programming in lua in this case is that there is no compiler.  so when it doesn't work, you have no idea what the problem is!

    when i tested this mod, i had removed every other mod i had (of course), so i'm pretty sure the name of the variable has nothing to do with why it's not working. anyway, i'm sure i'll work it out today, but i have two more questions...

    what exactly does a.type = advice_types.ENVIRONMENT do?

    and also, in your mod you've got a small threading function, i was just wondering why you needed to thread in your script? it doesn't look like there are two things trying to run concurrently at all.

    help would be greatly appreciated as i'm not actually doing this for myself, i'm doing this as part of a work experiment.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    I can field the first question...

    Nearly all lua-based mods involving triggers use news items because they have the simplest syntax and there aren't many other known methods. When you are creating a news element it must fall under a specific advisor or it will not work correctly. "type" is the property of advice_cityplanning objects that determines what category it is in. Thus "a.type=advice_types.ENVIRONMENT" sets the news item you are using as a trigger for your output to fall under the environmental advisor, meaning her face will appear next to the message by default, the message will be found under her submenu, et cetera.

    -qurlix

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online: A long, long time ago... 
     

    beautiful, thankyou.

    ok, i simplified my script to the following (just to test out basic functionality) and it still doesn't seem to work. is there something i'm missing?

    --#-package:0bac31a1# -- package signature

    DataFile = [[Apps\DataFile.dat]]

    function ProjMain(filename)

    local exist = io.open(filename) --open file

    if not(exist) then --if file doesn't exit, create one with this data...

    io.output(DataFile.dat)

    io.write("hello!","\n")

    io.flush()

    io.output()

    end

    end

    ProjMain(DataFile)

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online: A long, long time ago... 
     

    oh my god, how did i overlook that.

    thanks daeley, it's working fine now.

    Share this post


    Link to post
    Share on other sites

    Sign In or register to comment...

    To comment in reply, you must be a community member

    Sign In  

    Already have an account? Sign in here.

    Sign In Now

    Create an Account  

    Sign up to join our friendly community. It's easy!  

    Register a New Account

    Sign In to follow this  

    • Recently Browsing   0 members

      No registered users viewing this page.

    ×

    Thank You for the Continued Support!

    Simtropolis depends on donations to fund site maintenance costs.
    Without your support, we just would not be in our 24th year online!  You really help make this a great community. *:thumb:

    But we still need your support to stay online. If you're able to, please consider a donation to help us stay up and running. This helps sustain a platform where we can share our community creations for years to come.

    Make a Donation, Get a Gift!

    Expand your city with the best from the Simtropolis Exchange.
    Make a Donation and get one or all three discs today!

    STEX Collections

    By way of a "Thank You" gift, we'd like to send you our STEX Collector's DVD. It's some of the best buildings, lots, maps and mods collected for you over the years. Check out the STEX Collections for more info.

    Each donation helps keep Simtropolis online, open and free!

    Thank you for reading and enjoy the site!

    More About STEX Collections