Author Topic: Function refusing to call correctly  (Read 1947 times)

0 Members and 1 Guest are viewing this topic.

Offline k7g

  • 25
Function refusing to call correctly
Can anyone help with this one please?

Here whats happening:

I'm creating a custom script for ships (turret control / repair systems, etc.)
But I've come across an issue,

Here is a basically what I'm trying to do:
Code: [Select]
#On Game Init:
[
 function ***()
]

#On Frame
[
 call function ***()
]

I've looked out other scripts and that works fine, but when I try to do it, I get nil value errors when calling it. Is there something I missed here?  :blah:

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Function refusing to call correctly
Here is a basically what I'm trying to do:

But is that really what you're trying to do? Because your syntax is all messed up, which is evident if you look at said other scripts. Posting your actual code would be more useful.

 

Offline k7g

  • 25
Re: Function refusing to call correctly
Here we go, just trying to get things working before i start getting tables and that in.

Code: [Select]
#Conditional Hooks

$Application: FS2_Open

$On Key Pressed: [[atc_KeyPressed.lua]]
$On Key Released: [[atc_KeyReleased.lua]]

$On Game Init:
[
ui_keyToggleFunctions = {}
InputStates = {}

ui_keyToggleFunctions["Up Arrow"]    = function(val) InputStates.up    = val end
ui_keyToggleFunctions["Down Arrow"]  = function(val) InputStates.down  = val end
ui_keyToggleFunctions["Left Arrow"]  = function(val) InputStates.left  = val end
ui_keyToggleFunctions["Right Arrow"] = function(val) InputStates.right = val end
ui_keyToggleFunctions["0"] = function(val) InputStates.zero = val end

function doInput()
    if InputStates.up then   
    gr.setColor(200,200,200)
    gr.drawString("Up Key pressed",1,20)
   end
end
]

$On Frame:
[
    doInput()
]
#End

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: Function refusing to call correctly
"On Frame" scripts are evaluated every time gr_flip() is called; "On Game Init" scripts are evaluated towards the end of the initialization process. The "splash screen" is displayed towards the beginning of the initialization process (causing gr_flip() to be called, causing the evaluation of "On Frame" scripts). You're trying to call doInput() before it's been defined.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline k7g

  • 25
Re: Function refusing to call correctly
hmmm...

didn't know that one, i'm surprised its not mentioned on the wiki about the load order. (or I've missed it  :blah:)

So, if make the script only call them at  a mission start, that should fix the issue? due the fact its only running at the startup of FS2 atm.

edit: The script i'm looking has the following Hooks in it (besides the Game Init and On Frame)

$On HUD Draw
$State: GS_STATE_GAME_PLAY
$On Gameplay Start

and &On Mission End (I know how to implement this one)
« Last Edit: November 07, 2014, 12:51:52 am by k7g »

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: Function refusing to call correctly
You probably just want to replace "On Frame" with "On HUD Draw", unless you want to be displaying these messages during mainhalls...
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline k7g

  • 25
Re: Function refusing to call correctly
I was just testing to make sure the script was activating and executing correctly.

And I think what you said about 'Game Init' being almost last when the game loads, I assume that you have the 'On Frame', execute the functions only when a mission starts and the HUD draws(via variables).

edit: It works.

By adding a variable to the 'On Frame' hook, it stops it executing its code until the game starts and the HUD is drawn. Now its time to start getting the script to find the player ship and starting populating some tables...
« Last Edit: November 07, 2014, 01:22:41 am by k7g »